DEV Community

Ahmed Musallam
Ahmed Musallam

Posted on

6 1

How to add a "run script" context menu item in macOs

I usually write a shell script file for everything... so I don't have to remember long a$$ commands, especially for running servers or starting builds. It just makes everything easier.

I usually use "New Terminal Tab at Folder" service to, you guessed it, cd into that folder then run my shell script ./run.sh

New Terminal Tab at Folder

You might be thinking: "well, you can just double click it", yeah, I can, but I usually reserve double clicking for when I need to "edit" the shell script, not when I need to "run" it. Because I am frequently editing shell scripts.

So I thought, what if I add a context menu item "Run Script". So I can right-click a file then click "Run Script" and it will open a terminal and run my script for me.

run script

googled 'round, didn't quite find something like that, so I created it! Here is how:

  1. Open Automator
  2. Choose Service
  3. Drag the Run AppleScript action into the workflow
  4. add code below
  5. Save the workflow as run script
  6. Enjoy!


tell application "Finder"
    # Get the directory of the selected file, store it in "selDir"
    set selDir to POSIX path of (parent of first item of (get selection as alias list) as alias)
    # Get the path of the selected file, store it in "selPath"
    set selPath to POSIX path of (selection as text)
    # Optional commands to show dialog, for debugging puposes.
    # display dialog selDir as text
    # display dialog selPath as text
end tell

tell application "Terminal"
    # Open terminal, cd to selected file directory, then run te file.
    do script "cd " & selDir & " && " & selPath
end tell


Enter fullscreen mode Exit fullscreen mode

And now I realized that I've spent about an hour and a half learning some AppleScript when I should have been working on my side project... #MySideProjectsHaveSideProjects πŸ˜…

Heroku

Deploy with ease. Manage efficiently. Scale faster.

Leave the infrastructure headaches to us, while you focus on pushing boundaries, realizing your vision, and making a lasting impression on your users.

Get Started

Top comments (2)

Collapse
 
vivekkodira profile image
Vivek Kodira β€’

Thank you :)

Collapse
 
rowild profile image
Robert Wildling β€’

How would I get the current folder instead of the parent folder?

Image of Stellar post

πŸš€ Stellar Dev Diaries Series: Episode 1 is LIVE!

Ever wondered what it takes to build a web3 startup from scratch? In the Stellar Dev Diaries series, we follow the journey of a team of developers building on the Stellar Network as they go from hackathon win to getting funded and launching on mainnet.

Read more

πŸ‘‹ Kindness is contagious

Engage with a wealth of insights in this thoughtful article, valued within the supportive DEV Community. Coders of every background are welcome to join in and add to our collective wisdom.

A sincere "thank you" often brightens someone’s day. Share your gratitude in the comments below!

On DEV, the act of sharing knowledge eases our journey and fortifies our community ties. Found value in this? A quick thank you to the author can make a significant impact.

Okay