Shell commands as a link

Trigger Terminal Command As a Link

What I usually do is I keep a list of commands I frequently execute (e.g. mvn build, git update) in a text file accompanying whatever I work on at the moment, along with notes and references.

Each time I would want to run a command, I had to copy + switch to Terminal + paste. It is not exactly intellectually challenging process but it gets a bit tedious after while. Therefore I decided to automate it by creating custom terminal: URL protocol scheme which enables me to execute any script with click of a mouse.

1. define terminal scheme

screen-shot26536

2. create handler

screen-shot26549

This is very simple AppleScript handler. All it does it tells Terminal to activate and then runs a command which is passed in the URL. …and don’t forget to return 1 upon success.

3. activate the new URL scheme

Click the Save and Activate button and hide LinCastor application, leaving it running in the background.

Using the new ‘terminal:’ scheme

For this demonstration I regular TextEdit. However, most applications supporting embedded links should work with this.

Create link (Edit -> Edit Link…)

screen-shot26609

Enter shell command a link’s destination, with prefix terminal: You can split the lines using pressing <atl + enter> key. Although links can be few K in size, this method is not intended to do more then to trigger a few commands. If you need something more complex, think about putting it into a proper shell script file and trigger it from the link instead.

The resulting link should look something like this:

screen-shot26686

When you click it, command ‘ls -lrt’ (which lists a directory) should execute in your Terminal.

A word of caution!

Executing arbitrary command by clicking on a link is powerful thing to do. Since a link can be embedded in any HTML (web site, email, chat), be careful where it comes from and who is sending it to you before you click it.