1

I have rar and unrar installed in /usr/local/bin/. I need:

  1. In Finder: to archive selected files and folders with .rar extension to the same folder. Is it possible to make it through right click and context menu? Or with automator services?
  2. In Finder: Unrar selected archives to the same folder in Finder with context menu.

Is it possible with automator and shell script?

4
  • RE: "Is it possible with automator and shell script?", yes it's doable. Commented Apr 24, 2020 at 22:53
  • @user3439894, how can I do this?
    – dizz610382
    Commented Apr 25, 2020 at 1:59
  • @user3439894, I have macOS Catalina. I’m trying to make 2 scenarios: 1. For archiving: There are multiple files and folders all in one folder. When one selects several files and folders, then right click (or goes to Finder>services) and choose «Archive», it archives all selected files and folders into the same folder and creates file archive.rar (or archive«date»«time».rar)
    – dizz610382
    Commented Apr 25, 2020 at 11:58
  • @user3439894, 2. For unarchiving: There are archive file in a folder. When one selects it with right click (or with Finder>services), and choose «Unarchive», it creates a folder named exactly as archive, in a folder where archive is, and unarchives it into this created folder.
    – dizz610382
    Commented Apr 25, 2020 at 11:59

2 Answers 2

2

The example shell script code used in this answer was tested under macOS Catalina 10.15.4.

As coded the location the RAR archive is to be created/extracted to must be readable/writable to you with normal privileges, otherwise the RAR archive is created/extracted on your Desktop. If a selected file/folder to be archived is not readable it will throw an error message and an incomplete RAR archive may be created.

The two Automator Quick Actions work as advertised with the settings as shown in the images of the workflows, further below, so use the same settings!

  • Workflow receives current [files or folders] in [Finder]
  • Run Shell Script action:
    • Shell: [/bin/bash]     Pass input: [as arguments]


  • NOTE: In order for these workflows to work, Finder must be given Full Disk Access in: System Preferences > Security & Privacy > Privacy

  • NOTE: If rar and unrar are not properly signed it will throw an error message. Go to System Preferences > Security & Privacy > General, click the lock to make changes, enter your credentials, click the Allow button. You will still have to grant the permission the next time they run, but not there after.



Create RAR Archive

Using how by default in Finder a ZIP archive is created in macOS, only recursing the relative path from within the PWD, the same applies to the example shell script code used in the first Run Shell Script action of the Automator Quick Action shown below.

In this first Automator Quick Action, as coded, the example shell script code will:

  • Define RAR variables.
  • Change directory to the location of the selected file(s)/folder(s) within the same parent folder of the selected file(s)/folder(s).
  • Build an array of just the basenames of the selected file(s)/folder(s).
  • Verify the PWD is writable and if true, create the RAR archive in the PWD, else create it on your Desktop.
  • Create a RAR archive, named e.g.:
    • Archive 2020-04-25 at 10.56.00 PM.rar
      Containing the selected file(s)/folder(s).

rar_exe="/usr/local/bin/rar"
rar_args="a -r -idq"
rar_archive="Archive $(date '+%Y-%m-%d at %I.%M.%S %p').rar"

cd "$(dirname "$1")" || exit

declare -a my_files
for f in "$@"; do
    my_files+=( "$(basename "$f")" )
done

if [ -w "${PWD}" ]; then
    $("${rar_exe}" ${rar_args} "${rar_archive}" "${my_files[@]}")
else
    $("${rar_exe}" ${rar_args} "${HOME}/Desktop/${rar_archive}" "${my_files[@]}")
fi

Automator Create RAR Archive Quick Action Image



Extract RAR Archive

Unlike how by default in Finder a ZIP archive is extracted in macOS, incrementing the name of the folder the archive is extracted to if it previously exists, the example shell script code used in this second Run Shell Script action of the Automator Quick Action shown below does not do that! If the folder named for the extracted RAR archive already exists, then it is not extracted a second time.

In you need to extract it again, rename or delete the existing extracted RAR archive, or copy the RAR archive to an alternate location, then extract it.

In this second Automator Quick Action, as coded, the example shell script code will:

  • Define UNRAR variables.
  • Change directory to the location of the selected RAR archive(s).
  • Verify the file extension of the selected file(s) is rar.
  • Verify the PWD is writable and if true, extract the RAR archive in the PWD, else extract it on your Desktop.
  • Verify a folder of the same name does not already exist, and if not, then extract the contents of the selected RAR archive(s) in a folder the name of the selected RAR archive(s).

unrar_exe="/usr/local/bin/unrar"
unrar_args="x -ad -idq"

cd "$(dirname "$1")" || exit

for f in "$@"; do
    [ "${f##*.}" == "rar" ] || continue
    d="${f##*/}"
    if [ -w "${PWD}" ]; then
        [ ! -d "${d%.*}" ] || continue
        $("${unrar_exe}" ${unrar_args} "$f")
    else
        cd "${HOME}/Desktop" || exit
        [ ! -d "${d%.*}" ] || continue
        $("${unrar_exe}" ${unrar_args} "$f")
    fi
done

Automator Extract RAR Archive Quick Action Image


Note: The example shell script code is just that and, sans existing error handling, does not contain any additional error handling as may be appropriate. The onus is upon the user to add any error handling as may be appropriate, needed or wanted.

0

The answer given by @user3439894 is great, I just improved the Unrar script so that it extracts an archive despite already having the folder with the same name in the working directory:

unrar_exe="/usr/local/bin/unrar"
unrar_args="x -ad -idq"

cd "$(dirname "$1")" || exit

for f in "$@"; do
    [ "${f##*.}" == "rar" ] || continue
    d="${f##*/}"
    if [ ! -w "${PWD}" ]; then
        cd "${HOME}/Desktop" || exit
    fi
    if [ -d "${d%.*}" ]; then
        d="${d%.*} $(date '+%Y-%m-%d at %I.%M.%S')/"
    fi
    $("${unrar_exe}" ${unrar_args} "$f" "$d")
done

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .