Create Context Menu Items in Mac Finder

By  on  

As much as I like exercising my command line skills, for many tasks I'd rather click a time or two.  The problem is that yarn install, brew install, and other package managers don't come with UI extending capabilities...so knowledgable people like us need to find a way to shoehorn those functionality into a UI.  That UI on Mac is Automator; let's explore how we can add custom context menu items within macOS Finder!

Creating a Context Menu Item with Automator

The example we'll use is creating a context menu to unrar rar archives.  The following steps will have you on your way to adding a context menu within macOS Finder:

  1. Open Applications -> Automator
  2. File -> New in the menu bar
  3. Chose a document type of Service

  4. Within the right pane's top panel, choose files or folders from the first dropdown and then Finder.app from the second dropdown.
  5. Within the left actions pane, select Run Shell Script

  6. When the Run Shell Script box appears in the right pane, change the Pass input value to to arguments.
  7. Now you can write whatever shell scripting you'd like, receiving the file arguments as $@.  My script to unrar'ing files is overly simple:
    # Enter the first file's directory
    current_path=$(dirname "$1")
    cd "$current_path"
    
    # For every file, unrar it to the current directory
    for f in "$@"
    do
    	/usr/local/bin/unrar x "$f"
    done
    
    # Exit 0 so annoying errors don't popup 
    exit 0
        

    You can add additional checks to ensure the file is of the correct extension and other validation using a bit more scripting.
  8. File -> Save in the menu bar to save the Automator task.  The name you save the task as is what will display as the context menu name.

Debugging and Testing the Context Menu Item

The best method for testing and debugging an Automator task is by adding additional actions to the task to simulate real usage.  In our case, adding a Get Specified Finder Items action allows us to choose a valid file and test the outcome of the action:

Automator provides error and success messages in when running tasks, a luxury that using the context menu may not provide you.  When you're satisfied that your Automator task is working properly, delete any temporary debugging actions you created.

Using the Context Menu Item

Right-click any files or folders in Finder (in our case, just .rar files) and choose Services -> (Your Task Name Here):

Your task will execute in the background.

While I pride myself on improving my command line skills, I wish I had researched Automator task creation years ago.  Downloading a file to finder and then jumping over to command line is sometimes inconvenient -- sometimes it's just nice to perform a few clicks instead of remembering commands and argument formats.

Recent Features

  • By
    5 More HTML5 APIs You Didn’t Know Existed

    The HTML5 revolution has provided us some awesome JavaScript and HTML APIs.  Some are APIs we knew we've needed for years, others are cutting edge mobile and desktop helpers.  Regardless of API strength or purpose, anything to help us better do our job is a...

  • By
    Send Text Messages with PHP

    Kids these days, I tell ya.  All they care about is the technology.  The video games.  The bottled water.  Oh, and the texting, always the texting.  Back in my day, all we had was...OK, I had all of these things too.  But I still don't get...

Incredible Demos

Discussion

  1. Akis

    Thank you for all your tips / tricks :) I really appreciate them. But this one is not complete I think because /unrar is not an app or executable file. Where I can find and install it?

    • unrar needs to be installed with a package manager like homebrew.

    • Akis

      Thank you for your response. I did a little google search and I found the solution a couple of minutes after posting my initial question :) Forgive me. Now I am trying to add the specific variable into the script, so every RAR file to unrar it’s content within a same-name-as-the-file folder.

  2. I don’t know how to work with mac apps and make it compatible to iOS devices.

Wrap your code in <pre class="{language}"></pre> tags, link to a GitHub gist, JSFiddle fiddle, or CodePen pen to embed!