« Share v1.0Share v1.0.1 »

[AS] Applescript and the command line

I just learned today that osascript (the command line Applescript interpreter) will print the result of the last statement made to stdout. This is good because it doesn’t like commands like display dialog (there’s no windowing system for the command line) or log (which seems to be a Script Editor-only thing). So, to print to stdout, you’d likely do something like the following:

property stdout : ""
on run(arg) -- As an aside, you can also pass args to applescripts using the `on run` handler
    (* Do stuff *)
    set stdout to stdout & "Stage 1 status: complete"

    (* Do more stuff *)
    if success then
        set stdout to stdout & return & "Stage 2 status: complete"
    else
        set stdout to stdout & return & "Stage 2 status: failed"
    end if
    stdout -- Will put stdout in result which will be written to the terminal
    -- Alternatively, you could write `set result to stdout`
end run

Leave a Reply