• Home
  • History
  • Annotate
  • Line#
  • Navigate
  • Raw
  • Download
  • only in /macosx-10.9.5/pyobjc-42/2.5/pyobjc/pyobjc-framework-Cocoa/Examples/AppKit/PyObjCLauncher/
1import objc; objc.setVerbose(1)
2from AppKit import *
3from Foundation import *
4from PyObjCTools import AppHelper
5from MyDocument import *
6from LaunchServices import *
7from PreferencesWindowController import *
8
9PYTHON_EXTENSIONS = [u'py', 'pyw', u'pyc']
10
11FILE_TYPE_BINDING_MESSAGE = u"""
12%s is not the default application for all Python script types.  You should fix this with the Finder's "Get Info" command.
13
14See "Changing the application that opens a file" in Mac Help for details.
15""".strip()
16
17class MyAppDelegate(NSObject):
18    def init(self):
19        self = super(MyAppDelegate, self).init()
20        self.initial_action_done = False
21        self.should_terminate = False
22        return self
23
24    @objc.IBAction
25    def showPreferences_(self, sender):
26        PreferencesWindowController.getPreferencesWindow()
27
28    def applicationDidFinishLaunching_(self, aNotification):
29        self.testFileTypeBinding()
30        if not self.initial_action_done:
31            self.initial_action_done = True
32            self.showPreferences_(self)
33
34    def shouldShowUI(self):
35        if not self.initial_action_done:
36            self.should_terminate = True
37        self.initial_action_done = True
38        if NSApp().currentEvent().modifierFlags() & NSAlternateKeyMask:
39            return True
40        return False
41
42    def shouldTerminate(self):
43        return self.should_terminate
44
45    def applicationShouldOpenUntitledFile_(self, sender):
46        return False
47
48    def testFileTypeBinding(self):
49        if NSUserDefaults.standardUserDefaults().boolForKey_(u'SkipFileBindingTest'):
50            return
51        bndl = NSBundle.mainBundle()
52        myURL = NSURL.fileURLWithPath_(bndl.bundlePath())
53        myName = bndl.infoDictionary()[u'CFBundleName']
54        for ext in PYTHON_EXTENSIONS:
55            err, outRef, outURL = LSGetApplicationForInfo(kLSUnknownType, kLSUnknownCreator, u'txt', kLSRolesViewer, None, None)
56            if (err or myURL != outURL):
57                res = NSRunAlertPanel(
58                    u'File type binding',
59                    FILE_TYPE_BINDING_MESSAGE % myName,
60                    u'OK',
61                    u"Don't show this warning again",
62                    None)
63                if res == 0:
64                    NSUserDefaults.standardUserDefaults().setObject_forKey_(u'YES', u'SkipFileBindingTest')
65                return
66
67if __name__ == '__main__':
68    AppHelper.runEventLoop()
69