SunQuest
           Mac Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsOperating SystemsMac Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
Stop making mediocre tutorials.The best tutorials are video! Camtasia Studio makes it easy to create engaging, buzz-building screen videos at any size, in any popular format. Download the free trial!
  #1  
Old March 9th, 2006, 02:25 AM
pilaftank pilaftank is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2005
Location: San Carlos, California
Posts: 12 pilaftank User rank is Corporal (100 - 500 Reputation Level)pilaftank User rank is Corporal (100 - 500 Reputation Level)pilaftank User rank is Corporal (100 - 500 Reputation Level)pilaftank User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 8 h 12 m 43 sec
Reputation Power: 0
Launching a Shell Script from Finder

How do you launch a shell script from Finder? I want to be able to run a ".sh" file by just double clicking it. This seems like an easy one, but I can't seem to find the answer.

One catch (which really shouldn't be a problem) is that the script needs to run in the folder where it resides (since the script operates on files in the folder). Oh, and hard coding paths is not acceptable for this situation.

I tried saving a ".term" file from Terminal and editing the value for "<key>ExecutionString</key>" to launch my script, but the terminal doesn't run in the current folder (very annoying).

I also tried renaming the file extension of the script itself from ".sh" to ".command", but again the script runs in the wrong directory.

Reply With Quote
  #2  
Old March 9th, 2006, 12:25 PM
LinuxPenguin's Avatar
LinuxPenguin LinuxPenguin is offline
fork while true;
Dev Shed God 1st Plane (5500 - 5999 posts)
 
Join Date: May 2005
Location: England, UK
Posts: 5,535 LinuxPenguin User rank is General (90000 - 100000 Reputation Level)LinuxPenguin User rank is General (90000 - 100000 Reputation Level)LinuxPenguin User rank is General (90000 - 100000 Reputation Level)LinuxPenguin User rank is General (90000 - 100000 Reputation Level)LinuxPenguin User rank is General (90000 - 100000 Reputation Level)LinuxPenguin User rank is General (90000 - 100000 Reputation Level)LinuxPenguin User rank is General (90000 - 100000 Reputation Level)LinuxPenguin User rank is General (90000 - 100000 Reputation Level)LinuxPenguin User rank is General (90000 - 100000 Reputation Level)LinuxPenguin User rank is General (90000 - 100000 Reputation Level)LinuxPenguin User rank is General (90000 - 100000 Reputation Level)LinuxPenguin User rank is General (90000 - 100000 Reputation Level)LinuxPenguin User rank is General (90000 - 100000 Reputation Level)LinuxPenguin User rank is General (90000 - 100000 Reputation Level)LinuxPenguin User rank is General (90000 - 100000 Reputation Level)LinuxPenguin User rank is General (90000 - 100000 Reputation Level)  Folding Points: 11590 Folding Title: Novice Folder
Time spent in forums: 1 Month 3 Weeks 1 Day 19 h 23 m 58 sec
Reputation Power: 1008
blah.sh.command

That is an important thing, .sh just won't work

The other thing you need to do is cd.

at the top of your script

cd /path/to/directory

Last edited by LinuxPenguin : March 9th, 2006 at 12:30 PM.

Reply With Quote
  #3  
Old March 9th, 2006, 04:56 PM
stanleypane's Avatar
stanleypane stanleypane is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2003
Location: Baltimore, MD
Posts: 207 stanleypane User rank is Sergeant (500 - 2000 Reputation Level)stanleypane User rank is Sergeant (500 - 2000 Reputation Level)stanleypane User rank is Sergeant (500 - 2000 Reputation Level)stanleypane User rank is Sergeant (500 - 2000 Reputation Level)stanleypane User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 4 Days 6 h 24 m 31 sec
Reputation Power: 21
You might want to take a look at DropScript. I don't have the url handy right now, but it should be an easy one to Google.

I'm not sure what you're trying to accomplish, but this may be of help.

Reply With Quote
  #4  
Old April 17th, 2006, 04:07 PM
pilaftank pilaftank is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2005
Location: San Carlos, California
Posts: 12 pilaftank User rank is Corporal (100 - 500 Reputation Level)pilaftank User rank is Corporal (100 - 500 Reputation Level)pilaftank User rank is Corporal (100 - 500 Reputation Level)pilaftank User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 8 h 12 m 43 sec
Reputation Power: 0
After some digging around, I finally found a reasonable hack.

Code:
#!/bin/sh

##################################################
#  File: doit.sh.command                         #
#  Setup:  $ chmod +x doit.sh.command            #
#  Mac shell script that runs in the current     #
#  folder when you double click it from Finder.  #
##################################################

cd "`dirname "$0"`"
pwd

The $0 variable provides the full path of the file, and the dirname command returns the path to the folder. After the cd command, the script is running in the correct folder. The extra quotes are needed to handle spaces in file or folder names.

Now to get my scripts to run the way they should when double-clicked from Finder, I just add the .command extension to all my shell scripts and insert that little one-line cd command.


________
Translate perfect mac browser style colors.
Comments on this post
SimonGreenhill agrees!

Reply With Quote
Reply

Viewing: Dev Shed ForumsOperating SystemsMac Help > Launching a Shell Script from Finder


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway