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
    Designing for Simplicity

    Before we get started, it's worth me spending a brief moment introducing myself to you. My name is Mark (or @integralist if Twitter happens to be your communication tool of choice) and I currently work for BBC News in London England as a principal engineer/tech...

  • By
    7 Essential JavaScript Functions

    I remember the early days of JavaScript where you needed a simple function for just about everything because the browser vendors implemented features differently, and not just edge features, basic features, like addEventListener and attachEvent.  Times have changed but there are still a few functions each developer should...

Incredible Demos

  • By
    MooTools onLoad SmoothScrolling

    SmoothScroll is a fantastic MooTools plugin but smooth scrolling only occurs when the anchor is on the same page. Making SmoothScroll work across pages is as easy as a few extra line of MooTools and a querystring variable. The MooTools / PHP Of course, this is a...

  • By
    HTML5 Input Types Alternative

    As you may know, HTML5 has introduced several new input types: number, date, color, range, etc. The question is: should you start using these controls or not? As much as I want to say "Yes", I think they are not yet ready for any real life...

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!