Here's my attempt at a Caja script that creates a rar archive from the selected files and directories. If there is only one file or directory selected, the archive gets the name of that file or directory (+ ".rar" added). Otherwise it uses the name of the parent directory (+ ".rar" added).
NOTE: you must first install the package gridsite-clients to get the needed urlencode command. Install it through Software Manager or with command:
apt install gridsite-clients
The code might look a bit more complex but that's because I took extra care for it to work perfectly with files and directories that have spaces in their name. It may not work as expected for files that have newlines in their name but probably adding
IFS=' '
to the top of the script makes it work with that also. I didn't test this yet.
Also, why is your rar file in your home directory? I installed rar through Software Manager and it installs in /usr/bin/rar. The variable at the start of the script configures where to find the rar command.
Mind that while I've tested this thoroughly (for how it works with file names with spaces), it isn't fool proof yet. For example if the rar name already exists, I don't know what the rar command will do with the exists rar archive? Overwrite it? Add to it? There should probably be added some logic to check for that and perhaps pop up a question with zenity to have the user choose what to do. Same as with the script not letting you know its progress, any errors or when it's done. You might also want to add the option to the rar command that makes it assume yes to all questions -- because you can't interact with the rar command currently. I could easily make it so this script opens a terminal first and then runs on that though. That way you get to see any errors and questions rar has, and progress.
Code: Select all
#!/bin/bash
RAR=/home/linux/rar/rar
target_dir=$(urlencode -d $CAJA_SCRIPT_CURRENT_URI | sed 's#^file://##')
target_archive=$(basename "$target_dir")
files_arg=""
count=0
for f in $CAJA_SCRIPT_SELECTED_URIS; do
files_arg="${files_arg:+${files_arg} }'$(urlencode -d $f | sed 's#^file://##')'"
(( count++ ))
done
if [[ $count -eq 1 ]]; then
target_archive=$(basename "${files_arg:1:${#files_arg}-2}")
fi
eval "$RAR a -k -s -m5 -ma5 -ep1 '${target_dir}/${target_archive}.rar' $files_arg"