Skip to content

Instantly share code, notes, and snippets.

@hynkle
Last active December 17, 2023 11:25
Show Gist options
  • Save hynkle/7432418 to your computer and use it in GitHub Desktop.
Save hynkle/7432418 to your computer and use it in GitHub Desktop.
Getting AppleScript to output multiple lines to shell when executed with `osascript` proved challenging.

I wanted to output a list of single-line strings, each item separated by a newline. I couldn't find a way to do that without combining them all into a single string. Fine.

Reasonable attempt:

osascript -e '
set myList to {"foo", "bar", "baz"}
set myString to ""
repeat with myItem in myList
  set myString to myString & myItem & return
end repeat
return myString
'

No dice. That outputs:

baz

Finally got it working with:

osascript -e '
set myList to {"foo", "bar", "baz"}
set myString to "" as text
repeat with myItem in myList
  set myString to myString & myItem & linefeed
end repeat
return myString
'

Difference #1: putting as text after the empty string. Difference #2: using linefeed rather than return for the newline.

Which outputs the expected:

foo
bar
baz
@noSumo
Copy link

noSumo commented Mar 30, 2017

Thank you for posting this script, really helped me sorting out mine ;)

@aaronclarke
Copy link

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment