Search code examples
pythoncocoamacosservice

Can you create an Mac OS X Service with Python? How?


Can you create an Mac OS X Service with Python? How ?

What I want to do is to know hook my Python-fu to the service system provided by Mac OS X. Anyone knows how? If yes any working code snippet? Will work only on text or also on a given mimetype - defined object?

Service Popup


Solution

    • Open Automator.app and create a new service.

    • Select "Utilities" from the left-hand actions list, then drag the "Run Shell Script" action into the workflow.

    • Choose /usr/bin/python as your shell.

    • Type some python. For example:

    :

    import sys
    
    for f in sys.stdin:
         print "Hello World: " + f,
    
    • Save the service as, say, "Test"

    • Try it out in TextEdit.app. Type some text, select the text, then choose TextEdit -> Services -> Test from the menu. It should prepend "Hello World: " to each line of the text (as per the python code for f in sys.stdin)

    The above example works with text. Presumably, it could be modified to work with other data types provided through the OS X Services system.