Run shell scripts from the Script Menu

Apr 23, '03 09:18:00AM

Contributed by: discordantus

I was perusing the contents of the Script Menu bundle, and I saw that it had bundled icons for, of all things, shell scripts and perl scripts! So on a whim, I tried dropping a shell script in my ~/Library/Scripts folder. Well, it shows up in the menu with a nice shell script icon, and runs fine when I choose it from the menu, without opening up Terminal.app. I'm going to use this to run my X11 apps.

[robg adds: After some email conversations with discordantus, we've figured out why this worked for him and wouldn't work for me, hence the following qualifications on this hint:

  1. You absolutely must include the #! line at the top of your script, ie #! /bin/sh. If you don't, it thinks it's an AppleScript applet. It depends on this line to tell it what shell to run the script in.
  2. If you don't set the executable bits (chmod a+x filename), it'll show up, but fails to run (completely without notice) when you choose it from the menu.
  3. When the script menu reloads itself, it seems to just scan its folder for any new or missing files. It doesn't check to see if the actual contents of the files have changed. So you may need to remove the menu and relaunch it to see some changes.
  4. When you run a script via the script menu, it seems to effectively redirect the STDOUT (standard output, ie the Terminal window in most cases) to /dev/null. So all that output just flies out into the ether, and you don't see anything. So you'll need to redirect the script output elsewhere in order to see the results.
As a real-world example, I started with the script in the random fortune from thinkgeek hint, and modified it to dump its output into a new TextEdit document:
  #!/bin/sh
  #
  # fetch a fortune from thinkgeek
  #

  curl -s http://www.thinkgeek.com/fortune.shtml | \
  sed -n '/(refresh for another)/,/table\>/p' | \
  sed -n '/<p>/,/<\/p>/{/[.]/p; }' | \
  sed '{/^<[/]*p>/d; s/<[BbRr]*>//g; s/<\;/</g; s/>\;/>/g; }' > \
  /tmp/output.txt
  open /tmp/output.txt
I then copied this script from my ~/bin directory into my ~/Library/scripts directory, and ran it from the menubar. The end result was a new TextEdit window containing a random fortune from thinkgeek.com. Hopefully, you'll be able to find more useful applications of this trick, but it does demonstrate that it works!]

Comments (14)


Mac OS X Hints
http://hints.macworld.com/article.php?story=20030418030024194