gpt4 book ai didi

python - 使用 python 守护进程在 OS X 10.7 上获取事件应用程序

转载 作者:搜寻专家 更新时间:2023-10-30 20:06:42 24 4
gpt4 key购买 nike

我正在尝试在 python 中构建一个守护进程,我想获取当前事件应用程序的名称。

对于守护进程,我正在使用这个漂亮的 code snipped来自 Sander Marechal

当我不将应用程序作为守护进程运行时,以下行在 OS X 10.7 上完美运行,尽管文档说“activeApplication()”在 10.6+ 上已弃用

activeAppName = str(NSWorkspace.sharedWorkspace().activeApplication()['NSApplicationName'])

但是,一旦我将应用程序作为守护进程运行,应用程序就会崩溃。

但是,当我只做时守护进程不会崩溃

workspace = str(NSWorkspace.sharedWorkspace())

返回:

<NSWorkspace: 0x7ffe7cc013c0>

所以我的问题是:

  1. 为什么它只作为守护进程崩溃?
  2. 如何通过 python 获取事件应用程序OS X 10.7(也适用于守护进程 ;-))?

我不明白错误信息,但也许你们中有人理解:

Process:         Python [7920]
Path: /System/Library/Frameworks/Python.framework/Versions/2.7/Resources/Python.app/Contents/MacOS/Python
Identifier: Python
Version: ??? (???)
Code Type: X86-64 (Native)
Parent Process: ??? [1]

Date/Time: 2012-02-29 23:35:25.202 +0100
OS Version: Mac OS X 10.7.3 (11D50b)
Report Version: 9

Interval Since Last Report: 818421 sec
Crashes Since Last Report: 21
Per-App Crashes Since Last Report: 15
Anonymous UUID: 05B412BD-4629-472B-964D-BE4A88B06DD1

Crashed Thread: 0 Dispatch queue: com.apple.main-thread

Exception Type: EXC_BAD_ACCESS (SIGSEGV)
Exception Codes: KERN_INVALID_ADDRESS at 0x0000000000000108

VM Regions Near 0x108:
-->
__TEXT 0000000102e90000-0000000102e91000 [ 4K] r-x/rwx SM=COW /System/Library/Frameworks/Python.framework/Versions/2.7/Resources/Python.app/Contents/MacOS/Python

Application Specific Information:
*** single-threaded process forked ***
objc[7918]: garbage collection is OFF

Thread 0 Crashed:: Dispatch queue: com.apple.main-thread
0 libdispatch.dylib 0x00007fff8ceb7ce9 _dispatch_wakeup + 108
1 libdispatch.dylib 0x00007fff8ceba876 _dispatch_resume_slow + 20
2 com.apple.CoreServices.CarbonCore 0x00007fff8d34f919 _ZL22connectToCoreServicesDv + 269
3 com.apple.CoreServices.CarbonCore 0x00007fff8d34f7d5 _ZL9getStatusv + 24
4 com.apple.CoreServices.CarbonCore 0x00007fff8d34f74f scCreateSystemServiceVersion + 50
5 com.apple.LaunchServices 0x00007fff90b5ace1 _ZL45SetupCoreApplicationServicesCommunicationPortv + 147
6 com.apple.LaunchServices 0x00007fff90b5b37a getProcessDispatchTable() + 19
7 com.apple.LaunchServices 0x00007fff90b56de0 LSClientSideSharedMemory::GetClientSideSharedMemory(LSSessionID, bool) + 158
8 com.apple.LaunchServices 0x00007fff90b6b152 _LSCopyFrontApplication + 42
9 com.apple.AppKit 0x00007fff899adc5d -[NSWorkspace activeApplication] + 26
10 libffi.dylib 0x00007fff91df2e7c ffi_call_unix64 + 76
11 libffi.dylib 0x00007fff91df3ae9 ffi_call + 728
12 _objc.so 0x00000001031c7d60 PyObjCFFI_Caller + 2272
13 _objc.so 0x00000001031dd169 0x1031ae000 + 192873
14 org.python.python 0x0000000102ea0d32 PyObject_Call + 97
15 org.python.python 0x0000000102f20f63 PyEval_EvalFrameEx + 14353
16 org.python.python 0x0000000102f23df7 0x102e99000 + 568823
17 org.python.python 0x0000000102f20e0a PyEval_EvalFrameEx + 14008
18 org.python.python 0x0000000102f23df7 0x102e99000 + 568823
19 org.python.python 0x0000000102f20e0a PyEval_EvalFrameEx + 14008
20 org.python.python 0x0000000102f23cd8 PyEval_EvalCodeEx + 1996
21 org.python.python 0x0000000102f23d4d PyEval_EvalCode + 54
22 org.python.python 0x0000000102f3b08f 0x102e99000 + 663695
23 org.python.python 0x0000000102f3b14f PyRun_FileExFlags + 157
24 org.python.python 0x0000000102f3c2a2 PyRun_SimpleFileExFlags + 392
25 org.python.python 0x0000000102f4c2af Py_Main + 2715
26 org.python.python 0x0000000102e90e88 0x102e90000 + 3720

最佳答案

我对此进行了一些测试,我认为您的问题可能是您将此工具守护进程的方式,然后尝试进行需要可能不可用的 Windows 服务的调用。此链接暗示了这种情况:http://grokbase.com/t/python/pythonmac-sig/08axst378p/appscript-and-launching-apps-from-background-only-python-processes

我首先使用您的守护程序脚本测试了这个理论,并通过 AppleScript 调用 osascript 来查找事件的应用程序:

from subprocess import Popen, PIPE

cmd = """osascript \
-e 'tell application "System Events"' \
-e 'set app_name to name of the first process whose frontmost is true' \
-e 'end tell' """
v = Popen(cmd, shell=True, stdout=PIPE).stdout.read()

Popen 是一种在子进程中启动系统命令并能够检查其返回码或读取其输出(或发送输入)的方法 http://docs.python.org/library/subprocess.html . . Osascript 是一个调用苹果脚本的命令行工具。

对我来说,这是可行的,因为它启动了一个我认为是否可以访问窗口服务器的新子进程?

然后我创建了一个 launchd plist 而不是使用您的守护程序脚本。这有效:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>com.company.test</string>
<key>Nice</key>
<integer>1</integer>
<key>OnDemand</key>
<false/>
<key>Program</key>
<string>/path/to/script.py</string>
</dict>
</plist>

Launchd 是 OSX 守护程序进程管理器,它似乎以完全访问窗口服务器的方式启动程序。

对于 script.py,我只是让它循环,将最前面的应用程序名称写入文件并休眠。

更新

由于您提到过您的 pyobjc 方法已被弃用,而且您似乎喜欢 applescript 方法,我想我会使用 appscript - the python bindings to apple script 来采用 pythonic 方式来实现这一点。

from appscript import app, its
activeApp = app('System Events').processes[its.frontmost == True].first()
print activeApp

#result
app(u'/System/Library/CoreServices/System Events.app').application_processes[u'Terminal']

activeApp 是代表最前端应用程序的对象,由系统事件应用程序报告。

关于python - 使用 python 守护进程在 OS X 10.7 上获取事件应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9507984/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com