{"id":116,"date":"2008-08-22T19:22:33","date_gmt":"2008-08-23T00:22:33","guid":{"rendered":"http:\/\/vgable.com\/blog\/2008\/08\/22\/is-an-application-running\/"},"modified":"2009-01-13T19:51:40","modified_gmt":"2009-01-14T00:51:40","slug":"is-an-application-running","status":"publish","type":"post","link":"https:\/\/vgable.com\/blog\/2008\/08\/22\/is-an-application-running\/","title":{"rendered":"Is an Application Running?"},"content":{"rendered":"<p>UPDATED 2009-01-13 to correctly detect <a href=\"http:\/\/vgable.com\/blog\/2009\/01\/13\/running-in-the-background\/\">background applications<\/a>.<\/p>\n<p>Pass in the <a href=\"http:\/\/developer.apple.com\/documentation\/CoreFoundation\/Conceptual\/CFBundles\/Tasks\/locating.html#\/\/apple_ref\/doc\/uid\/20001123-123081\">bundle-identfier<\/a> of an application, and this will tell you if it&#8217;s currently running or not:<br \/>\n<code>- (BOOL) ThereIsARunningApplicationWithBundleID:(NSString*)bundleID; {<br \/>\n&nbsp;&nbsp;&nbsp;ProcessSerialNumber\tPSN = {kNoProcess};<br \/>\n&nbsp;&nbsp;&nbsp;while(GetNextProcess(&PSN) == noErr) {<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;CFDictionaryRef info = ProcessInformationCopyDictionary(&PSN,kProcessDictionaryIncludeAllInformationMask);<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if(info) {<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;NSString *theirBundleID = (NSString*)CFDictionaryGetValue(info, kIOBundleIdentifierKey);<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;BOOL bundleIDMatches = [bundleID isEqualTo:(NSString*)theirBundleID];<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;CFRelease(info);<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if(bundleIDMatches)<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return YES;<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br \/>\n&nbsp;&nbsp;&nbsp;}<br \/>\n&nbsp;&nbsp;&nbsp;return NO;<br \/>\n}<\/code><\/p>\n<p>(It&#8217;s a good idea to test if an application is running before sending it an AppleEvent, because that will launch it.)<\/p>\n<p>Interestingly, none of the the <a href=\"http:\/\/developer.apple.com\/documentation\/Carbon\/Reference\/Process_Manager\/Reference\/reference.html\">Process Manager<\/a> APIs can see into <a href=\"http:\/\/developer.apple.com\/documentation\/MacOSX\/Conceptual\/BPMultipleUsers\/BPMultipleUsers.html\">another user&#8217;s process-space<\/a>; but <a href=\"http:\/\/developer.apple.com\/documentation\/Darwin\/Reference\/ManPages\/man1\/ps.1.html\"><code>ps<\/code><\/a> can; at least on OS X 10.5 and 10.4.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>UPDATED 2009-01-13 to correctly detect background applications. Pass in the bundle-identfier of an application, and this will tell you if it&#8217;s currently running or not: &#8211; (BOOL) ThereIsARunningApplicationWithBundleID:(NSString*)bundleID; { &nbsp;&nbsp;&nbsp;ProcessSerialNumber PSN = {kNoProcess}; &nbsp;&nbsp;&nbsp;while(GetNextProcess(&#038;PSN) == noErr) { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;CFDictionaryRef info = ProcessInformationCopyDictionary(&#038;PSN,kProcessDictionaryIncludeAllInformationMask); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if(info) { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;NSString *theirBundleID = (NSString*)CFDictionaryGetValue(info, kIOBundleIdentifierKey); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;BOOL bundleIDMatches = [bundleID isEqualTo:(NSString*)theirBundleID]; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;CFRelease(info); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if(bundleIDMatches) [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6,3,5,4,13,9],"tags":[303,299],"class_list":["post-116","post","type-post","status-publish","format-standard","hentry","category-cocoa","category-macosx","category-objective-c","category-programming","category-sample-code","category-unix","tag-carbon","tag-process-manager"],"_links":{"self":[{"href":"https:\/\/vgable.com\/blog\/wp-json\/wp\/v2\/posts\/116","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/vgable.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/vgable.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/vgable.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/vgable.com\/blog\/wp-json\/wp\/v2\/comments?post=116"}],"version-history":[{"count":0,"href":"https:\/\/vgable.com\/blog\/wp-json\/wp\/v2\/posts\/116\/revisions"}],"wp:attachment":[{"href":"https:\/\/vgable.com\/blog\/wp-json\/wp\/v2\/media?parent=116"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/vgable.com\/blog\/wp-json\/wp\/v2\/categories?post=116"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/vgable.com\/blog\/wp-json\/wp\/v2\/tags?post=116"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}