如何从应用程序B访问应用程序A的功能
我想知道一个应用程序是否以及以多少方式可以访问另一个应用程序的特定功能。 例如,
- 在 safari/firefox/chrome 中打开一个网址
- 在当前浏览器选项卡中运行 JavaScript
- 播放/暂停 itunes
- 在 Finder 中重命名选定的文件
我知道 applescript 的存在,但我想知道这是否是我必须与之交互的唯一方式这些应用程序和其他应用程序
谢谢
I was wondering if and in how many way an app can access specific funcions of another app.
for example
- open an url in safari/firefox/chrome
- run a javascript in current browser-tab
- play/pause itunes
- rename selected files in Finder
I am aware of the existence of applescript but i was wondering if that's the only way i have to interact with those apps and others
thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
应用程序向外界公开其功能的方式主要有三种。
其中之一是支持 URL 协议。要打开 URL,只需使用
NSWorkspace
。方法有很多种;如果应用程序注册了特定协议,例如x-my-app://some-work
,您可以执行如果您想打开一个 URL,其协议(例如 http)受许多应用程序支持,并且您想指定要使用的应用程序,请使用 openURLs:withAppBundleIdentifier:options:additionalEventParamDescriptor:launchIdentifiers:
.
另一个是系统服务。这样,应用程序可以在服务菜单和其他应用程序的上下文菜单中添加条目;您也可以以编程方式调用它。
否则,将通过 Apple 活动进行。 Applescript 是处理它们的一种方法,但不是唯一的一种。它只是一种发布Apple事件的语言。 Cocoa 处理 Apple 事件的方法有很多,请参见 Apple 的这份详细文档。
基本上,应用程序可以通过
以面向对象的方式导出其内部(这不仅仅是其 Objective-C 层次结构;您可以控制公开的内部对象和方法的数量等) sdef 文件。然后,另一个应用程序可以通过 Apple 事件使用这个面向对象的系统。
要发送和接收 Apple 事件,您当然可以手动构造它们,但您可以使用更高级别的对象,例如
要了解应用程序公开的方面,只需打开 AppleScript 编辑器并选择菜单“文件”→“打开词典”,然后选择一个应用程序。
现在,使用应用程序未通过任何这些方法公开的功能相当困难。您还有一些解决方法。
UI 脚本。这是通过将 Apple 事件发送到名为 System Events 的无头应用程序来完成的,该应用程序是 OS X 中的核心程序之一。通过这种方式,您可以以编程方式模拟单击按钮、选择菜单等。另一个应用程序。因此,几乎任何您可以通过另一个应用程序使用 GUI 执行的操作都可以通过另一个应用程序以编程方式完成。要查看可通过 UI 脚本访问的 UI 对象的层次结构,请使用 XCode 工具附带的实用程序,位于
这是非常基本的,但可以完成工作;如果您经常使用 UI 脚本,请考虑获取 UI 浏览器,正如 Zygmunt 建议的那样。
最后,如果你想使用另一个应用程序的非GUI非公开功能,你可以将代码注入到另一个应用程序中。
There are three main ways an app exposes its function to the outside world.
One is by supporting an URL protocol. To open an URL, just use
NSWorkspace
. There are many methods; if an app registers a specific protocol likex-my-app://some-work
, you can just doIf you want to open an URL whose protocol (say http) is supported by many apps and if you want to specify which app to use, use openURLs:withAppBundleIdentifier:options:additionalEventParamDescriptor:launchIdentifiers:
.
Another is the System Services. With this, an app can add entries in the Service menu and in the context menu of other apps; you can also call it programmatically.
Otherwise, it's via Apple events. Applescript is one way to deal with them, but not the only one. It's just a language to issue Apple events. There are many ways to deal with Apple events from Cocoa, see this detailed document by Apple.
Basically, an app can export its internal as an object-oriented manner (which is not just its Objective-C hierarchy; you can control how much of its internal objects and methods you expose, etc.) by an
sdef
file. Then, another app can use this object-oriented system via Apple events.To send and receive Apple events, you can of course construct them by hand, but you can use higher-level objects like
To learn what kind of aspects an app exposes, just open the AppleScript Editor and choose the menu File → Open Dictionary, and choose an app.
Now, it's rather hard to use features of an app which the app does not expose via any of these methods. You still have a few workaround.
UI Scripting. This is done by sending Apple Events to a headless app called
System Events
which is one of the core program in OS X. This way, you can programmatically emulate clicking a button, choosing a menu, etc. of another app. So, almost whatever you can do using GUI with another app can be done programmatically from another app. To see the hierarchy of UI objects accessible from UI scripting, use a utility which comes with XCode tools, atThis is very rudimentary but does the job; if you regularly use UI scripting, consider obtaining UI browser, as Zygmunt suggests.
Finally, if you want to use a non-GUI non-exposed feature of another app, you can inject a code into another app.
只是扩展 Yuji 的答案。如果您被迫走 UI 脚本路径,有一个很好的应用程序可以分析界面 - hxxp://pfiddlesoft.com/uibrowser/。但是,您提到的示例应该公开一些 API。
我还可能建议使用 Sikuli hxxp://groups.csail.mit.edu/uid/sikuli/ 作为 IDE,以稳健地围绕用户界面编写脚本。
对于某些通常来自 GNU/Linux 的应用程序,可以使用 D-BUS hxxp://en.wikipedia.org/wiki/D-Bus - 尽管我还没有在自己的 Mac 上使用过它。我还要引用维基百科关于 Cocoa 的内容:“它是可用于 Mac OS X 的五个主要 API 之一;其他的是 Carbon、POSIX(用于 BSD 环境)、X11 和 Java。” hxxp://en.wikipedia.org/wiki/Cocoa_%28API%29 这只是进一步探索的一个松散提示,因为 Yuji 已经解释了对你的问题至关重要的苹果事件。
Just expanding on Yuji's answer. If you were forced to go the UI scripting path, there's a nice application to analyze the interface - hxxp://pfiddlesoft.com/uibrowser/. However, the examples you mentioned should expose some APIs.
I might also recommend using Sikuli hxxp://groups.csail.mit.edu/uid/sikuli/ as an IDE to script around user interface robustly.
For some applications usually coming from GNU/Linux there is D-BUS hxxp://en.wikipedia.org/wiki/D-Bus - although I haven't used it on a Mac on my own yet. And let me also quote Wikipedia about Cocoa "It is one of five major APIs available for Mac OS X; the others are Carbon, POSIX (for the BSD environment), X11 and Java." hxxp://en.wikipedia.org/wiki/Cocoa_%28API%29 That's just a loose tip for further exploration as Yuji has already explained Apple events that are key to your question.