Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

Launch Terminal commands via Finder filenames UNIX
You can create a 2-line text file that, when launched in the Finder, will run the command that is the name of the file in the Terminal app. Using your favorite Unix editor create a file containing:
    #! /bin/bash
    exec $(echo ${0##*/} | tr ':' '/')
and save the file named, say, ls /Users/paul. After quitting the editor, in a Terminal window, do a:
    % chmod +x ls\ \:Users\:pjl
(Mac OS X substitutes colons for slashes in the Terminal, and they need to be escaped with backslashes)

In the Finder, do a Get Info on the file and change the Open With application to Terminal. Now double-clicking the file in the Finder will create a new Terminal window and run the command ls /Users/paul in it.

If you want to change the command, simply rename the file in the Finder. I use this for ssh machine so I can ssh to various machines. If you want several such commands, simply duplicate the file in the Finder and rename it. The contents of the file never need to change.

    •    
  • Currently 1.00 / 5
  • 1
  • 2
  • 3
  • 4
  • 5
  (1 vote cast)
 
[4,805 views]  

Launch Terminal commands via Finder filenames | 10 comments | Create New Account
Click here to return to the 'Launch Terminal commands via Finder filenames' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Launch Terminal commands via Finder filenames
Authored by: eagle_eyes on Feb 26, '03 10:46:42AM

Sweet little trick, took me a bit to understand what it was trying to do, but the proof is in trying it!

---
Eagle Eyes
eagleeyes@mac.com

---------------------------------------
MacOSXHints has an irc channel now. Come check it out! Free software to connect:



[ Reply to This | # ]
Launch Terminal commands via Finder filenames
Authored by: jimphelps on Feb 26, '03 11:32:16AM

I get an error message:

[dyn-19-81:~] jphelps% /Users/jphelps/Desktop/ls\ \~jphelps; exit
/Users/jphelps/Desktop/ls ~jphelps: exec: $: not found
logout
[Process completed]



[ Reply to This | # ]
Hmm...
Authored by: robg on Feb 26, '03 12:20:04PM

What's the name of the file when you view it in the Terminal? Looks like it might have an extra backslash in it or something...

-rob.



[ Reply to This | # ]
Launch Terminal commands via Finder filenames
Authored by: JohnnyMnemonic on Feb 26, '03 10:57:13PM

I get much the same looking error:

[Tycho-iceBook:~] md% /Volumes/BigPart/Users/md/Desktop/top.txt; exit
/Volumes/BigPart/Users/md/Desktop/top.txt: exec: top.txt: not found
logout
[Process completed]

Hm.



[ Reply to This | # ]
Launch Terminal commands via Finder filenames
Authored by: JohnnyMnemonic on Feb 26, '03 11:11:56PM

Doesn't like files created using TextEdit; following the same process, but with pico, works fine. Probably because textedit appends an extension to the title, invisible or not--whereas you can strictly dictate the name in pico etc.

There may be another reason, but pico works for me.

Cool hint. Wonder what the max scriptable length is? Could make an eay way to make startup scripts--select these "applets" if you will in the "launch on login".



[ Reply to This | # ]
Launch Terminal commands via Finder filenames
Authored by: blueoak on Feb 26, '03 11:49:55AM

Thanks alot, anonymous. This one's a keeper.



[ Reply to This | # ]
Much easier way to do this
Authored by: sgi_oh_too on Feb 26, '03 02:08:09PM
This hint is a rather strange way to perform this task. Just do the following at a prompt (%)

% cd ~/Desktop/
% echo "top" > top.command
% chmod a+x top.command

then just go double click the newly created "top.command" file on your desktop

[ Reply to This | # ]
Easier once, but...
Authored by: robg on Feb 26, '03 02:55:02PM

Now say you want 20 SSH shells for the various hosts you connect to. With this hint, you can duplicate the file once. With the other method (also documented here somewhere, I'm pretty sure), you have to create the 20 files by hand.

-rob.



[ Reply to This | # ]
Launch Terminal commands via Finder filenames
Authored by: vajonez on Feb 27, '03 02:24:03AM
Neat tip. To avoid having to to mess with "Get Info" and "Open With:" use the following command and save the file with a ".command" extension:
#!/bin/sh
exec $(echo ${0##*/} | tr ':' '/' | sed -e s/\.command$//)


[ Reply to This | # ]
More robust with | sed -e s/.command$//
Authored by: jzsimon on Mar 21, '03 11:58:44AM

This method is actually more robust. Firstly it doesn't require the .command extension but does allow it. Secondly, there are filenames which will not work without the .command extension, because the shell script file takes on the same extension as the last file in the filename, which can change the filetype as far as the OS is concerned.

As an example, try the file name "open /Applications/Preview.app.command". It works great with this more robust method. Then try the file name "open /Applications/Preview.app". This fails miserably because the OS interprets the file as an application, suppresses the .app extension, and then never lets you put it back.



[ Reply to This | # ]