我正在构建一个应用程序,用于学习使用AppleScript在动作期间发送多个命令的目的。下面是我正在搞乱的代码,但我已经删除了“”之间的操作,并用数字替换它们。一切都运行得很好,但是把它变成了NSApplescript的initwithsource:line一直很麻烦。
tell application "Terminal"
activate
set currentTab to do script "1"
do script "2" in currentTab
do script "3" in currentTab
do script "4" in currentTab
delay 0.5
tell application "Finder" to set visible of process "Terminal" to false
end tell
将这个AppleScript合并为一行的最佳方法是什么?谢谢!
答案 0 :(得分:4)
“将这个AppleScript合并为一行的最佳方法是什么?”
使用AppleScript? :-D
首先,在AppleScript编辑器中,打开“首选项”窗口,然后单击Show Script menu in menu bar
选项。
然后从屏幕右上方的脚本菜单项中选择Open Scripts Folder
。
使用以下脚本创建新的AppleScript .scptd文档:
tell application "AppleScript Editor"
set string_ to text of first document
-- make a list with each line of the script
set stringLines to paragraphs of string_
set originalDelims to AppleScript's text item delimiters
-- add newlines
set AppleScript's text item delimiters to "\\n"
-- now combine the items in the list using newlines
set stringNewlines to stringLines as string
set AppleScript's text item delimiters to "\""
set stringNewlines to text items of stringNewlines
set AppleScript's text item delimiters to "\\\""
set stringNewlines to stringNewlines as string
set AppleScript's text item delimiters to originalDelims
set stringNewlines to "@\"" & stringNewlines & "\""
set the clipboard to stringNewlines
end tell
(请注意,此脚本并不完美:它适用于您提供的简单脚本,但无法自行转换)。
将其保存为您之前透露的Scripts文件夹中的脚本。
然后打开要转换的脚本文档,并将其作为AppleScript编辑器中的前端文档。然后通过从“脚本”菜单中选择转换脚本来调用转换脚本。
根据您提供的脚本,它应该生成以下常量NSString
:
@"tell application \"Terminal\"\n activate\n set currentTab to do script \"1\"\n do script \"2\" in currentTab\n do script \"3\" in currentTab\n do script \"4\" in currentTab\n delay 0.5\n tell application \"Finder\" to set visible of process \"Terminal\" to false\nend tell\n"