(Python 2.7 Mac Yosemite 10.10.2)除非我以管理员身份运行,否则带有Foundations导入的Python脚本将无法运行?

时间:2015-02-24 14:47:35

标签: python macos notifications admin pyobjc

背景:我正试图在Yosemite上用Python创建原生外观的应用程序。问题是我不喜欢,如果我不能以管理员身份运行程序我无法发送通知...我想知道有没有办法覆盖这个或任何东西......因为我得到一个讨厌的错误..不要以为这应该发生。

除非我以管理员身份运行,否则会收到此错误:

    Richy:Desktop Richy$ python2.7 a.py 
    Traceback (most recent call last):
      File "a.py", line 1, in <module>
        import Foundation
      File "/Library/Python/2.7/site-packages/Foundation/__init__.py", line 8, in <module>
        import objc
      File "/Library/Python/2.7/site-packages/objc/__init__.py", line 32, in <module>
        from objc._bridgesupport import *
      File "/Library/Python/2.7/site-packages/objc/_bridgesupport.py", line 13, in <module>
        import pkg_resources
      File "/Library/Python/2.7/site-packages/distribute-0.6.49-py2.7.egg/pkg_resources.py", line 2897, in <module>
        add_activation_listener(lambda dist: dist.activate())
      File "/Library/Python/2.7/site-packages/distribute-0.6.49-py2.7.egg/pkg_resources.py", line 712, in subscribe
        callback(dist)
      File "/Library/Python/2.7/site-packages/distribute-0.6.49-py2.7.egg/pkg_resources.py", line 2897, in <lambda>
        add_activation_listener(lambda dist: dist.activate())
      File "/Library/Python/2.7/site-packages/distribute-0.6.49-py2.7.egg/pkg_resources.py", line 2329, in activate
        self.insert_on(path)
      File "/Library/Python/2.7/site-packages/distribute-0.6.49-py2.7.egg/pkg_resources.py", line 2436, in insert_on
        self.check_version_conflict()
      File "/Library/Python/2.7/site-packages/distribute-0.6.49-py2.7.egg/pkg_resources.py", line 2475, in check_version_conflict
        for modname in self._get_metadata('top_level.txt'):
      File "/Library/Python/2.7/site-packages/distribute-0.6.49-py2.7.egg/pkg_resources.py", line 2323, in _get_metadata
        for line in self.get_metadata_lines(name):
      File "/Library/Python/2.7/site-packages/distribute-0.6.49-py2.7.egg/pkg_resources.py", line 1246, in get_metadata_lines
        return yield_lines(self.get_metadata(name))
      File "/Library/Python/2.7/site-packages/distribute-0.6.49-py2.7.egg/pkg_resources.py", line 1238, in get_metadata
        return self._get(self._fn(self.egg_info,name))
      File "/Library/Python/2.7/site-packages/distribute-0.6.49-py2.7.egg/pkg_resources.py", line 1353, in _get
        stream = open(path, 'rb')
    IOError: [Errno 13] Permission denied: '/Library/Python/2.7/site-packages/oauthlib-0.7.2-py2.7.egg/EGG-INFO/top_level.txt'

如果我以管理员身份运行,这是我的脚本:

    import Foundation
    import objc
    import AppKit
    import sys

    NSUserNotification = objc.lookUpClass('NSUserNotification')
    NSUserNotificationCenter = objc.lookUpClass('NSUserNotificationCenter')

    def notify(title, subtitle, info_text, delay=0, sound=False, userInfo={}):
        notification = NSUserNotification.alloc().init()
        notification.setTitle_(title)
        notification.setSubtitle_(subtitle)
        notification.setInformativeText_(info_text)
        notification.setUserInfo_(userInfo)
        if sound:
            notification.setSoundName_("NSUserNotificationDefaultSoundName")
        notification.setDeliveryDate_(Foundation.NSDate.dateWithTimeInterval_sinceDate_(delay, Foundation.NSDate.date()))
        NSUserNotificationCenter.defaultUserNotificationCenter().scheduleNotification_(notification)


    notify("Test message", "Subtitle", "This message should appear instantly, with a sound", sound=False)
    sys.stdout.write("Notification sent...\n")

0 个答案:

没有答案