is there a separator so you can put several statements all on one line?
i’m setting up a cron call to an applescript. i don’t want to save the applescript then call that as an application, but i’m going to use the format like: osascript -l AppleScript -e 'tell Application “Finder” to display dialog “punch in” ’
but i want to make it interactive now, and need multiple statements. seems i saw somewhere a long time back that you can use a special character to put multiple statements all on one line of text.
that’s true, i could probably make several separate applescript calls.
but since i’m trying to post a dialog, collect response, and react to response, i can’t think o a way of doing that. except writing to a file or something super clunky maybe.
i guess i just make it into an application script, and call it from cron after all – unless someone has another idea?
First, if you make ‘several AppleScript calls’ you should be aware that each one runs in its own space. Variables, properties, etc. in one call are completely independent from, and unknown to another osascript call, so that probably won’t work.
Second, why is writing to a file and running that ‘super clunky’?
If you save the script as a compiled script you can call that via osascript (‘osascript /path/to/scpt’). This has several distinct advantages - first you can use Script Editor (or similar) to write, edit, compile and test your script (a lot easier to do here than in the command line), and secondly it will be much faster since osascript won’t have to compile the script each time it’s run.
the whole thing was about making a ‘one liner’ so i can put it in cron via cronnix, and call it every morning.
imo, writing one script’s results to a file and handing data between different applescripts or more work/less elegant than just saving the applescript as an app, and using a shell one liner to execute it.
so i’ve compromised: i just made the 4 line applescript into a run only app. i have a one-liner shell script call it every morning from cron. seems to me to be the simplest/quickest way of doing it.
You may be able to use ‘run script’ to run a multi line script. What you do is place your script within quotes, then you run script the text. I don’t know how to format this in unix at the moment, but I’m thinking it can be done.
except that if you want a one liner, then delete until you delete the continuation characters and instead of setting the script_text variable, run script the text.
One can send multiple shell commands by separating them with a semi-colon. Something like this:
set command to "osascript -e 'tell application "Finder" to activate';osascript -e 'tell application "Finder" to display dialog "Hello"'"
do shell script command
--> "button returned:OK"
That’s about all I know about it - I don’t know how the shell handles things when executed this way (in reference to Camelot’s comment about each call running in its own space).
Rob, your code would work, but only because you’re not passing any data between the different osascript commands. In effect you’re running one script that activates the Finder, and another script that tells the Finder to display a dialog.
When the second script runs it’s as much coincidence that the Finder is active.
If you consider the following snippet you’ll see that this doesn’t hold true for more complex scripts involving variables:
When the second osascript command runs it knows nothing about myVar which was set in the preceeding osascript command.
So, in summary, you’re fine as long as the various AppleScript commands are autonomous and don’t need to know anything about any other command in the list.