Starting an Applescript program from the unix shell?

Hello,
I’m a large unix user and I have a cron job that I have created which employs a perl script to search directories for files and to create log files etc… If a certain file(s) are found I need to be able to open a program that cannot be started from the unix shell. So I thought well I’ll use applescript but as far as I can tell I can’t start applescript from the command line either or is this possible?

thanks for your help,
Dave.

I figured it out. Thanks anyway

Since you figured it out, you probably came across the ‘osascript’ command which runs an AppleScript, either from a file or from the command-line using the -e switch.

Just one caveat, though - osascript cannot (currently) pass arguments to the script file. It only invokes the ‘on run’ handler.

This can be a limitation if you need to be truly dynamic.

Hopefully it’ll get addressed in a future version.

This is not exactly true. By embedding the applesript in a shell script you can pass variables. Look at this addressbook lookup script:


#!/bin/sh

findCode="tell application "Address Book"
	set thePerson to person "$2"
	set theProps to the properties of thePerson
	set the firstName to the first name of theProps
	set the lastName to last name of theProps
	set the phnList to the value of every phone of thePerson
	set testList to firstName & " " & lastName & " " & phnList
	return testList
end tell"

addCode="tell application "Address Book"
	set thePerson to make new person at end with properties {first name:"$2", last name:"$3"}
	make new phone at end of phones of thePerson with properties {label:"$4", value:"$5"}
end tell"
	
delCode="tell application "Address Book"
	delete person "$2"
end tell"

case $1 in
	h | help | -h | -help) 
		echo "Useage:"
  		echo "	To find a Phone Number: phone -f "FirstName LastName""
 		echo "	To add a Name and Phone Number to the phonebook do: phone add "FirstName" "LastName" "label" "123-456-789""
 		echo "	To remove a user from the phone book do: phone delete "FirstName LastName""
  		echo "	NOTE: Be sure to use quotes around entries"
  		;;
	f | find | -f | -find) 
		osascript -e "$findCode"
		#if ["$?" -ne "0"]; then
		#	echo "This name was not found in the Address Book database"
		#	echo "To add a Name and Phone Number to the phonebook do: phone add "FirstName" "LastName" "label" "123-456-789""
		#fi
		;;
	a | add | -a | -add) 
		osascript -e "$addCode"
		;;
	d | delete | -d | -delete) 
		osascript -e "$delCode"
		;;
esac

Notice how this script passes a variable from the command line to the shell script and then passes it to the applescript to run.

Thanks.
SA :smiley:

Interesting trick, macman, but I still stand by my statement. :slight_smile:

Your hack simply uses shell scripting to rewrite the AppleScript code before it gets to osascript.

What should happen, of course, is your script has a hander such as:

on run (someVar)
...
end run

and you can:

osascript myScript someVar

and have someVar passed to the run handler.

Of course, even better would be the ability to put:

#! /bin/applescript

as the first line of your script and give AppleScript the same respect given to perl and shell scripters.

Regardless, I still maintain that osascript cannot pass arguments to an AppleScript handler. :slight_smile:

Sure, if you really want to split hairs …
:slight_smile:

But at least there is a work around. And I too, wish Applescript would be updated to run like other scripting languages. This is an obvious gui hold-over from Classic (or OS9). Now that we have a powerful operating system, it is time to design an engine for applescript and let it play with the big boys. I believe osascript was Apple’s attempt to combine both worlds. I for one don’t use OS 9 at all, so everything I do can be done from the terminal. But that is just me, I’m sure there are plenty of people holding onto OS9 with a death grip.

(Disclaimer: Before any old timers start flaming this, I am not disrespecting OS9. I just have no use for it)
:slight_smile:

Thanks.
SA
:smiley:

Are you sure about that? Most Mac programs can be launched by using the command ‘open’ from the shell.

For example:

open /Applications/TextEdit.app

will open TextEdit. Basically, open works about the same as if you had double-clicked an item in the Finder. Read the man page for more info.

Even simpler:

Most apps that OSX knows of will open with

open -a Application name

For example:

open -a TextEdit

Opens the TextEdit Application.

Good Luck.
SA :smiley:

For that matter, the open -a nomenclature lets you specify an application to open a file with, actually improving on double-clciking the file in the Finder. :slight_smile:

Try this:
/usr/bin/osascript -l AppleScript -e ‘tell Application “Your program name here” to activate’