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

  • 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.


One way to build an OS X service using Python is to bundle your Python app with py2app and edit the Info.plist file to provide the menu entries you need in the Service menu. A sample plist file can be found at https://pyobjc.readthedocs.io/en/latest/examples/Cocoa/AppKit/SimpleService/index.html You need to build the functionality to accept the commands you defined in the Info.plist's NSMessage section (see link mentioned above). You can use PyObjC to do this. This is a little bit trickier than using Automator, but has the advantage that you can bundle the whole functionality into a single App.