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

2024-04-29 01:40:16 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图使用CGEventPostToPSN发布事件,但在获取正确的进程序列号时遇到问题。我找到了我所关心的via流程NSWorkspace.sharedWorkspace().launchedApplications(),但我不知道作为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)

有人有主意吗?在


Tags: fromimportgetmyprocesscoregraphicsquartzpsn