gpt4 book ai didi

applescript - 从 osascript/Applescript 打印到标准输出

转载 作者:行者123 更新时间:2023-12-02 21:50:54 30 4
gpt4 key购买 nike

我有一些使用 osascript 执行的 AppleScript 代码。这是一个更大的 Perl 程序的一部分。我希望能够从 AppleScript 打印到标准输出,然后让 Perl 脚本处理输出。但我无法从 AppleScript 中进行打印。我该怎么办?

这是我尝试过的:

  • 执行 shell 脚本“echo Foo”。不输出 Foo。
  • This Google Groups discussion使用一些技巧来打开/dev/fd/1。对我来说,我收到错误“File Macintosh HD:dev:fd:1 was not found”

这是我正在运行的脚本:

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 应该更简单。

谢谢

最佳答案

我不知道如何做你所要求的事情,而且我不了解 Perl,但是我认为如果你在字符串而不是列表中收集 url,你可以使 Perl 的解析变得简单。每个 url 将位于字符串的单独行上。 Perl 应该能够很容易地将其转换为数组,然后用它做一些事情。像下面的苹果脚本一样。当然,您可以在 applescript 中使用不同的分隔符。我使用了“return”,但它也可以很容易地是“逗号”或您想要的任何其他字符。在 Perl 中,将字符串更改为数组对您来说最简单的方法。

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

关于applescript - 从 osascript/Applescript 打印到标准输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8766868/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com