• Home
  • History
  • Annotate
  • Line#
  • Navigate
  • Raw
  • Download
  • only in /macosx-10.9.5/pyobjc-42/2.5/pyobjc/pyobjc-framework-Cocoa/Examples/AppKit/TinyURLService/
1from Foundation import *
2from AppKit import *
3from PyObjCTools import AppHelper
4import objc
5import traceback
6import urllib
7import urllib2
8
9def serviceSelector(fn):
10    # this is the signature of service selectors
11    return objc.selector(fn, signature="v@:@@o^@")
12
13def ERROR(s):
14    #NSLog(u"ERROR: %s" % (s,))
15    return s
16
17NAME = 'TinyURLService-0.0'
18TINYURL_API = 'http://tinyurl.com/api-create.php'
19def getTinyURL(url):
20    data = urllib.urlencode(dict(url=url, source=NAME))
21    return urllib2.urlopen(TINYURL_API, data).read().decode('utf-8')
22
23class TinyURLService(NSObject):
24
25    @serviceSelector
26    def doTinyURLService_userData_error_(self, pboard, data, error):
27        # Mail.app in 10.4.1 doesn't do NSURLPboardType correctly!
28        # Probably elsewhere too, so we just use strings.
29        try:
30            #NSLog(u'doTinyURLService: %r' % (pboard,))
31            types = pboard.types()
32            url = None
33
34
35            if NSStringPboardType in types:
36                #NSLog(u'getting NSStringPboardType')
37                urlString = pboard.stringForType_(NSStringPboardType)
38                #NSLog(u'NSStringPboardType: %r' % (urlString,))
39                url = NSURL.URLWithString_(urlString.strip())
40                if url is None:
41                    #NSLog(u'urlString was %r' % (urlString,))
42                    return ERROR(NSLocalizedString(
43                        "Error: Given URL was not well-formed.",
44                        "Given URL not well-formed."
45                    ))
46
47            if url is None:
48                return ERROR(NSLocalizedString(
49                        "Error: Pasteboard doesn't contain a valid URL.",
50                        "Pasteboard doesn't contain a valid URL.",
51                    ))
52
53
54            urlString = url.absoluteString()
55            #NSLog(u'urlString = %r' % (urlString,))
56
57            res = getTinyURL(urlString.UTF8String())
58
59            #NSLog(u'res = %r' % (res,))
60            resURL = NSURL.URLWithString_(res)
61            #NSLog(u'resURL = %r' % (resURL,))
62            if resURL is None:
63                NSLog(u'res was %r' % (res,))
64                return ERROR(NSLocalizedString(
65                    "Error: Resultant URL was not well-formed.",
66                    "Resultant URL not well-formed."
67                ))
68            pboard.declareTypes_owner_([NSStringPboardType], None)
69            pboard.setString_forType_(resURL.absoluteString(), NSStringPboardType)
70            return ERROR(None)
71        except:
72            traceback.print_exc()
73            return ERROR(u'Exception, see traceback')
74
75
76def main():
77    serviceProvider = TinyURLService.alloc().init()
78    NSRegisterServicesProvider(serviceProvider, u'TinyURLService')
79    AppHelper.runConsoleEventLoop()
80
81if __name__ == '__main__':
82    main()
83