似乎如果我想创建一个带有停靠图标等的非常基本的Cocoa应用程序,我将不得不使用 Xcode和GUI构建器(带有PyObjC).
我打算编写的应用程序主要关注算法和基本IO - 因此,并不主要与Apple特定的东西相关.
基本上应用程序应定期运行(例如,每3分钟一次)..通过AppleScript提取一些信息并将HTML文件写入特定目录.我想为这个应用程序添加一个Dock图标..主要是为了显示进程的"状态"(例如,如果有错误......停靠图标上会有一个红色标记).dock图标的另一个优点是我可以让它在启动时运行.
以简单的方式定义dock右键菜单的额外奖励(例如:使用Python的callables列表).
我可以在不使用Xcode或GUI构建器但只使用Emacs和Python的情况下实现这一目标吗?
使用 Mac OS X API,我试图保存一个应用了 Quartz 过滤器的 PDF 文件,就像可以从预览应用程序的“另存为”对话框中一样。到目前为止,我已经编写了以下代码(使用 Python 和 pyObjC,但这对我来说并不重要):
-- filter-pdf.py: 开始
from Foundation import *
from Quartz import *
import objc
page_rect = CGRectMake (0, 0, 612, 792)
fdict = NSDictionary.dictionaryWithContentsOfFile_("/System/Library/Filters/Blue
\ Tone.qfilter")
in_pdf = CGPDFDocumentCreateWithProvider(CGDataProviderCreateWithFilename ("test
.pdf"))
url = CFURLCreateWithFileSystemPath(None, "test_out.pdf", kCFURLPOSIXPathStyle,
False)
c = CGPDFContextCreateWithURL(url, page_rect, fdict)
np = CGPDFDocumentGetNumberOfPages(in_pdf)
for ip in range (1, np+1):
page = CGPDFDocumentGetPage(in_pdf, ip)
r = CGPDFPageGetBoxRect(page, kCGPDFMediaBox)
CGContextBeginPage(c, r)
CGContextDrawPDFPage(c, page)
CGContextEndPage(c)
Run Code Online (Sandbox Code Playgroud)
-- filter-pdf.py: 结束
不幸的是,没有应用过滤器“蓝色色调”,输出 PDF …
我写了一个AppleScript安装SparseBundle图像,我希望它在Time Machine启动时完全执行.
现在,我定期检查Time Machine是否使用AppleScriptusing on idle语句运行:
on idle
....
return <interval>
end idle
Run Code Online (Sandbox Code Playgroud)
这不是一种强有力的方式.在我看来,为Application Launch事件添加事件触发器将是一种更好的方法.
能否请你帮忙?
一个Objective-C或Python示例代码(我更喜欢Python)非常受欢迎.
我有一个用PyObjC编写的statusItem应用程序.statusItem有一个menuItem,它应该在单击时启动一个新窗口:
# Create statusItem
statusItem = NSStatusBar.systemStatusBar().statusItemWithLength_(NSVariableStatusItemLength)
statusItem.setHighlightMode_(TRUE)
statusItem.setEnabled_(TRUE)
statusItem.retain()
# Create menuItem
menu = NSMenu.alloc().init()
menuitem = NSMenuItem.alloc().initWithTitle_action_keyEquivalent_('Preferences', 'launchPreferences:', '')
menu.addItem_(menuitem)
statusItem.setMenu_(menu)
Run Code Online (Sandbox Code Playgroud)
launchPreferences:方法是:
def launchPreferences_(self, notification):
preferences = Preferences.alloc().initWithWindowNibName_('Preferences')
preferences.showWindow_(self)
Run Code Online (Sandbox Code Playgroud)
Preferences是NSWindowController类:
class Preferences(NSWindowController):
Run Code Online (Sandbox Code Playgroud)
当我在XCode(Build&Go)中运行应用程序时,这很好用.但是,当我从XCode外部运行构建的.app文件时,statusItem和menuItem按预期显示,但是当我单击Preferences menuItem时,窗口不会出现.我已通过检查控制台输出验证了launchPreferences代码是否正在运行.
此外,如果我再次双击.app文件,则会出现窗口但如果我通过单击(例如,在Finder窗口上)更改活动窗口,则首选项窗口将消失.在我看来,这与活动窗口有关.
更新1 我尝试了这 两个答案,但都没有工作.如果我添加到launchPreferences方法:
preferences.makeKeyAndOrderFront_()
Run Code Online (Sandbox Code Playgroud)
要么
preferences.setLevel_(NSNormalWindowLevel)
Run Code Online (Sandbox Code Playgroud)
那我只是得到一个错误:
'Preferences'对象没有属性