4

Is it possible to add gzip (or any other command-line commands) as an option when you right-click on files in the Finder? There is a "Compress" option, but it makes a zip.

1

1 Answer 1

3

You can use the Automator application to create a Quick Action, which can execute gzip (or any other command-line commands) as an option when you right-click on files or folders in the Finder application. The quick action needs to be stored as a .workflow file in your ~/Library/Services folder. Below is a example, where the gzip -r command can be executed on selected files. The AppleScript can be modified to execute other commands.

Note: This example was done using macOS Monterey version 12.5. I also tested using macOS High Sierra version 10.13.6. Prior to macOS Mojave version 10.14, Quick Actions are referred to a Services.

The AppleScript may seem lengthly, but has the following features:

  • There is a popup dialog that prompts the user confirm before executing the command.
  • There is a popup message when an error occurs. This message is currently truncated to 2000 characters.
  • Complete error messages can be viewed using the Console application.
  • The Finder application updates the window containing the selected files after executing the command.

The image below shows how the Quick Action is configured in the Automator application.

Monterey automator

Below is the AppleScript that is partially shown in the above image.

to truncateReturns from userstring
    repeat while (userstring ends with return)
        if userstring = return then return ""
        set userstring to text from beginning to item -2 of userstring
    end repeat
    return userstring
end truncateReturns

to replaceCharacters of userstring over maxlength by endstring
    if userstring ends with return then
        set userstring to truncateReturns from userstring
    end if
    if length of userstring > maxlength then
        set userstring to (text from beginning to item maxlength of userstring)
        set userstring to (truncateReturns from userstring) & endstring
    end if
    return userstring
end replaceCharacters

on finderLogger(input)
    set logger to "printf"
    repeat with listitem in input
        set liststr to listitem as string
        if liststr = "<date>" then
            set liststr to (do shell script "date  +'%Y-%m-%d %H:%M:%S'" as string)
        end if
        set logger to logger & " " & quoted form of liststr
    end repeat
    set logger to logger & " >> ~/Library/Logs/Finder.log"
    set stdout to do shell script logger
    return stdout
end finderLogger

to fileInfo for thelink given following:followingBoolean
    set thequoted to quoted form of thelink
    if followingBoolean is false then
        set cmd to "if [ -L " & thequoted & " ];then echo LNK;fi"
        set followingBoolean to (do shell script cmd) is not "LNK"
    end if
    
    if followingBoolean is false then
        set linkFolderHFS to (do shell script "dirname " & thequoted) as POSIX file as text
        set linkFileHFS to (do shell script "basename " & thequoted) as POSIX file as text
        tell application "Finder" to set thefile to file linkFileHFS of folder linkFolderHFS
    else
        set linkFullHFS to thelink as POSIX file as text
        tell application "Finder" to set thefile to alias linkFullHFS
    end if
    return thefile
end fileInfo

on run {input, parameters}
    set cmd to "gzip -r"
    set messagetext to "Do you want run gzip on"
    set wait to 3600
    set maxlength to 2000
    considering numeric strings
        if system version of (system info) < "10.14" then
            set workflowname to "service gzip"
        else
            set workflowname to "quick action gzip"
        end if
    end considering
    
    try
        set filecount to count of input
        if filecount = 0 then error "The list of items was empty."
        
        set filelist to ""
        set fullpath to ""
        repeat with currentfile in input
            set fullpath to POSIX path of currentfile
            if length of fullpath > 1 and text -1 of fullpath = "/" then
                set fullpath to text 1 thru -2 of fullpath
            end if
            set filelist to filelist & " " & quoted form of fullpath
        end repeat
        
        if filecount is not equal to 1 then
            set messagetext to messagetext & " " & filecount & " items?"
        else
            set fullinfo to fileInfo for fullpath without following
            set displayname to displayed name of fullinfo
            set filekind to kind of fullinfo
            set messagetext to messagetext & return & "“" & displayname & "” " & filekind & "?"
        end if
        tell application "Finder"
            activate
            with timeout of wait + 100 seconds
                set status to display dialog messagetext buttons {"Stop", "Continue"} with icon caution giving up after wait
            end timeout
        end tell
        if gave up of status then error "Time out occurred after wait for Continue button to selected."
        if button returned of status is not equal to "Continue" then return input
        set fullcommand to cmd & filelist & " 2>&1"
        set stdout to do shell script fullcommand
    on error errmsg number errnum
        try
            finderLogger({"%s %s: %s\\n", "<date>", workflowname, errmsg})
            error (replaceCharacters of errmsg over maxlength by "…")
        on error errmsg number errnum
            tell application "Finder"
                activate
                with timeout of wait + 100 seconds
                    display alert errmsg as critical giving up after wait
                end timeout
            end tell
        end try
    end try
    try
        tell application "Finder"
            activate
            tell front window
                update every item with necessity
            end tell
        end tell
    end try
    return input
end run

The image below shows an error message appearing in the Finder log.

Monterey console


Below are the corresponding images taken from macOS High Sierra version 10.13.6.

High Sierra automator

High Sierra console


I should also point out that if you are not interested in having a popup to confirm and being notified when an error occurs, then you could simply modify this answer to use the command gzip -r.

5
  • That worked! It actually saves in ~/Library/Services automatically, so you don't even need to worry about that. Do you know how to make the menu item appear in the right-click menu directly instead of under "Quick Actions"?
    – burger
    Sep 15, 2022 at 19:11
  • 1
    I believe originally the menu item did appear in the right-click menu directly. However, this was changed to appear under "Services", then finally under "Quick Actions". So I do not know how to make the menu item appear in the right-click menu directly when using your version of macOS. Sep 16, 2022 at 4:38
  • I saw some tutorials where it showed up directly in the right-click menu. I could not determine what exactly they did differently. Maybe it was a different version.
    – burger
    Sep 16, 2022 at 16:44
  • As I already stated, originally the menu item did appear in the right-click menu directly. For example, this is true if you are using OS X Snow Leopard version 10.6.8. So you are viewing old tutorials. (I happen to have Snow Leopard installed in a VirtualBox virtual machine so I can verify what I have posted in this comment.) Sep 16, 2022 at 17:09
  • 1
    Here is an image of Snow Leopard installed in a virtual machine. The Automator application window shows a service which just creates a popup. The Finder application window shows this service exists in the ~/Library/Services folder. The next image shows right clicking on a file in the Finder application window. The MyTest service does appear directly in the menu. This final image shows the popup generated when MyTest is selected. Sep 16, 2022 at 18:04

You must log in to answer this question.

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