How to execute Terminal command using selected text?
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?
Solution 1:
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"
activate
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.
Solution 2:
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.