将 Applescript 更改为单行 NSApplescript 源

发布于 2024-12-06 05:27:44 字数 492 浏览 0 评论 0原文

我正在构建一个应用程序,用于学习使用 applescript 在操作期间发送多个命令的目的。下面是我正在弄乱的代码,但我删除了“”之间的操作并用数字替换它们。在 applescript 中一切都工作正常,但将其放入 NSApplescript initwithsource: 行一直很麻烦。

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 组合成一行的最佳方法是什么?谢谢!

Im building an app for learning purposes of using applescript to send multiple commands during an action. Below is the code I'm messing with but I have stripped out the actions in between the "" and replaced them with numbers. Everything works fine in applescript but making this into an NSApplescript initwithsource: line has been a bother.

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

What is the best way to combine this applescript into a single line? Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

小糖芽 2024-12-13 05:27:44

“将此 applescript 组合成一行的最佳方法是什么?”

使用AppleScript? :-D

首先,在 AppleScript 编辑器中,打开“首选项”窗口,然后单击“在菜单栏中显示脚本菜单”选项。

然后从屏幕右上角的脚本菜单项中选择打开脚本文件夹

使用以下脚本创建一个新的 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"

"What is the best way to combine this applescript into a single line?"

Use AppleScript? :-D

First, in AppleScript Editor, open the Preferences window and click the option to Show Script menu in menu bar.

Then choose Open Scripts Folder from the script menu item up in the upper right of the screen.

Create a new AppleScript .scptd document with the following script:

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

(Note that this script isn't perfect: it works fine for simple scripts like the one you provided, but isn't able to convert itself).

Save that as a script in the Scripts folder you revealed earlier.

Then open your script document you want to convert and make it the front document in AppleScript Editor. Then invoke the conversion script by choosing it from the Script menu.

Given the script you provided, it should produce the following constant 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"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文