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

Run shell scripts from the Script Menu UNIX
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!]
    •    
  • Currently 3.00 / 5
  • 1
  • 2
  • 3
  • 4
  • 5
  (2 votes cast)
 
[15,972 views]  

Run shell scripts from the Script Menu | 14 comments | Create New Account
Click here to return to the 'Run shell scripts from the Script Menu' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Run shell scripts from the Script Menu
Authored by: cj on Apr 23, '03 12:54:40PM

It appears that ScriptMenu.menu has an issue with paths
that have spaces in them. After upgrading to 10.2, I
noticed that my shell scripts no longer worked from the
script menu -- didn't spend any time on fixing them until I
saw this hint today. As it turns out, I was getting "No such
file or directory" errors truncated at the first space in the
path; replacing the spaces in the paths with underscores
fixed the problem. This didn't happen with 10.1's Script
Menu, so I'll be sending this bug to Apple, but I thought
I'd post it here in case anyone else was experiencing this
issue.



[ Reply to This | # ]
Run shell scripts from the Script Menu
Authored by: webdog on Apr 23, '03 01:10:27PM

Is there any way to have the system perform "root" level scripts
without being logged in as "root"? It would be great to have a
script that could perform a graceful restart of apache from the
finder, for example.



[ Reply to This | # ]
Run shell scripts from the Script Menu
Authored by: googoo on Apr 23, '03 04:05:53PM

You could write a little applescript interface with

do shell script "path/to/script" with administrator privileges

You will be prompted for your password, and then the script will execute as root. You may want to redirect the stdout and stderr to a file so that you can see what happened.

-Mark



[ Reply to This | # ]
Run shell scripts from the Script Menu
Authored by: wilseven on Apr 23, '03 01:18:23PM

one thing people might want to check out is the fastScripts
program from red-sweater, which is basically the same as the
script menu but takes multiple script locations into account and
has some other feature.

http://www.red-sweater.com/RedSweater/FSFeatures.html

You can run shell scripts from this. As far as running as root,
couldn't you just sudo? If not, how about the applescript
command 'do shell script'?

http://developer.apple.com/technotes/tn2002/tn2065.html



[ Reply to This | # ]
Run shell scripts from the Script Menu
Authored by: satcomer on Apr 23, '03 02:22:57PM

Thanks for the (chmod a+x filename). I've been racking my
brain about that for a little while. This site is golden!



[ Reply to This | # ]
Run shell scripts from the Script Menu
Authored by: Max Mouseroom on Apr 23, '03 10:15:23PM

This hint is very cool! I like it!

However, the sed code is abstruse and garrulous. This performs the same task:

curl -s http://www.thinkgeek.com/fortune.shtml| \
awk '/#F1F5FB/,/\/p/'| sed 's/<.*>//g' > /tmp/output.txt
open /tmp/output.txt




[ Reply to This | # ]
Run shell scripts from the Script Menu
Authored by: bluehz on Apr 24, '03 01:45:00PM
You can also use pbcopy to place the fortune directly on the clipboard - ready to paste.
curl -s http://
www.thinkgeek.com/fortune.shtml | awk '/#F1F5FB/,/\/p/'| sed 
's/<.*>//g' | pbcopy
can any of you sed pros tell me why this won't work to remove the blank lines too?
curl -s http://www.thinkgeek.com/
fortune.shtml | awk '/#F1F5FB/,/\/p/'| sed 's/<.*>//g' | 
sed '/./,$!d'\ | pbcopy
BTW - there is a great resource of sed one-liners here:
http://www-h.eng.cam.ac.uk/help/tpl/unix/ sed.html

[ Reply to This | # ]
Run shell scripts from the Script Menu
Authored by: Impatient1 on Apr 25, '03 05:17:53PM

url_fortune=http://www.thinkgeek.com/fortune.shtml

curl -s $url_fortune |
awk '/#F1F5FB/,/\/p/' |
sed 's/<.*>//g' |
sed 's/.$//' |
sed '/./!d' |
pbcopy



[ Reply to This | # ]
Where is the scripts Menu?
Authored by: SOX on Apr 24, '03 02:43:44PM

Where do I look for or how do I activate the scripts menu



[ Reply to This | # ]
Where is the scripts Menu?
Authored by: bluehz on Apr 25, '03 08:53:11AM

I believe by default - assuming it is included in the default
install - it is usually in /Applications/Applescript. I think you just
launch it once from there and it installs itself - you don't have to
mess with it again. Drag it from menu bar to dispose of it.

You can also find it at the Apple site under Applescript

I would highly recommend you take a look at FastScript
(mentioned above) as a replacement for ScriptMenu - same
functionality - less overhead, and faster.



[ Reply to This | # ]
Where is the scripts Menu?
Authored by: intrinsicchaos on Jun 28, '03 02:48:24PM

just found your comment via Google - i'm trying to find out how to remove the Scripts Menu from my menu bar. The drag-out-of-menu-bar doesn't seem to work; it just opens the menu itself. Do you know of another way to get rid of it? Thanks!



[ Reply to This | # ]
Where is the scripts Menu?
Authored by: Ambush_Bug on May 20, '04 11:08:12AM

Yeah, I had the same problem, too. You have to command(open apple)-drag to get rid of the damn thing.

another "obvious" macism.

don't get me wrong, i love mac os x... but coming from a unix environment, all these wierd option clicks and command clicks which are never documented drive me bonkers.

you can also use this to move the order of the wireless indicator, the power bar, the ichat bubble, the volume controls, and the clock around.... unfortunately it doesn't seem to work for the third party apps i have up there, but i can move them around by changing the order that they get executed in my login items.

hope this helps



[ Reply to This | # ]
Shell scripts from menu - user interaction...
Authored by: bluehz on Apr 25, '03 08:56:08AM

What happens if the shell script you run from the ScriptMenu
requires user interaction - say for example a user password? Is
this even possible in a ScriptMenu execution?



[ Reply to This | # ]
Run shell scripts from the Script Menu
Authored by: brave_baker on Sep 28, '03 01:33:04AM

I've noticed that if you run shell scripts from the menu, you end up with zombie (dead) processes. If you run 'ps -x' in the terminal, you might see a lot of (sh) lines.

Using an applescript and 'do shell script' seems to work without causing this.



[ Reply to This | # ]