I want to bind a shortcut to run a command in Terminal with text that I selected while web browsing. How could I do this?


You can achieve this by making an automator workflow and saving it as a service.

Open Automator.app, choose to make a Service.

In the top menu set "Service receives selected" to "text" in "any application".

From the Actions, Library menu search for "Get Value of Variable" and drag to the right in the workflow area. Again in the Actions, Library search for "Run AppleScript" and drag to the workflow area.

In the "Run AppleScript" action enter the flowing code:

on run {input, parameters}

    set terminalCommand to item 1 of input

    tell application "Terminal"
        do script terminalCommand
    end tell

    return input
end run

Add a variable by clicking on the little double bar icon at the bottom of the window and then right-clicking in the variables frame to create a new variable. Name your variable anything you like. When your done your workflow should look like below. Save it and name it something like "Run in Terminal". Now when your in a web browser you can right click and navigate to Services and you will see your "Run in Terminal" command, which will open up terminal and run the highlighted text.

enter image description here

  • For anyone else who's just done this and would like to test it, here's an arbitrarily complicated terminal expression that prints the number of items in your home directory: python -c "print(len(\"\"\"$(ls -A)\"\"\".split()))" Jun 22 '17 at 5:49

You might find PopClip very handy. There's an extension to do just that called Terminal.

Check it out. It's a great simple and useful app. They have several extensions for quite useful shortcuts. Here it is in the Mac App Store.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .