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?
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.
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)
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.