如何使用Python获取OSX进程的ProcessSerialNumber?

时间:2014-06-04 03:29:30

标签: python macos

我尝试使用CGEventPostToPSN发布活动,但我无法获得正确的流程序列号。我通过NSWorkspace.sharedWorkspace()。launchApplications()找到了我关心的过程,但是我不知道将什么作为processSerialNumber参数传递给CGEventPostToPSN。

我看到字段NSApplicationProcessSerialNumberHigh和NSApplicationProcessSerialNumberLow,但我不知道如何处理它们。我尝试将它们作为(NSApplicationProcessSerialNumberHigh,NSApplicationProcessSerialNumberLow)的元组传递,但这似乎没有任何效果。

代码看起来像这样:

from AppKit import NSWorkspace
from Quartz.CoreGraphics import CGEventPostToPSN
from Quartz.CoreGraphics import kCGMouseButtonLeft
from Quartz.CoreGraphics import CGEventCreateMouseEvent

my_process = filter(lambda x: x, [x if x.get('NSApplicationName') == 'My Process' else None for x in  NSWorkspace.sharedWorkspace().launchedApplications()])[0]
high_psn = my_process.get('NSApplicationProcessSerialNumberHigh')
low_psn = my_process.get('NSApplicationProcessSerialNumberLow')
my_psn = (high_psn, low_psn)
theEvent = CGEventCreateMouseEvent(None, type, (posx, posy), kCGMouseButtonLeft)
CGEventPostToPSN(my_psn, theEvent)

有人有想法吗?

0 个答案:

没有答案