Created
March 4, 2023 18:07
-
-
Save NSEcho/53712229f47657b0eec3d01c61fdd3c3 to your computer and use it in GitHub Desktop.
Get PID for bundle identifier
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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