从 osascript/Applescript 打印到标准输出 [英] Print to stdout from osascript/Applescript

查看:52
本文介绍了从 osascript/Applescript 打印到标准输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些使用 osascript 执行的 AppleScript 代码.这是一个更大的 Perl 程序的一部分.我希望能够从 AppleScript 打印到标准输出,然后让 Perl 脚本处理输出.但是我无法从 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?

这是我尝试过的:

  • 执行shell脚本echo Foo".不输出 Foo.
  • 此 Google 网上论坛讨论打开/dev/fd/1 做了一些技巧.对我来说,我收到找不到文件 Macintosh HD:dev:fd:1"的错误
  • 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

由于 osascript 会自动打印程序的最后一个值,我可以将 URL 收集到一个列表中并打印出来.但是我的 Perl 脚本必须解析列表、删除引号等.似乎每行打印一个 URL 应该更简单.

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.

谢谢

推荐答案

我不知道如何做你问的事情,我也不知道 Perl,但是我认为你可以使 perl 的解析变得简单,如果你在一个字符串而不是一个列表中收集你的网址.每个 url 将位于字符串的单独行上.Perl 应该能够很容易地把它变成一个数组,然后用它做一些事情.类似于下面的applescript.当然,您可以在 Applescript 中使用不同的分隔符.我使用了return",但它可以很容易地成为逗号"或您想要的任何其他字符.在 perl 中最容易将字符串更改为数组的方法.

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

这篇关于从 osascript/Applescript 打印到标准输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆