Tested on Mac OS X 10.5 Leopard!
Today someone asked me if it was possible to circumvent osascript’s limitation to disallow any user interaction when executing an AppleScript from the command line. And yes, it is possible to allow user interaction
Let’s assume you have an AppleScript source file named «source.applescript» on your desktop that contains only the following line of code:
display dialog "Hello, world!"
If you open a Terminal window and enter the following command, you will instantly get an error message:
osascript /Users/yourname/Desktop/source.applescript
Error mesage:
execution error: No user interaction allowed. (-1713)
Of course, you could try to write another AppleScript that runs this source file (passed as an argument), but even this fails:
on run {sourcefile}
run script (POSIX file sourcefile)
end run
Just save this code in a source file named «run.applescript» on your desktop and enter the following command into an open Terminal window:
osascript /Users/yourname/Desktop/run.applescript /Users/yourname/Desktop/source.applescript
Again, an error message:
execution error: No user interaction allowed. (-1713)
But as I said before, there is a solution. Just change the code in the file named «run.applescript» accordingly:
on run {sourcefile}
tell application "AppleScript Runner"
do script sourcefile
end tell
end run
If you now enter the command once again into a Terminal window, you won’t get an error message:
osascript /Users/yourname/Desktop/run.applescript /Users/yourname/Desktop/source.applescript
In fact, you will even get the result from the executed AppleScript source file:
{button returned:“OK”}
Happy Scripting
P.S.: You can find AppleScript Runner here: /System/Library/CoreServices/AppleScript Runner.app
And it also contains some hidden commands like do folder action, which you can inspect by opening the following file with a text editor:
/System/Library/CoreServices/AppleScript Runner.app/Contents/Resources/Hidden.scriptTerminology