Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Mac OS X > Scripting in OSX

Scripting in OSX
Thread Tools
Fresh-Faced Recruit
Join Date: Nov 2010
Status: Offline
Reply With Quote
Nov 29, 2010, 02:10 PM
 
Hi, i'm a windows user who seek a way to make .bat-like files on mac.

I know about the terminal, but how do you execute some stored script by doubleclicking a file? (like a bat file does)

Also in a bat file, you can get arguments by using %1 %2 %3 etc.. i need this functionality also.

Can someone give me a clue?
     
Clinically Insane
Join Date: Mar 2001
Location: yes
Status: Offline
Reply With Quote
Nov 29, 2010, 02:16 PM
 
I'd worry about making the script double-clickable once you have actually created your script.

Google Unix shell scripting, or else tell us more about what the script should do so that we can help you get started.
     
Fresh-Faced Recruit
Join Date: Nov 2010
Status: Offline
Reply With Quote
Nov 29, 2010, 04:14 PM
 
Hmm.. im talking about simple simple commandlines..

Ok, foran example if i wanted to open a app with an argument:
/Applications/TheApp/TheApp.app/Contents/MacOS/TheApp -file schema1.xml

So, instead of having to open the terminal, and type this.. i'd like for the user just to be able to doubleclick , and the above script fires.
     
Clinically Insane
Join Date: Mar 2001
Location: yes
Status: Offline
Reply With Quote
Nov 29, 2010, 04:32 PM
 
Check out Platypus (http://www.sveinbjorn.org/platypus), it's a utility that creates executable wrappers out of Unix commands. Last I checked you can also create GUI executable commands with the Terminal app itself.
     
Fresh-Faced Recruit
Join Date: Nov 2010
Status: Offline
Reply With Quote
Nov 29, 2010, 04:57 PM
 
Ok thanks.

I find it really weird that there's isnt a "native" way to do it..

Well..atleast the software you link to is opensource freeware...for now.
     
Mac Elite
Join Date: Feb 2000
Location: Nashua NH, USA
Status: Offline
Reply With Quote
Nov 29, 2010, 08:08 PM
 
"native" would be AppleScript or a bash script.
     
Posting Junkie
Join Date: Dec 2000
Status: Offline
Reply With Quote
Nov 29, 2010, 08:43 PM
 
And AppleScripts can be compiled as executable apps.

Ticking sound coming from a .pkg package? Don't let the .bom go off! Inspect it first with Pacifist. Macworld - five mice!
     
P
Moderator
Join Date: Apr 2000
Location: Gothenburg, Sweden
Status: Offline
Reply With Quote
Nov 30, 2010, 02:17 AM
 
There is a native way to do this - a script file with the suffix .command will be opened and executed by Terminal.app like a shell script.
The low-end Mac Pro is the most overpriced Mac since the IIvx
     
Mac Elite
Join Date: Feb 2000
Location: Nashua NH, USA
Status: Offline
Reply With Quote
Nov 30, 2010, 08:08 AM
 
Have you found a way to get the terminal window to close at the end of the script?
     
P
Moderator
Join Date: Apr 2000
Location: Gothenburg, Sweden
Status: Offline
Reply With Quote
Nov 30, 2010, 10:05 AM
 
Seem to remember that if all outputs are redirected away from the terminal, the window closes, but I'm far from certain.
The low-end Mac Pro is the most overpriced Mac since the IIvx
     
Clinically Insane
Join Date: Mar 2001
Location: yes
Status: Offline
Reply With Quote
Nov 30, 2010, 10:16 AM
 
Does adding "exit" as the final statement in the shell script close the terminal window?
     
P
Moderator
Join Date: Apr 2000
Location: Gothenburg, Sweden
Status: Offline
Reply With Quote
Nov 30, 2010, 11:46 AM
 
I played around a bit, but I couldn't find a way to do so in the file. You can of course do so in the Terminal settings, but not in the file that I can see.
The low-end Mac Pro is the most overpriced Mac since the IIvx
     
Mac Elite
Join Date: Feb 2000
Location: Nashua NH, USA
Status: Offline
Reply With Quote
Nov 30, 2010, 12:37 PM
 
Completely untested applescript version (On a XP machine ATM) Copy the following into ScriptEditor

tell application "TheApp"
activate
open for access "schema1.xml"
end tell
     
Posting Junkie
Join Date: Dec 2000
Status: Offline
Reply With Quote
Nov 30, 2010, 04:27 PM
 
Originally Posted by P View Post
I played around a bit, but I couldn't find a way to do so in the file. You can of course do so in the Terminal settings, but not in the file that I can see.
It's a kludge, but you could end the script with:

osascript -e 'tell application "Terminal" to close the front window'

If having to hit the Return key to dismiss the "Are you sure?" sheet annoys you, you could tack on a second script to simulate a Return keypress using GUI scripting.

Ticking sound coming from a .pkg package? Don't let the .bom go off! Inspect it first with Pacifist. Macworld - five mice!
     
P
Moderator
Join Date: Apr 2000
Location: Gothenburg, Sweden
Status: Offline
Reply With Quote
Nov 30, 2010, 04:54 PM
 
Well if we permit kludges, just ending the script with "killall Terminal" would work too...with the minor sideeffect of closing all open Terminal windows. Perhaps checking if any other shells were running (count the number of login processes) and only kill Terminal if the current window is the only open one, or something.

Probably best to just end the script with

echo Please close this window now
The low-end Mac Pro is the most overpriced Mac since the IIvx
     
Posting Junkie
Join Date: Dec 2000
Status: Offline
Reply With Quote
Nov 30, 2010, 06:22 PM
 
How about this? Write this AppleScript and save it as an application:

Code:
on run set myPath to the POSIX path of (path to me as string) do shell script myPath & "/Contents/Resources/my_script.sh" end run
Then save a shell script as "my_script.sh" and put it inside the AppleScript application's bundle, in Contents/Resources. This will give you a nicely wrapped shell script using nothing but "native" methods.

Ticking sound coming from a .pkg package? Don't let the .bom go off! Inspect it first with Pacifist. Macworld - five mice!
     
   
Thread Tools
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Top
Privacy Policy
All times are GMT -5. The time now is 02:06 AM.
All contents of these forums © 1995-2011 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.7 © 2000-2011, Jelsoft Enterprises Ltd., Content Relevant URLs by vBSEO 3.3.2