Commits

Bob Ippolito  committed 522badc

New TinyURLService

  • Participants
  • Parent commits e97ddb9
  • Branches pyobjc-ancient

Comments (0)

Files changed (4)

File Examples/AppKit/TinyURLService/README.rtf

+{\rtf1\mac\ansicpg10000\cocoartf824\cocoasubrtf100
+{\fonttbl\f0\fswiss\fcharset77 Helvetica-Bold;\f1\fswiss\fcharset77 Helvetica;}
+{\colortbl;\red255\green255\blue255;}
+\vieww12000\viewh15440\viewkind0
+\deftab720
+\pard\tx960\tx1920\tx2880\tx3840\tx4800\tx5760\tx6720\tx7680\tx8640\tx9600\pardeftab720\ql\qnatural
+
+\f0\b\fs36 \cf0 TinyURLService\
+\
+
+\f1\b0\fs24 TinyURLService is a little program which shows to to create services for the services menu.  There is one services provided in this example:\
+\
+\pard\tx960\tx1920\tx2880\tx3840\tx4800\tx5760\tx6720\tx7680\tx8640\tx9600\pardeftab720\li160\fi-160\ql\qnatural
+\cf0 \'a5 "Shorten URL" service, which takes a URL and returns a tinyurl.com shortened URL.\
+\pard\tx960\tx1920\tx2880\tx3840\tx4800\tx5760\tx6720\tx7680\tx8640\tx9600\pardeftab720\ql\qnatural
+\cf0 \
+The service information is stored in Info.plist and read by the system during login. Take a look at the "Application Settings" to see how the service information is specified. Details on the various entries can be found in the  documentation, in "Services" under "Programming Topics."\
+\
+To install SimpleService, copy it into ~/Library/Services, log out and log back in.\
+\
+There are some tricky issues in creating a service:\
+\'a5 The Info.plist needs to specify LSBackgroundOnly = 1, in order to assure the service does not appear in the dock.}

File Examples/AppKit/TinyURLService/TinyURLService.py

+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'
+TINYURL_API = 'http://tinyurl.com/api-create.php'
+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):
+        try:
+            types = [NSURLPboardType, NSStringPboardType]
+            destType = pboard.availableTypeFromArray_(types)
+            if destType is None:
+                return ERROR(NSLocalizedString(
+                        "Error: Pasteboard doesn't contain a string.",
+                        "Pasteboard couldn't give string."
+                    ))
+
+            if destType == NSStringPboardType:
+                urlString = pboard.stringForType_(destType)
+                url = NSURL.URLWithString_(urlString.strip())
+            else:
+                url = NSURL.URLFromPasteboard_(pboard)
+
+            urlString = url.absoluteString()
+            
+            res = getTinyURL(urlString.UTF8String())
+
+            resURL = NSURL.URLWithString_(res)
+            if resURL is None:
+                return ERROR(NSLocalizedString(
+                    "Error: Resultant URL was not well-formed.",
+                    "URL not well-formed."
+                ))
+            pboard.declareTypes_owner_(types, None)
+            resURL.writeToPasteboard_(pboard)
+            pboard.setString_forType_(res, NSStringPboardType)
+            return ERROR(None)
+        except:
+            traceback.print_exc()
+            return ERROR(u'Exception, see traceback')
+
+    doTinyURLService_userData_error_ = serviceSelector(
+       doTinyURLService_userData_error_
+    )
+
+def main():
+    serviceProvider = TinyURLService.alloc().init()
+    NSRegisterServicesProvider(serviceProvider, u'TinyURLService')
+    AppHelper.runConsoleEventLoop()
+
+if __name__ == '__main__':
+    main()

File Examples/AppKit/TinyURLService/rebuild.py

+#!/usr/bin/env python
+"""
+Quickie script to update the services
+"""
+import AppKit
+AppKit.NSUpdateDynamicServices()

File Examples/AppKit/TinyURLService/setup.py

+"""
+Script for building the example.
+
+Usage:
+    python setup.py py2app
+"""
+from distutils.core import setup
+import py2app
+
+plist = dict(
+    CFBundleIdentifier = u'net.sf.pyobjc.TinyURLService',
+    LSBackgroundOnly = 1,
+    NSServices = [
+        dict(
+            NSKeyEquivalent=dict(
+                default=u'F',
+            ),
+            NSMenuItem=dict(
+                default=u'Open File',
+            ),
+            NSMessage=u'doOpenFileService',
+            NSPortName=u'TinyURLService',
+            NSSendTypes=[
+                u'NSStringPboardType',
+            ],
+        ),
+        dict(
+            NSKeyEquivalent=dict(
+                default=u'V',
+            ),
+            NSMenuItem=dict(
+                default=u'Shorten URL'
+            ),
+            NSMessage=u'doTinyURLService',
+            NSPortName=u'TinyURLService',
+            NSReturnTypes=[
+                u'NSURLPboardType',
+                u'NSStringPboardType',
+            ],
+            NSSendTypes=[
+                u'NSURLPboardType',
+                u'NSStringPboardType',
+            ],
+        ),
+    ],
+)
+
+
+setup(
+    app=["TinyURLService.py"],
+    options=dict(py2app=dict(plist=plist)),
+)