Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 673
fabrizioM Avatar asked Apr 03 '11 06:04

fabrizioM


1 Answers

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

like image 65
Darren Avatar answered Oct 14 '22 10:10

Darren