首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将Applescript改为单行NSApplescript源代码

将Applescript改为单行NSApplescript源代码
EN

Stack Overflow用户
提问于 2011-09-26 08:46:54
回答 1查看 1K关注 0票数 2

我正在构建一个学习使用applescript在一个动作中发送多个命令的应用程序。下面是我正在处理的代码,但我已经去掉了"“之间的动作,并将它们替换为数字。在applescript中一切都很好,但是将这段代码放入NSApplescript initwithsource:行中是一件麻烦的事情。

代码语言:javascript
复制
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合并为一行的最佳方法是什么?谢谢!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2011-09-26 10:40:26

“将此应用程序脚本合并为一行的最佳方法是什么?”

使用AppleScript?:-D

首先,在AppleScript编辑器中,打开Preferences窗口,然后单击Show Script menu in menu bar选项。

然后从屏幕右上角的script菜单项中选择Open Scripts Folder

使用以下脚本创建新的AppleScript .scptd文档:

代码语言:javascript
复制
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编辑器中打开要转换的脚本文档并将其设置为前端文档。然后通过从script菜单中选择它来调用转换脚本。

给定您提供的脚本,它应该生成以下常量NSString

代码语言:javascript
复制
@"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"
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/7549667

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档