SimpleService

Shows how to implement entries for the Services menu.

Sources

ServiceTest.py

import objc
from Foundation import *
from AppKit import *
import objc

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

class ServiceTest(NSObject):

    @serviceSelector
    def doOpenFileService_userData_error_(self, pboard, data, error):
        #NSLog(u"doOpenFileService_userData_error_(%s, %s)", pboard, data)
        try:
            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)
        except:
            import traceback
            traceback.print_exc()
            return ERROR(u'Exception, see traceback')


    @serviceSelector
    def doCapitalizeService_userData_error_(self, pboard, data, err):
        #NSLog(u"doCapitalizeService_userData_error_(%s, %s)", pboard, data)
        try:
            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)
        except:
            import traceback
            traceback.print_exc()
            return ERROR(u'Exception, see traceback')

SimpleService_main.py

from AppKit import *
from Foundation import *
from ServiceTest import *
from PyObjCTools import AppHelper

def main():
    #NSLog(u"main()")
    serviceProvider = ServiceTest.alloc().init()
    #NSLog(u"serviceProvider = %s", serviceProvider)
    NSRegisterServicesProvider(serviceProvider, u"PyObjCSimpleService")
    #NSLog(u"registered as PyObjCSimpleService")
    AppHelper.runConsoleEventLoop()

if __name__ == '__main__':
    main()

rebuild.py

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

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.PyObjCSimpleService',
    CFBundleName = u'PyObjCSimpleService',
    LSBackgroundOnly = 1,
    NSServices = [
        dict(
            NSKeyEquivalent=dict(
                default=u'F',
            ),
            NSMenuItem=dict(
                default=u'Open File',
            ),
            NSMessage=u'doOpenFileService',
            NSPortName=u'PyObjCSimpleService',
            NSSendTypes=[
                u'NSStringPboardType',
            ],
        ),
        dict(
            NSMenuItem=dict(
                default=u'Capitalize String',
            ),
            NSMessage=u'doCapitalizeService',
            NSPortName=u'PyObjCSimpleService',
            NSReturnTypes=[
                u'NSStringPboardType',
            ],
            NSSendTypes=[
                u'NSStringPboardType',
            ],
        ),
    ],
)


setup(
    name='Simple Service',
    app=["SimpleService_main.py"],
    options=dict(py2app=dict(plist=plist)),
)

Resources

Table Of Contents

Resources

Support development