22

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

kb_
  • 1,245
  • 4
  • 18
  • 33
  • 1
    possible duplicate of [Print to stdout from osascript/Applescript](http://stackoverflow.com/questions/8766868/print-to-stdout-from-osascript-applescript) – Raphael Schweikert Nov 14 '13 at 09:34

3 Answers3

18

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.

Raphael Schweikert
  • 18,244
  • 6
  • 55
  • 75
  • 2
    This is a much simpler approach than spawning a separate shell process just to print the output of another. In the above example, this would look like `return the quoted form of the output` right before the `end run` line. – Beejor May 05 '15 at 01:50
  • this is much much better. Thank you – fabrizioM Oct 11 '15 at 06:44
  • 5
    But it sure would be nice to be able to just output chunks of text as they are encountered, rather than having to accumulate them all into a big string, and then having to return that big string at the end. – Krazy Glew Mar 04 '16 at 19:05
  • This also seems to be a way to prevent a newline from being output. (`log` does this; `return` doesn't.) – mjs Aug 18 '17 at 22:22
13

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*
tripleee
  • 175,061
  • 34
  • 275
  • 318
markhunte
  • 6,805
  • 2
  • 25
  • 44
  • Hm. That doesn't seem to work either. Updated with how I'm running it. Thanks for pointing that out :) – kb_ Mar 24 '13 at 23:52
  • I see. How about posting more code. Which may help to get a better idea of what you are doing. Because either way of running should produce something. What are you getting back in terminal. – markhunte Mar 25 '13 at 00:06
  • Yeah so I'm not only getting errors if the script has crashed in the terminal, however when I have it running in the AppleScript Editor, all of the log data comes out. I'll post some more code. – kb_ Mar 25 '13 at 00:09
  • Also I assume you mean osascript "/path/to/myFile.scpt" – markhunte Mar 25 '13 at 00:10
  • Actually the file is a tab delimitated file so it's a .txt. I don't have issues with getting and reading the file. – kb_ Mar 25 '13 at 00:11
  • updated the answer. I should have suggested do shell script and echo first but my log test worked for me with a simple code. But not for this – markhunte Mar 25 '13 at 00:25
  • Still isn't working. I replaced "copy output to stdout" with "do shell script "echo " & quoted form of output" and it still doesn't print anything – kb_ Mar 25 '13 at 00:30
  • Can you put up an example of the text in the text file. I will try it on that. – markhunte Mar 25 '13 at 00:34
  • I've added it. Thanks a lot for your help. Appreciate your time on this. – kb_ Mar 25 '13 at 00:44
  • Arg ok looks like my terminal or something must be messed up. Thanks for the help – kb_ Mar 25 '13 at 00:51
  • I had someone else try it on their computer, and it works. Thank you for your help! – kb_ Mar 25 '13 at 01:06
7

Figured out this based on the first answer:

copy "Hello World!" to stdout
  • 4
    Note that if you do several outputs like this then only the last will be visible. The solution is to save the strings as you go along (`set FinalString to FinalString & CurrString & "\n"`) and output the concatenated string at the end. – Philip Kearns Sep 19 '20 at 08:18