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

10.5: Create executable command-line AppleScripts UNIX
It looks like AppleScript 2 in Leopard now supports # as a comment character, so it's possible to have a unix world-executable AppleScript, eg:
#!/usr/bin/osascript
tell app "iTunes" to pause
Save that as pauseitunes.sh and chmod +x it. Much better than the old 'here doc' method.

[robg adds: A comment on the queue review site indicates that you can name your script anything you like; it doesn't have to end in .sh. More info can be found in the AppleScript release notes.]
    •    
  • Currently 2.00 / 5
  • 1
  • 2
  • 3
  • 4
  • 5
  (4 votes cast)
 
[20,016 views]  

10.5: Create executable command-line AppleScripts | 11 comments | Create New Account
Click here to return to the '10.5: Create executable command-line AppleScripts' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
10.5: Create executable command-line AppleScripts
Authored by: jspivack on Nov 13, '07 09:05:01AM
Re: robg's comment:

whoa! last year, I tried driving applescripts via a web page, and ended up using acgi dispatcher (http://www.sentman.com/acgi/) to do so, with spotty results. Are you saying that this is now unnecessary? in other words, I can now host a webpage on a Mac mini, and have users execute applescripts by clicking on links on that webpage? is this new leopard behavior?
thanks!
Jeff

[ Reply to This | # ]
oops
Authored by: jspivack on Nov 13, '07 09:06:08AM

oops! wrong hint. sorry.



[ Reply to This | # ]
10.5: Create executable command-line AppleScripts
Authored by: allanmarcus on Nov 13, '07 02:28:25PM

yes, but two things to remember:

1) The server needs to be logged in for most (if not all) applescripts.

2) The AppleScripts are executed on the server, not the client.



[ Reply to This | # ]
what hint??
Authored by: sdrubbins on Nov 13, '07 11:10:24AM

Wait! That sounds really useful! What hint was that in reply to?



[ Reply to This | # ]
10.5: Create executable command-line AppleScripts
Authored by: mjb on Nov 13, '07 12:33:20PM
As robg said, you don't have to use .sh as an extension. And in fact, you shouldn't.

In the unix world, an extension on an executable is intended only to show what type of content the executable is, and .sh indicates that the executable is a script written in shell code (/bin/sh).

There are many different extensions in use, but there are no hard and fast rules.. .pl is usually perl, .py is python, etc. For applescript you could use .as, or even leave the extension off all together.

[ Reply to This | # ]

Standard is ".scpt"
Authored by: Anonymous on Nov 13, '07 12:56:11PM
A moment of research shows there's already a standard for this, so please use it.

From the man page for osascript:

NAME
     osascript -- execute AppleScripts and other OSA language scripts
Notice that this isn't just for Applescript.

The man page gives examples of script files with ".scpt" extensions. Please use that.

[ Reply to This | # ]

Standard is ".scpt"
Authored by: adrianm on Nov 13, '07 10:59:24PM

Not really, .scpt is what Script Editor uses when saving as a 'script.'

If anything, you should use .applescript as that is what Script Editor uses by default when saving apple scripts as text.

But the first responder was correct in that one shouldn't really use an extension at all.



---
~/.sig: not found



[ Reply to This | # ]
10.5: Create executable command-line AppleScripts
Authored by: jubalkessler on Feb 11, '09 01:53:11PM

I completely disagree with any suggestions of leaving off a file extension for OSA scripts. Let me put it this way. When viewing files on the commandline -- or even in Finder view -- it's impossible to tell what kind of script it is unless (a) it's been typed by the OS via type/creator codes, which is deprecated IIRC, and (b) file extensions. One could also look inside the file, but that's tedious on a large scale.

So if I'm looking at ten or twenty scripts, half of which are Bourne shell (.sh) and the other OSA, then I definitely want to differentiate based on filename.

There's nothing wrong with using filename extensions, and it's good practice to use ones that are commonly agreed (as much as possible, anyway) to represent, and only represent, AppleScript scripts.

So perhaps .scpt after all.



[ Reply to This | # ]
10.5: Create executable command-line AppleScripts
Authored by: jwz on Nov 13, '07 02:49:27PM

It's always been possible to do that like so:

#!/bin/sh

exec osascript << EOF

tell application "itunes"
...blah blah blah...
end tell

EOF



[ Reply to This | # ]
10.5: Create executable command-line AppleScripts
Authored by: adrianm on Nov 13, '07 10:52:16PM

yes, but i did say that this is nicer than using 'here' docs.

---
~/.sig: not found



[ Reply to This | # ]
10.5: Create executable command-line AppleScripts
Authored by: arcimboldo on May 19, '09 08:07:05AM
It's called "shebang" http://en.wikipedia.org/wiki/Shebang_(Unix) Another useful thing to do (very common in UNIX world) is passing the script via standard input, as in:
  echo 'tell application "iTunes" to play' | osascript


[ Reply to This | # ]