Skip to content

Instantly share code, notes, and snippets.

@NSEcho
Created March 4, 2023 18:07
Show Gist options
  • Save NSEcho/53712229f47657b0eec3d01c61fdd3c3 to your computer and use it in GitHub Desktop.
Save NSEcho/53712229f47657b0eec3d01c61fdd3c3 to your computer and use it in GitHub Desktop.
Get PID for bundle identifier
// gcc pid_for_bundle.m -o pid_for_bundle -framework Foundation -framework AppKit
#import <Foundation/Foundation.h>
#import <AppKit/AppKit.h>
int main(int argc, const char **argv) {
if (argc != 2) {
printf("missing bundle identifier\n");
printf("usage: %s com.example.name\n", argv[0]);
exit(-1);
}
const char *identifier = argv[1];
NSString *appIdentifier = [[NSString alloc] initWithBytes:identifier length:strlen(identifier) encoding:NSASCIIStringEncoding];
NSArray<NSRunningApplication *> *runningApps = [NSRunningApplication runningApplicationsWithBundleIdentifier:appIdentifier];
if (runningApps == nil || [runningApps count] == 0) {
printf("no running apps identified with %s identifier\n", identifier);
exit(-1);
}
NSRunningApplication *app = runningApps[0];
pid_t pid = [app processIdentifier];
printf("pid for %s is %d\n", identifier, pid);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment