When interacting with other applications on the Mac, AppleScript is the main option to use. Writing AppleScript to run within an application can be pretty annoying, and there’s no syntax checking unless you write it in AppleScript Editor first and then bring over the code. A good solution is using the ScriptingBridge framework, which will provide you with a class interface to interact directly with a target application. Of course your target application must support Applescript, just as if you were writing the script manually.
To generate a ScriptingBridge header, you need to run the application through some terminal commands. Based on your app name, the command is something along the lines of:
sdef /Applications/Safari.app | sdp -fh --basename Safari
The first instance is the path to the application’s bundle, and then the second is what the class should be referred to as in code (it can be anything).
This will spit out a header into your current working directory.
To use it, import the header and link your application with the ScriptingBridge framework. You will need to know the bundle id of the application you want to interact with.
SafariApplication* app = [SBApplication applicationWithBundleIdentifier:@"com.apple.Safari"]; NSLog(@"%@", [[[app windows] objectAtIndex:0] currentTab].name);
Providing Safari is open, this will get the top window and log the loaded page’s title.