multi-statment script in one line?

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.

thanks in advance

No, there is only the opposite: the continuation character allows you to break a single statement into several lines. option-L (¬)

Presumably, the shell thingy is intelligent enough to make use of escape sequences:

osascript unix crap 'statement 1 n statement 2 n etc… ’

Of course, within an AppleScript string, the escape sequences themselves would have to be escaped:

set s to “osascript unix crap 'statement 1 \n statement 2 \n etc… '”

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?


Um… I don’t think that is what I was suggesting.

In any case, I got the following to work:

do shell script "osascript -l AppleScript -e '
	tell app "Finder"


		display dialog "punch in" buttons {"Punch Out", "Punch In"}

	if (button returned of result = "Punch In") then

		display dialog "You pressed \"Punch In\""
		display dialog "You pressed \"Punch Out\""


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.

thanks for the replies.

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.


Here’s an example:

set script_text to (“try” & return & ¬
“display dialog “hi”” & return & ¬
“display dialog “You pressed OK.” buttons {“OK”} default button “OK”” & return & ¬
“on error” & return & ¬
“display dialog “You pressed Cancel.” buttons {“OK”} default button “OK”” & return & ¬
“end try”)
run script script_text

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.


Here’s the one liner I hope it wraps:

run script (“try” & return & “display dialog “hi”” & return & “display dialog “You pressed OK.” buttons {“OK”} default button “OK”” & return & “on error” & return & “display dialog “You pressed Cancel.” buttons {“OK”} default button “OK”” & return & “end try”)

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

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:

[This script was automatically tagged for color coded syntax by Convert Script to Markup Code]

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.

That’s what I suspected but I thought I’d leave that issue to those in the know (I hoped that you would clarify it). :wink:

– Rob