Commits

Bob Ippolito  committed eadbaa1

Beginnings of a /Developer/Examples/AppKit/SimpleService port

  • Participants
  • Parent commits 221b408

Comments (0)

Files changed (3)

File pyobjc/Examples/SimpleService/README.rtf

+{\rtf1\mac\ansicpg10000\cocoartf102
+{\fonttbl\f0\fswiss\fcharset77 Helvetica-Bold;\f1\fswiss\fcharset77 Helvetica;\f2\fswiss\fcharset77 Helvetica-Oblique;
+}
+{\colortbl;\red255\green255\blue255;}
+\vieww12000\viewh15840\viewkind0
+\deftab720
+\pard\tx960\tx1920\tx2880\tx3840\tx4800\tx5760\tx6720\tx7680\tx8640\tx9600\pardeftab720\ql\qnatural
+
+\f0\b\fs36 \cf0 SimpleService\
+\
+
+\f1\b0\fs24 SimpleService is a little program which shows to to create services for the services menu.  There are two services provided in this example:\
+\
+\pard\tx960\tx1920\tx2880\tx3840\tx4800\tx5760\tx6720\tx7680\tx8640\tx9600\pardeftab720\li160\fi-160\ql\qnatural
+\cf0 \'a5 "Open File" service, which opens the file specified by the provided string. This service has no return value.\
+\pard\tx960\tx1920\tx2880\tx3840\tx4800\tx5760\tx6720\tx7680\tx8640\tx9600\pardeftab720\ql\qnatural
+\cf0 \
+\'a5 "Capitalize" service, which takes a string and returns a capitalized version.\
+\
+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.\
+\pard\tx960\tx1920\tx2880\tx3840\tx4800\tx5760\tx6720\tx7680\tx8640\tx9600\pardeftab720\li160\fi-160\ql\qnatural
+\cf0 \'a5 Makefile.preamble specifies that the wrapper extension should be ".service", not ".app" (see APP_WRAPPER_EXTENSION). 
+\f2\i However, in Public Beta, this needs to be ".app" for the service to be properly launched.
+\f1\i0 \
+\
+\
+}

File pyobjc/Examples/SimpleService/ServiceTest.py

+from Foundation import *
+from AppKit import *
+
+def serviceSelector(fn):
+    return obj.selector(fn, signature="v@:@@o^@")
+
+def ERROR(s):
+    return (s,)
+
+class ServiceTest(NSObject):
+    def doOpenFileService_userData_error_(self, pboard, data):
+        types = pboard.types()
+        pboardString = None
+        if NSStringPboardType in types:
+            pboardString = pboard.stringForType_(NSStringPboardType)
+        if pboardString is None:
+            return ERROR(NSLocalizedString(
+                "Error: Pasteboard doesn't contain a string.",
+                "Pasteboard couldn't give string."
+            ))
+
+        if not NSWorkspace.sharedWorkspace().openFile_(pboardString):
+            return ERROR(NSLocalizedString(
+                "Error: Couldn't open file %s.",
+                "Couldn't perform service operation for file %s."
+            ) % pboardString)
+        
+        return ERROR(None)
+         
+    
+    doOpenFileService_userData_error_ = serviceSelector(
+        doOpenFileService_userData_error_
+    )
+
+    def doCapitalizeService_userData_error_(self, pboard, data, error):
+        types = pboard.types()
+        pboardString = None
+        if NSStringPboardType in types:
+            pboardString = pboard.stringForType_(NSStringPboardType)
+        if pboardString is None:
+            return ERROR(NSLocalizedString(
+                "Error: Pasteboard doesn't contain a string.",
+                "Pasteboard couldn't give string."
+            ))
+
+        newString = NSString.capitalizedString(pboardString)
+
+        if not newString:
+            return ERROR(NSLocalizedString(
+                "Error: Couldn't capitalize string %s.",
+                "Couldn't perform service operation for string %s."
+            ) % pboardString)
+
+        types = [NSStringPboardType]
+        pboard.declareTypes_owner_([NSStringPboardType], None)
+        pboard.setString_forType_(newString, NSStringPboardType)
+        return ERROR(None)
+   
+   doCapitalizeService_userData_error_ = serviceSelector(
+       doCapitalizeService_userData_error_
+    )
+

File pyobjc/Examples/SimpleService/SimpleService_main.py

+from Foundation import *
+from ServiceTest import *
+
+if __name__ == '__main__':
+    serviceProvider = ServiceTest.alloc().init()
+    NSRegisterServiceProvider(serviceProvider, u"PyObjCSimpleService")
+    try:
+        NSRunLoop.currentRunLoop().configureAsServer()
+        NSRunLoop.currentRunLoop().run()
+    except Exception, localException:
+        NSLog(localException)
+    
+    del serviceProvider