如何向我的 Cocoa 应用程序添加 Applescript 支持?
我是 Cocoa 编程领域的新手,我想向我的应用程序添加 Applescript 支持。苹果网站上的例子似乎已经过时了。
如何向我的 Cocoa 应用程序添加 Applescript 支持?
I am new to the world of Cocoa programming, and I want to add Applescript support to my application. The examples at Apple's website seem out of date.
How do I add Applescript support to my Cocoa application?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您想从应用程序发送 AppleScript 并需要沙盒应用程序,则需要创建临时权利
您需要在 info.plist 中添加这两个密钥
...当然您必须将“MyAppName”更改为您的应用程序名称
创建一个 .sdef 文件并将其添加到您的项目中。
现在进一步的课程很大程度上取决于您的应用程序的需求,有:
-
转到此处查找有关其实现的详细说明和许多细节:https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/ScriptableCocoaApplications/SApps_script_cmds/SAppsScriptCmds.html
我发现可以使用类和 KVC 元素非常复杂,因为我只想执行一个命令,没什么花哨的。因此,为了帮助其他人,这里有一个示例,说明如何使用一个参数创建一个新的简单命令。在此示例中,它将“查找”一个字符串,如下所示:
此命令的 .sdef 文件如下所示:
创建 NSScriptCommand 的子类并将其命名为 MyLookupCommand
MyLookupCommand.h
MyLookupCommand.m
基本上就是这样。其秘诀在于继承NSScriptCommand并覆盖performDefaultImplementation。我希望这可以帮助某人更快地获得它...
If you want to send AppleScript from your application and need a sandboxed app, you need to create a temporary entitlement
You need to add those two keys in your info.plist
...of course you have to change "MyAppName" to your app's name
Create a .sdef file and add it to your project.
The further course now greatly depends on the needs of your application, there are:
-
Go here to find a detailed description and many details on their implementation: https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/ScriptableCocoaApplications/SApps_script_cmds/SAppsScriptCmds.html
I found working with Class and KVC Elements very complicated, as I just wanted to execute a single command, nothing fancy. So in order to help others, here's an example of how to create a new simple command with one argument. In this example it'll "lookup" one string like this:
The .sdef file for this command looks like this:
Create a subclass of NSScriptCommand and name it MyLookupCommand
The MyLookupCommand.h
The MyLookupCommand.m
That's basically it. The secret to this is to subclass NSScriptCommand and override performDefaultImplementation. I hope this helps someone to get it faster...
现代版本的 Cocoa 可以直接解释脚本定义 (.sdef) 属性列表,因此对于基本 AppleScript 支持,您所需要做的就是根据文档创建 sdef,将其添加到“复制捆绑资源”阶段并声明 AppleScript 支持在你的 Info.plist 中。要访问 NSApp 以外的对象,您需要定义对象说明符,以便每个对象都知道其在脚本世界层次结构中的位置。这使您能够对对象属性进行 kvc 操作,并能够将对象方法用作简单的脚本命令。
Modern versions of Cocoa can directly interpret the scripting definition (.sdef) property list, so all you need to do for basic AppleScript support is to create the sdef per the docs, add it to your "copy bundle resources" phase and declare AppleScript support in your Info.plist. To access objects other than NSApp, you define object specifiers, so each object knows its position in the scripting world's hierarchy. That gets you kvc manipulation of object properties, and the ability to use object methods as simple script commands.
一个简单的例子可以帮助您入门,
将一个脚本(名为对话框)放入文档文件夹中,然后您可以从 Xcode 运行它。
将脚本保留在外部的好处是能够在 Xcode 之外对其进行编辑。
如果您确实开始编辑,我建议添加错误检查,因为 applescript 可能无法编译,
也许可以检查
A simple example to get you started,
place a script (named dialog) into the documents folder then you can run it from Xcode
The nice thing about keeping the script external is the ability to edit it outside of Xcode.
I would recommend adding the error checking if you did start editing as the applescript may not compile
maybe check with