Tiago
Tiago

Reputation: 1991

Another way to populate ProcessSerialNumber from PID, since GetProcessForPID is deprecated

I have the CGWindowID and the PID (int) of a window I'm simulating mouse clicks on. I can successfully simulate mousedown and mouseup events using this snippet:

NSEvent *customEvent = [NSEvent mouseEventWithType: NSEventTypeLeftMouseUp
                                              location: point
                                         modifierFlags: 0 | NSEventModifierFlagCommand
                                             timestamp:[NSDate timeIntervalSinceReferenceDate]
                                          windowNumber:[self.windowID intValue]
                                               context: nil
                                           eventNumber: 0
                                            clickCount: 1
                                              pressure: 0];

ProcessSerialNumber psn = { 0, kNoProcess };
    OSStatus err = GetProcessForPID(PID, &psn);
    if (err == noErr) {
        CGEvent = [customEvent CGEvent];
        CGEventPostToPSN(&psn, CGEvent);

        CGEvent = [customEvent CGEvent];
        CGEventPostToPSN(&psn, CGEvent);
    }

My concern is that GetProcessForPID is deprecated, so I'm looking for alternatives to populate the ProcessSerialNumber struct so I can use CGEventPostToPSN. ProcessSerialNumber is a struct containing high and low values:

struct ProcessSerialNumber {
  UInt32              highLongOfPSN;
  UInt32              lowLongOfPSN;
};

If there isn't a method for getting this using a PID, how can I find this for another process that's already running? I've tried listening to the NSWorkspaceDidActivateApplicationNotification and clicking on the target window, but the notification doesn't contain the serial number high and low values.

NSWorkSpace launchedApplications also has this information in the result array, but is also deprecated since 10.7. runningApplications from NSWorkspace doesn't contain this information, or am I missing something obvious?

Upvotes: 2

Views: 897

Answers (1)

uliwitness
uliwitness

Reputation: 8773

AIUI the basic idea is that the PSN itself is deprecated, and all API that relates to it. Is there a reason you can't just use CGEventPostToPid?

Upvotes: 3

Related Questions