views:

85

answers:

2

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

+6  A: 

There are three main ways an app exposes its function to the outside world.

  1. 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 like x-my-app://some-work, you can just do

    [[NSWorkspace sharedWorkspace] openURL:[NSURL URLWithString:@"x-my-app://some-work"] ];
    

    If 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: .

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

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

  1. 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, at

    /Developer/Applications/Utilites/Accessibility Tools/Accessibility Inspector.app
    

    This is very rudimentary but does the job; if you regularly use UI scripting, consider obtaining UI browser, as Zygmunt suggests.

  2. Finally, if you want to use a non-GUI non-exposed feature of another app, you can inject a code into another app.

Yuji
Thanks for the answer, very well explained! I'll check study this.
G3z
If Yuji has answered your question, then please give Yuji credit by accepting the answer.
Philip Regan
I think I did it now. BTW Scripting Bridge made my day!
G3z
A: 

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.

Zygmunt