(LM Mate) WinRAR to context menu

All Gurus once were Newbies
Forum rules
There are no such things as "stupid" questions. However if you think your question is a bit stupid, then this is the right place for you to post it. Please stick to easy to-the-point questions that you feel people can answer fast. For long and complicated questions prefer the other forums within the support section.
Before you post please read how to get help
Post Reply
EMKEI
Level 1
Level 1
Posts: 21
Joined: Tue Nov 21, 2017 2:21 am

(LM Mate) WinRAR to context menu

Post by EMKEI » Mon Jan 01, 2018 11:20 am

Hi all,

I use WinRAR on the command line. i often use a similar command:

Code: Select all

/home/linux/rar/rar a -k -s -m5 -ma5 -ep1 "/home/linux/Desktop/Documents.rar" "/home/linux/Desktop/Documents"
I need to simplify my work... add this option to the context menu in linux.

Example:
When I right click the folder. (The context menu opens) and there I can choose "folder to .rar archive".
The .rar archive is created in the current original folder located with these parameters "-k -s -m5 -ma5 -ep1".
If a file with a name already exists, the number is added, example (old "ABC.rar" > new "ABC1.rar")

I can not find a way to do it... Please help me thx.

User avatar
thx-1138
Level 7
Level 7
Posts: 1922
Joined: Fri Mar 10, 2017 12:15 pm
Location: Athens, Greece

Re: (LM Mate) WinRAR to context menu

Post by thx-1138 » Mon Jan 01, 2018 12:09 pm

I need to simplify my work... add this option to the context menu in linux.
...you would need to come up with a script located under .config/caja/scripts...more or less something among those lines below...
#!/bin/bash

title="RAR extract"

rar a -k -s -m5 -ma5 -ep1 "$@" | zenity --text-info \
--title "$title" \
--width=640 \
--height=480
exit 0

User avatar
xenopeek
Level 24
Level 24
Posts: 24102
Joined: Wed Jul 06, 2011 3:58 am
Location: The Netherlands

Re: (LM Mate) WinRAR to context menu

Post by xenopeek » Mon Jan 01, 2018 12:14 pm

You can write scripts for Caja (MATE's file manager) which you can then call from the Scripts submenu in the right-click context menu. An example here: http://www.ethanjoachimeldridge.info/te ... ext-action
Image

EMKEI
Level 1
Level 1
Posts: 21
Joined: Tue Nov 21, 2017 2:21 am

Re: (LM Mate) WinRAR to context menu

Post by EMKEI » Mon Jan 01, 2018 1:11 pm

@thx-1138 and @xenopeek I love you :roll:

PS:
hx-1138: my knowledge in is very limited... I can not edit the script to work properly... :cry:

Code: Select all

#!/bin/bash

title="RAR extract"

/home/linux/rar/rar a -k -s -m5 -ma5 -ep1 "$@" | zenity --text-info \
--title "$title" \
--width=640 \
--height=480
exit 0
https://s17.postimg.org/jjelnkq8f/image.png
https://s17.postimg.org/heu8mjebz/image.png

Script archives files next to folder (not files in folder)

Can you still help me fix this your script?

User avatar
xenopeek
Level 24
Level 24
Posts: 24102
Joined: Wed Jul 06, 2011 3:58 am
Location: The Netherlands

Re: (LM Mate) WinRAR to context menu

Post by xenopeek » Mon Jan 01, 2018 2:43 pm

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"
Image

EMKEI
Level 1
Level 1
Posts: 21
Joined: Tue Nov 21, 2017 2:21 am

Re: (LM Mate) WinRAR to context menu

Post by EMKEI » Mon Jan 01, 2018 3:23 pm

Holy shit!

Thank you very much for your support and for the perfect explanation.

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.
Yes Yes you are absolutely right... For my needs, it would be enough for the file to change its name to.
Example: "ABC.rar", "ABC(1).rar", "ABC(2).rar", etc. (If the new archive has the same name)

PS:
RAR I have not installed but only copied into my home folder. (my stupidity)
So far, the script works perfectly. i will try to add support for "ABC.rar", "ABC(1).rar", "ABC(2).rar", etc.

But honestly, your knowledge is at a different level :mrgreen:
If you had time and tried to add this option...
Last edited by EMKEI on Mon Jan 01, 2018 3:37 pm, edited 1 time in total.

User avatar
xenopeek
Level 24
Level 24
Posts: 24102
Joined: Wed Jul 06, 2011 3:58 am
Location: The Netherlands

Re: (LM Mate) WinRAR to context menu

Post by xenopeek » Mon Jan 01, 2018 3:36 pm

Well, that was easier than expected :) I also tested it with files and directories that had newlines in their name and that works correctly.

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

sequence=""
while [[ -e "${target_dir}/${target_archive}${sequence:+($sequence)}.rar" ]]; do
        (( sequence++ ))
done

eval "$RAR a -k -s -m5 -ma5 -ep1 '${target_dir}/${target_archive}${sequence:+($sequence)}.rar' $files_arg"
Image

EMKEI
Level 1
Level 1
Posts: 21
Joined: Tue Nov 21, 2017 2:21 am

Re: (LM Mate) WinRAR to context menu

Post by EMKEI » Mon Jan 01, 2018 3:45 pm

Thank you very much it is exactly what I wanted (This is the most beautiful christmas present for me) :D

User avatar
xenopeek
Level 24
Level 24
Posts: 24102
Joined: Wed Jul 06, 2011 3:58 am
Location: The Netherlands

Re: (LM Mate) WinRAR to context menu

Post by xenopeek » Mon Jan 01, 2018 4:01 pm

You're welcome. It's handy to have some skills with Bash scripting for automating things like this.
Image

EMKEI
Level 1
Level 1
Posts: 21
Joined: Tue Nov 21, 2017 2:21 am

Re: (LM Mate) WinRAR to context menu

Post by EMKEI » Mon Jan 01, 2018 4:07 pm

Yes you are right (I'm a newbie in linux) until recently I used it windows command line and autohotkey...

Post Reply

Return to “Newbie Questions”