Programmatically check if a process is running on Mac

Here are some specific implementations and details, note that proc->kp_proc.p_comm has a character length limit that’s why I’m implemented infoForPID: instead Cocoa : [NSWorkspace launchedApplications] (10.2+ , deprecated in 10.7, very limited process listing) [NSWorkspace runningApplications] (10.6+ , less limited process listing but still not including daemon processes) Carbon : – (NSArray*)getCarbonProcessList { NSMutableArray *ret … Read more

Simulating key press events in Mac OS X

Here’s code to simulate a Cmd–S action: CGKeyCode inputKeyCode = kVK_ANSI_S; CGEventSourceRef source = CGEventSourceCreate(kCGEventSourceStateCombinedSessionState); CGEventRef saveCommandDown = CGEventCreateKeyboardEvent(source, inputKeyCode, YES); CGEventSetFlags(saveCommandDown, kCGEventFlagMaskCommand); CGEventRef saveCommandUp = CGEventCreateKeyboardEvent(source, inputKeyCode, NO); CGEventPost(kCGAnnotatedSessionEventTap, saveCommandDown); CGEventPost(kCGAnnotatedSessionEventTap, saveCommandUp); CFRelease(saveCommandUp); CFRelease(saveCommandDown); CFRelease(source); A CGKeyCode is nothing more than an unsigned integer: typedef uint16_t CGKeyCode; //From CGRemoteOperation.h Your real issue will be … Read more

How do I determine the OS version at runtime in OS X or iOS (without using Gestalt)?

On OS X 10.10 (and iOS 8.0), you can use [[NSProcessInfo processInfo] operatingSystemVersion] which returns a NSOperatingSystemVersion struct, defined as typedef struct { NSInteger majorVersion; NSInteger minorVersion; NSInteger patchVersion; } NSOperatingSystemVersion; There is also a method in NSProcessInfo that will do the comparison for you: – (BOOL)isOperatingSystemAtLeastVersion:(NSOperatingSystemVersion)version Beware, although documented to be available in OS … Read more