【发布时间】:2021-09-16 06:04:02
【问题描述】:
我有一些 AppleScript 代码正在使用 osascript 执行。这是一个更大的 Perl 程序的一部分。我希望能够从 AppleScript 打印到标准输出,然后让 Perl 脚本处理输出。但我无法从 AppleScript 中打印。我该怎么办?
这是我尝试过的:
-
do shell script "echo Foo"。不输出 Foo。 - This Google Groups discussion 做了一些诡计来打开 /dev/fd/1。对我来说,我收到“找不到文件 Macintosh HD:dev:fd:1”的错误
这是我正在运行的脚本:
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 应该更简单。
谢谢
【问题讨论】:
-
AppleScript hello world 是终端上的这一行:
osascript -e 'log "hello world!"' 应该打印:hello world!到标准输出。您可以打印内置变量的内容,例如:log version,它会打印类似1.0的内容。选择的标准输出取决于你用来运行它的解释器的版本(osascript)和前面几行定义的应用程序上下文,Finder、Chrome 和终端都以不同的方式重定向你的日志输出。要接收命令行参数,请使用on run argv。使用display dialog访问屏幕。
标签: applescript