- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我正在尝试在 python 中构建一个守护进程,我想获取当前事件应用程序的名称。
对于守护进程,我正在使用这个漂亮的 code snipped来自 Sander Marechal
当我不将应用程序作为守护进程运行时,以下行在 OS X 10.7 上完美运行,尽管文档说“activeApplication()”在 10.6+ 上已弃用
activeAppName = str(NSWorkspace.sharedWorkspace().activeApplication()['NSApplicationName'])
但是,一旦我将应用程序作为守护进程运行,应用程序就会崩溃。
但是,当我只做时守护进程不会崩溃
workspace = str(NSWorkspace.sharedWorkspace())
返回:
<NSWorkspace: 0x7ffe7cc013c0>
所以我的问题是:
我不明白错误信息,但也许你们中有人理解:
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/
我正在处理一组标记为 160 个组的 173k 点。我想通过合并最接近的(到 9 或 10 个组)来减少组/集群的数量。我搜索过 sklearn 或类似的库,但没有成功。 我猜它只是通过 knn 聚类
我有一个扁平数字列表,这些数字逻辑上以 3 为一组,其中每个三元组是 (number, __ignored, flag[0 or 1]),例如: [7,56,1, 8,0,0, 2,0,0, 6,1,
我正在使用 pipenv 来管理我的包。我想编写一个 python 脚本来调用另一个使用不同虚拟环境(VE)的 python 脚本。 如何运行使用 VE1 的 python 脚本 1 并调用另一个 p
假设我有一个文件 script.py 位于 path = "foo/bar/script.py"。我正在寻找一种在 Python 中通过函数 execute_script() 从我的主要 Python
这听起来像是谜语或笑话,但实际上我还没有找到这个问题的答案。 问题到底是什么? 我想运行 2 个脚本。在第一个脚本中,我调用另一个脚本,但我希望它们继续并行,而不是在两个单独的线程中。主要是我不希望第
我有一个带有 python 2.5.5 的软件。我想发送一个命令,该命令将在 python 2.7.5 中启动一个脚本,然后继续执行该脚本。 我试过用 #!python2.7.5 和http://re
我在 python 命令行(使用 python 2.7)中,并尝试运行 Python 脚本。我的操作系统是 Windows 7。我已将我的目录设置为包含我所有脚本的文件夹,使用: os.chdir("
剧透:部分解决(见最后)。 以下是使用 Python 嵌入的代码示例: #include int main(int argc, char** argv) { Py_SetPythonHome
假设我有以下列表,对应于及时的股票价格: prices = [1, 3, 7, 10, 9, 8, 5, 3, 6, 8, 12, 9, 6, 10, 13, 8, 4, 11] 我想确定以下总体上最
所以我试图在选择某个单选按钮时更改此框架的背景。 我的框架位于一个类中,并且单选按钮的功能位于该类之外。 (这样我就可以在所有其他框架上调用它们。) 问题是每当我选择单选按钮时都会出现以下错误: co
我正在尝试将字符串与 python 中的正则表达式进行比较,如下所示, #!/usr/bin/env python3 import re str1 = "Expecting property name
考虑以下原型(prototype) Boost.Python 模块,该模块从单独的 C++ 头文件中引入类“D”。 /* file: a/b.cpp */ BOOST_PYTHON_MODULE(c)
如何编写一个程序来“识别函数调用的行号?” python 检查模块提供了定位行号的选项,但是, def di(): return inspect.currentframe().f_back.f_l
我已经使用 macports 安装了 Python 2.7,并且由于我的 $PATH 变量,这就是我输入 $ python 时得到的变量。然而,virtualenv 默认使用 Python 2.6,除
我只想问如何加快 python 上的 re.search 速度。 我有一个很长的字符串行,长度为 176861(即带有一些符号的字母数字字符),我使用此函数测试了该行以进行研究: def getExe
list1= [u'%app%%General%%Council%', u'%people%', u'%people%%Regional%%Council%%Mandate%', u'%ppp%%Ge
这个问题在这里已经有了答案: Is it Pythonic to use list comprehensions for just side effects? (7 个答案) 关闭 4 个月前。 告
我想用 Python 将两个列表组合成一个列表,方法如下: a = [1,1,1,2,2,2,3,3,3,3] b= ["Sun", "is", "bright", "June","and" ,"Ju
我正在运行带有最新 Boost 发行版 (1.55.0) 的 Mac OS X 10.8.4 (Darwin 12.4.0)。我正在按照说明 here构建包含在我的发行版中的教程 Boost-Pyth
学习 Python,我正在尝试制作一个没有任何第 3 方库的网络抓取工具,这样过程对我来说并没有简化,而且我知道我在做什么。我浏览了一些在线资源,但所有这些都让我对某些事情感到困惑。 html 看起来
我是一名优秀的程序员,十分优秀!