Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Print to stdout from osascript/Applescript

Tags:

applescript

I have some AppleScript code that I'm executing with osascript. This is part of a larger Perl program. I'd like to be able to print to stdout from the AppleScript, then have the Perl script process the output. But I haven't been able to print from within AppleScript. What should I do?

Here's what I've tried:

  • do shell script "echo Foo". Does not ouptut Foo.
  • This Google Groups discussion does some trickery to open /dev/fd/1. For me, I get an error of "File Macintosh HD:dev:fd:1 wasn't found"

Here's the script I'm running:

tell application "Safari"
        set window_list to every window
        repeat with the_window in window_list
                set tab_list to every tab in the_window

                repeat with the_tab in tab_list
                        set the_url to the URL of the_tab
                        -- I'd like to put a print statement here,
                        -- instead of display dialog
                        display dialog the_url
                end repeat
        end repeat
end tell

Since osascript will automatically print the last value of a program, I could collect the URLs into a list and print that. But then my Perl script would have to parse the list, remove quotes, etc. It seems like it should be more straightforward to just print one URL per line.

Thanks

like image 387
Evan Avatar asked Jan 07 '12 02:01

Evan


1 Answers

I don't know how to do what you're asking and I don't know Perl, however I think you could make the parsing from perl simple if you collect your urls in a string instead of a list. Each url would be on a separate line of the string. Perl should be able to turn that into an array pretty easily and then do something with it. Something like the below applescript. Of course you can use a different separator in the applescript. I used "return" but it could just as easily be a "comma" or any other character you want. Whatever is easiest for you in perl to change the string to an array.

set urlString to ""

tell application "Safari"
    set window_list to every window
    repeat with the_window in window_list
        set tab_list to every tab in the_window

        repeat with the_tab in tab_list
            set the_url to the URL of the_tab
            set urlString to urlString & the_url & return
        end repeat
    end repeat
end tell

return text 1 thru -2 of urlString
like image 166
regulus6633 Avatar answered Nov 14 '22 03:11

regulus6633