Print to Stdout with applescript

kb_ picture kb_ · Mar 25, 2013 · Viewed 21.7k times · Source

I'm trying to run an AppleScript script from the terminal, however I can't get it to print anything by calling

 osascript myFile.scpt "/path/to/a/file"

I'm trying:

on run fileName

set unique_songs to paragraphs of (read POSIX file fileName)

repeat with nextLine in unique_songs
    if length of nextLine is greater than 0 then
        set AppleScript's text item delimiters to tab
        set song to text item 2 of nextLine
        set artist to text item 3 of nextLine
        set album to text item 4 of nextLine

        set output to ("Song: " & song & " - " & artist & " - " & album)
        copy output to stdout
    end if
end repeat
end run

The tab delimited file is formatted something like this:

1282622675  Beneath the Balcony Iron & Wine The Sea & the Rhythm    
1282622410  There Goes the Fear Doves   (500) Days of Summer        
1282622204  Go to Sleep. (Little Man Being Erased.) Radiohead   Hail to the Thief

Tabs aren't really showing up well on this :(

Answer

Raphael Schweikert picture Raphael Schweikert · Nov 14, 2013

When running the AppleScript with #!/usr/bin/osascript, you can just return your desired text output with a return statement at the end of your script.