从osascript/Applescript打印到标准输出
我有一些AppleScript代码,我正在用它执行从osascript/Applescript打印到标准输出,applescript,Applescript,我有一些AppleScript代码,我正在用它执行osascript。这是一个更大的Perl程序的一部分。我希望能够从AppleScript打印到标准输出,然后让Perl脚本处理输出。但我无法从AppleScript中打印。我该怎么办 以下是我尝试过的: 执行shell脚本“echo Foo”。不知道怎么回事 对打开/dev/fd/1进行了一些欺骗。对我来说,我得到了一个错误“找不到Macintosh HD:dev:fd:1文件” 以下是我正在运行的脚本: tell application
osascript
。这是一个更大的Perl程序的一部分。我希望能够从AppleScript打印到标准输出,然后让Perl脚本处理输出。但我无法从AppleScript中打印。我该怎么办
以下是我尝试过的:
。不知道怎么回事执行shell脚本“echo Foo”
- 对打开/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应该更简单
谢谢我不知道该怎么做,我也不懂Perl,但是我认为如果您将URL收集在字符串中而不是列表中,可以简化Perl解析。每个url将位于字符串的单独一行上。Perl应该能够非常轻松地将其转换为数组,然后用它做一些事情。类似下面的applescript。当然,您可以在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
我发现我可以使用“log”将结果转储到STDERR, 虽然我不得不使用Chrome而不是Safari: #!/usr/bin/osascript tell application "Chrome" repeat with w in every window repeat with t in tabs of w log (get URL of t) end repeat end repeat end tell #!/usr/bin/osascript 告诉应用程序“Chrome” 在每个窗口中重复w 在w的制表符中重复t 日志(获取t的URL) 结束重复 结束重复 结束语
只需使用
log
即可
MacBookPro:~zxj5470$cat demo.scpt
告诉应用程序“终端”
设置WindowNum以获取窗口计数
日志窗口数
结束语
MacBookPro:~zxj5470$osascript demo.scpt
1.
谢谢,这将使解析更容易。输出基本上与我打印的一样。但是我将暂缓接受这个答案,希望其他人知道如何在不建立字符串的情况下打印。为什么从osascript访问stdout的唯一方法是最终返回值?这对我来说是最好的答案,因为您可以从循环中输出到其他程序,适用于大规模循环。AppleScript hello world是终端上的一行:osascript-e'log“hello world!”
,它应该打印:hello world代码>到标准输出。您可以打印内置变量的内容,例如:log version
,它可以打印类似1.0
的内容。选择的标准输出取决于运行它所使用的解释器的版本(osascript)和前几行中定义的应用程序上下文,Finder、Chrome和Terminal都会以不同的方式重定向日志输出。要接收命令行参数,请在运行argv时使用。使用显示对话框
访问屏幕。