applescript

Print to Stdout with 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 :(


Solution

  • 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*