好的,我找到了方法。这是 Python 中的代码(取自此处)。它可以使用一些清理和一些错误处理,但它可以工作。关键是来自 ApplicationServices 的未记录的 CPSSetProcessName。
def _osx_set_process_name(app_title):
""" Change OSX application title """
from ctypes import cdll, c_int, pointer, Structure
from ctypes.util import find_library
app_services = cdll.LoadLibrary(find_library("ApplicationServices"))
if app_services.CGMainDisplayID() == 0:
print "cannot run without OS X window manager"
else:
class ProcessSerialNumber(Structure):
_fields_ = [("highLongOfPSN", c_int),
("lowLongOfPSN", c_int)]
psn = ProcessSerialNumber()
psn_p = pointer(psn)
if ( (app_services.GetCurrentProcess(psn_p) < 0) or
(app_services.SetFrontProcess(psn_p) < 0) ):
print "cannot run without OS X gui process"
print "setting process name"
app_services.CPSSetProcessName(psn_p, app_title)
return False
如果您立即调用它,它会更改活动监视器中的进程名称。由于某种原因,我不得不在小超时后调用它来更改菜单栏名称:
import gobject
gobject.timeout_add(100, _osx_set_process_name, "MyTitle")
请注意,如果您将它与 .app 包结合使用,您可以获得非常好的 Python 应用程序系统集成。虽然您可以更改图标、Finder 显示名称...,但我尝试的所有方法都将 CFBundleName(菜单栏中的那个)保留为“Python”。(例如,此答案仅更改了长名称(显示在停靠图标上)。还有很多其他的几乎可以工作。)
While this answer gives the intended result, it's not very elegant. I'd appreciate some insight into why I need the timeout... I think Python or pygtk changes the process name itself when I first import gtk or run the main loop.