Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Print to Stdout with applescript

Tags:

applescript

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 :(

like image 909
kb_ Avatar asked Mar 24 '13 23:03

kb_


2 Answers

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.

like image 92
Raphael Schweikert Avatar answered Oct 22 '22 19:10

Raphael Schweikert


Its is not very clear HOW you are trying to run it in Terminal. But I will assume you have saved a applescript text file with the #!/usr/bin/osascript shebang, and chmod'ed the file to be able to execute it.

Then called the file in Terminal, by just using the path to the file.

#!/usr/bin/osascript

#Here be the rest of your code ...

set output to ("Song: " & song & " - " & artist & " - " & album)


    do shell script "echo " & quoted form of output
end tell

Update 2, in response to comments.

If I have a tab delimited text file with the content as:

track   Skin Deep   Beady Belle Closer

The tabs are set like : track****TAB****Skin Deep****TAB****Beady Belle****TAB****Closer

And the script file as:

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)
            do shell script "echo " & quoted form of output
        end if
    end repeat

end run

Then in Terminal run:

/usr/bin/osascript ~/Documents/testOsa2.scpt ~/Documents/testTab.txt

I get back:

*Song: Skin Deep - Beady Belle - Closer*
like image 12
markhunte Avatar answered Oct 22 '22 19:10

markhunte