Python Execution Context Overview

 

Introduction

The Bellhop Python Execution Context plug-in executes services that are implemented in the Python scripting language. The plug-in makes use of the default Python installation bundled with Mac OS X (/usr/bin/python), and executes each service in a spawned NSTask that invokes the Python interpreter with the service's code.

To support services written in Python, the plug-in provides three extensions which are automatically loaded at runtime into any Python services that are managed by Bellhop. These extensions, pasteboard, panel and finder, allow Python scripts to interact with the pasteboard, interact with users, as well as invoke file functions via the Finder. The extensions are explained in more detail in the Reference part of this document.

 

Basic Syntax

Python scripts can be structured quite freely; the only requirement is that the script contain a run function defined at the top level of the Python environment. The name of this function should be runService() and it should take a single string argument that specifies the name of the pasteboard instance responsible for a service transaction. The listing below shows the basic structure of a Python service script.

def runService(aPasteboard): # Put your custom Python code here in the runService() function. # It will automatically be invoked when a user selects this # service from the menu.

The aPasteboard argument will be a string that specifies the unique pasteboard name for the service transaction, and is automatically passed to the function by Bellhop.

You are not required to explicitly load any of the Bellhop extensions into your service; they are automatically loaded when a Python service is executed. You can, however, import any installed Python modules and libraries you have on your system and use them within your service using any valid Python syntax. You can also define your own classes, additional methods, constants, etc., so long as you provide a valid runService() function at the top level.

For a more detailed Python example, see the Example section of this document.

 

Python Extensions

As metioned above, the Bellhop Python plug-in implements a set of custom extensions that allow Python services to interact with the Mac OS X Services System, as well as interact with users through a set of custom user interface panels and do basic file manipulation via the Finder.

There are three Python extensions that you can make use of in your services: pasteboard, panel and finder. The pasteboard extension provides methods that allow you to interact with the system pasteboard, which is used to pass data between the requesting application and your service. You can also use the pasteboard extension to write data to the standard clipboard. The panel module provides methods that display user interface panels to users. There are panels to display alerts, prompt panels that allow your services to accept user input, and file selection and save panels. The finder module has methods that allow you to do basic file manipulation, such as copy, move and delete files, as well as ask the Finder to open files, urls and applications.

For a complete description of the extensions, see the Reference section of this document.