Question

I'd like to get properties of the currently active app. I understand that this should be possible with ScriptingBridge, however, this seems to require you generate an sdef file and import this in your project for the app you are trying to target. Since I want to target all apps, is there another way to do this?

Example of accessing system preferences:

    SystemPreferencesApplication *systemPreferences =
[SBApplication
 applicationWithBundleIdentifier:@"com.apple.systempreferences"];

If there's another way to access properties of any active app, please do share. (For example; window title)

Thanks.

Was it helpful?

Solution

I assume you want to run an applescript. The scripting bridge is good if you have a lot of applescript code to run. However if you only have a small amount then a simpler way is with NSApplescript.

For example if you wanted to run this applescript...

tell application "System Events"
    set theProcesses to processes
    repeat with aProcess in theProcesses
        tell aProcess to get properties
    end repeat
end tell

Then you can write it this way...

NSString* cmd = @"tell application \"System Events\"\nset theProcesses to processes\nrepeat with aProcess in theProcesses\ntell aProcess to get properties\nend repeat\nend tell";
NSAppleScript* theScript = [[NSAppleScript alloc] initWithSource:cmd];
NSDictionary* errorDict = nil;
NSAppleEventDescriptor* result = [theScript executeAndReturnError:&errorDict];
[theScript release];
if (errorDict) {
    NSLog(@"Error:%@ %@", [errorDict valueForKey:@"NSAppleScriptErrorNumber"], [errorDict valueForKey:@"NSAppleScriptErrorMessage"]);
    return;
}

// do something with result
NSLog(@"result: %@", result);

OTHER TIPS

You can get a list of every currently running Application with

NSWorkSpace.sharedWorkspace.runningApplications;

Each object in that array should be an NSRunningApplication, which you can query and manipulate freely.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top