pyobjc / pyobjc-framework-Cocoa / Examples / AppKit / TinyURLService /

The branch 'pyobjc-2.3.x' does not exist.
from Foundation import *
from AppKit import *
from PyObjCTools import AppHelper
import objc
import traceback
import urllib
import urllib2

def serviceSelector(fn):
    # this is the signature of service selectors
    return objc.selector(fn, signature="v@:@@o^@")

def ERROR(s):
    #NSLog(u"ERROR: %s", s)
    return s

NAME = 'TinyURLService-0.0'
def getTinyURL(url):
    data = urllib.urlencode(dict(url=url, source=NAME))
    return urllib2.urlopen(TINYURL_API, data).read().decode('utf-8')

class TinyURLService(NSObject):

    def doTinyURLService_userData_error_(self, pboard, data, error):
        # in 10.4.1 doesn't do NSURLPboardType correctly!
        # Probably elsewhere too, so we just use strings.
            #NSLog(u'doTinyURLService: %s', pboard)
            types = pboard.types()
            url = None

            if NSStringPboardType in types:
                #NSLog(u'getting NSStringPboardType')
                urlString = pboard.stringForType_(NSStringPboardType)
                #NSLog(u'NSStringPboardType: %s', urlString)
                url = NSURL.URLWithString_(urlString.strip())
                if url is None:
                    #NSLog(u'urlString was %s', urlString)
                    return ERROR(NSLocalizedString(
                        "Error: Given URL was not well-formed.",
                        "Given URL not well-formed."

            if url is None:
                return ERROR(NSLocalizedString(
                        "Error: Pasteboard doesn't contain a valid URL.",
                        "Pasteboard doesn't contain a valid URL.",

            urlString = url.absoluteString()
            #NSLog(u'urlString = %s', urlString)
            res = getTinyURL(urlString.UTF8String())

            #NSLog(u'res = %r' % (res,))
            resURL = NSURL.URLWithString_(res)
            #NSLog(u'resURL = %s', resURL)
            if resURL is None:
                NSLog(u'res was %s', res)
                return ERROR(NSLocalizedString(
                    "Error: Resultant URL was not well-formed.",
                    "Resultant URL not well-formed."
            pboard.declareTypes_owner_([NSStringPboardType], None)
            pboard.setString_forType_(resURL.absoluteString(), NSStringPboardType)
            return ERROR(None)
            return ERROR(u'Exception, see traceback')

def main():
    serviceProvider = TinyURLService.alloc().init()
    NSRegisterServicesProvider(serviceProvider, u'TinyURLService')

if __name__ == '__main__':
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.