python - Write to NSLog and file PythonObjC -
i'm trying modify script additionally print log file. uses nslog(). i'm still learning python... anyways, here's have far:
# cocoa_keypress_monitor.py bjarte johansen licensed under # license: http://ljos.mit-license.org/ appkit import nsapplication, nsapp foundation import nsobject, nslog cocoa import nsevent, nskeydownmask 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) open("/users/zachary/downloads/foo.txt", "a", 0) 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() as can see, attempted pass myfile.write() same data nslog(), python doesn't , don't know how properly.
the argument file.write() not a format string nslog()'s argument, it's single regular string. need coerce nsevent object string, passing python's str() function. (pyobjc knows call objective-c object's -description method, nslog() does, in situation.)
you should note %@ not valid format specifier python format strings: used in objc. actually, preferred format string syntax in python doesn't use percent escapes. if wanted format nsevent python string explicitly, like: "{}".format(event)
Comments
Post a Comment