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
    JavaScript Promise API

    While synchronous code is easier to follow and debug, async is generally better for performance and flexibility. Why "hold up the show" when you can trigger numerous requests at once and then handle them when each is ready?  Promises are becoming a big part of the JavaScript world...

  • By
    How to Create a RetroPie on Raspberry Pi – Graphical Guide

    Today we get to play amazing games on our super powered game consoles, PCs, VR headsets, and even mobile devices.  While I enjoy playing new games these days, I do long for the retro gaming systems I had when I was a kid: the original Nintendo...

Incredible Demos

  • By
    Using MooTools to Instruct Google Analytics to Track Outbound Links

    Google Analytics provides a wealth of information about who's coming to your website. One of the most important statistics the service provides is the referrer statistic -- you've gotta know who's sending people to your website, right? What about where you send others though?

  • By
    WebKit Marquee CSS:  Bringin’ Sexy Back

    We all joke about the days of Web yesteryear.  You remember them:  stupid animated GIFs (flames and "coming soon" images, most notably), lame counters, guestbooks, applets, etc.  Another "feature" we thought we had gotten rid of was the marquee.  The marquee was a rudimentary, javascript-like...

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.

  3. sam

    The above method creates a sub menu item and you have to be in the parent directory for the menu item to work on a given directory.

    Following tool solves both of these issues. It allows you to open terminal from any directory while you are in the directory. The tool also allows you to write simple shell scripts to add more custom options to the right click menu. The tool is free and there is a GitHub link on the tool’s page if you want to check the source.

    You can get the tool here:
    https://samiyuru.com/project/macOS-finder-right-click-menu

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