Using Automator and WinRar to make RAR archives on Mac

KuyaChris
3 min readMar 11, 2022

--

So I wanted to make RAR archives on a Mac and the official WinRAR download page only provides command line for macOS.

It took me some time to troubleshoot, but I was able to create an Automator Quick Action to create a RAR archive of any file in Finder. You can select multiple folders and it will create an archive for each one. If you want to archive multiple files together, just select their parent folder.

Automator Setup

It’s very simple, but there are some Automator-specific notes in the script below.

I actually had multiple steps before this, setting variables in Automator, but I found out it’s not really needed. Just a single “Run Shell Script” action is enough.

Make sure “files or folders” is selected at the top for “Workflow receives current” so that the quick action shows up in Finder.

The actual code! // Shell Script

Now for the code, this took some Googling and troubleshooting.

The above code should work. No need to read more. The rest is just some notes and explanations.

Additional Explanations

Because WinRAR is installed in /usr/local/bin Automator cannot access that command. The default shell I used is /bin/bash and this first command will allow Automator to access the local user’s commands, where RAR is installed. (Source)

export PATH=/usr/local/bin:$PATH

Now this next next section, for i; do is very useful since it allows to loop through the contained commands for each selected file. Great for batch processing!

Contained in the loop is the actual rar command and the variables needed.

PATH1=$i
NAME=$(basename "$i")
PARENT=$(dirname "$i")
ARCHIVE=$"$NAME.rar"
rar a -ep1 "$NAME" "$PATH1"
mv "$ARCHIVE" "$PARENT"

So the rar command requires a name of the archive and a path to the file you’d like to compress.

Because automator passes the file path as the input information, the variables are there to transform the values to the correct format for rar.

basename is used to remove the file path and just deliver the actual file’s name.

dirname is used to get the path of the file’s parent directory. This is used to move the new archive into the same location as the original file.

For the actual rar command, I’ve added the -ep1 switch to create a a simple archive that matches the file being compressed. Without this, a folder on the desktop will be compressed into a hierarchy of folders that shows its path. So without this, when you unRAR an archive, it will contain multiple new folders. For example:

  • Without -ep1: Users > Username > Desktop > Compressed Folder > Contents
  • With -ep1: Compressed Folder > Contents

To note, the variables are also listed in quotes because Automator provides paths that are not terminal-friendly. Path names with spaces will not work properly without these quotation marks. The proper way is to have a backslash preceding every space like Desktop/Compressed\ Folder/Contents, but this is an easier fix.

Finally, we have a mv command. As a default, the rar command will create the new archive in the home folder of the user. I didn’t want this, so I just have a basic command to move the new archive into the same directory as the original file.

Hope this helps and saves someone some time!

--

--

KuyaChris
KuyaChris

Written by KuyaChris

Filipino American creative. Host of The Filipino Garage podcast available on iTunes & Spotify. Visit KuyaChris.com for more info 🙏🏾🇵🇭

No responses yet