Get real path of application from pid?

前端 未结 5 1665
轻奢々
轻奢々 2020-12-08 01:01

How can I get the process details like name of application & real path of application from process id?

I am using Mac OS X.

5条回答
  •  难免孤独
    2020-12-08 01:37

    If the PID is the PID of a "user application", then you can get the NSRunningApplication of the app like that:

    NSRunningApplication * app = [NSRunningApplication  
        runningApplicationWithProcessIdentifier:pid
    ];
    

    And to print the path of the executable:

    NSLog(@"Executable of app: %@", app.executableURL.path);
    

    the app bundle itself is here

    NSLog(@"Executable of app: %@", app.bundleURL.path);
    

    However this won't work with system or background processes, it's limited to user apps (those typically visible in the dock after launch). The NSRunningApplication object allows to to check if the app is ative, to hide/unhide it and do all other kind of neat stuff.

    Just thought I mention it here for completeness. If you want to work with arbitrary processes, then the accepted answer is of course better.

提交回复
热议问题