0
我試圖修改一個腳本來額外打印到日誌文件。它已經使用NSLog()
。我肯定還在學習Python的...不管怎麼說,這是我到目前爲止有:寫入NSLog和文件PythonObjC
# cocoa_keypress_monitor.py by Bjarte Johansen is licensed under a
# License: http://ljos.mit-license.org/
from AppKit import NSApplication, NSApp
from Foundation import NSObject, NSLog
from Cocoa import NSEvent, NSKeyDownMask
from PyObjCTools import AppHelper
import sys
class AppDelegate(NSObject):
def applicationDidFinishLaunching_(self, notification):
mask = NSKeyDownMask
NSEvent.addGlobalMonitorForEventsMatchingMask_handler_(mask, handler)
def handler(event):
try:
NSLog(u"%@", event)
with open("/Users/Zachary/Downloads/foo.txt", "a", 0) as myfile:
myfile.write(u"%@", event)
except KeyboardInterrupt:
AppHelper.stopEventLoop()
def main():
app = NSApplication.sharedApplication()
delegate = AppDelegate.alloc().init()
NSApp().setDelegate_(delegate)
AppHelper.runEventLoop()
if __name__ == '__main__':
main()
正如你所看到的,我試圖通過myfile.write()
相同的數據NSLog()
,但是Python不喜歡這一點,我不知道如何正確地做。