将 Applescript 更改为单行 NSApplescript 源

编程入门 行业动态 更新时间:2024-10-25 00:29:12
本文介绍了将 Applescript 更改为单行 NSApplescript 源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

我正在构建一个应用程序,用于学习在操作过程中使用 applescript 发送多个命令的目的.下面是我弄乱的代码,但我已经去掉了"之间的动作,并用数字代替了它们.在 applescript 中一切正常,但是将其变成 NSApplescript initwithsource: 行是一个麻烦.

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

将这个applescript 合并成一行的最佳方法是什么?谢谢!

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

推荐答案

将这个applescript合并成一行的最佳方法是什么?"

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

使用 AppleScript?:-D

Use AppleScript? :-D

首先,在 AppleScript 编辑器中,打开首选项"窗口并单击选项以在菜单栏中显示脚本菜单.

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

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

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

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

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).

将其作为脚本保存在您之前显示的 Scripts 文件夹中.

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

然后打开您要转换的脚本文档,并将其设为 AppleScript 编辑器中的前端文档.然后通过从脚本菜单中选择它来调用转换脚本.

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.

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

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"

这篇关于将 Applescript 更改为单行 NSApplescript 源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

更多推荐

[db:关键词]

本文发布于:2023-04-17 14:37:40,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/915918.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Applescript   NSApplescript

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!