PerlPad [ Homepage | Project Summary page | Download | Source code ]
[ Back | Forward | Index ]

Turning Perl snippets into System Services

Add-on Services

PerlPad allows you to turn your favorites perl snippets into system services easily. All you have to do is define Add-On-Services in a single, simple XML file.

Your service library

Your Add-On services are configured by placing Service-Bundles into the ~/Library/Services directory. You may have to create that directory first.

Now

  • Create a folder PerlPadAddOn.service in ~/Library/Services. You can give it a different name if you want, but keep the .service suffix.
  • Create a folder Contents in that folder.
  • Create a text file Info.plist in the Contents folder. Copy the text below into Info.plist (or use the sample file provided with your PerlPad distribution).

Note: PerlPad is creating another folder (Perl_Pad.service) in your ~/Library/Services. It contains the definitions of the core PerlPad services. Do not edit this file, as it will be overwritten by PerlPad from time to time.

 1:<?xml version="1.0" encoding="UTF-8"?>
 2:<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" 
 3:        "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
 4:<plist version="1.0">
 5:<dict>
 6:        <key>NSServices</key>
 7:        <array>
 8:                <dict>
 9:                        <key>NSMenuItem</key>
10:                        <dict>
11:                                <key>default</key>
12:                                <string>PerlPad/Sum up the numbers</string>
13:                        </dict>
14:                        <key>NSMessage</key>
15:                        <string>processService</string>
16:                        <key>NSPortName</key>
17:                        <string>PerlPad</string>
18:                        <key>NSReturnTypes</key>
19:                        <array>
20:                                <string>NSStringPboardType</string>
21:                        </array>
22:                        <key>NSSendTypes</key>
23:                        <array>
24:                                <string>NSStringPboardType</string>
25:                        </array>
26:                        <key>NSUserData</key>
27:                        <string>my $sum=0; $sum+=$_ foreach split; $sum;</string>
28:                </dict>
29:        </array>
30:</dict>
31:</plist>

Give it a spin

Before going into the details of that file, let us see if it worked. You need to rebuild your Services menu now. This happens when you log in, but you do not need to log out because PerlPad lets you rebuild that menu when you feel like it. You feel like it now. Just select Rebuild Services Menu from the PerlPad application menu.

This should have made a new entry in the PerlPad submenu in the Services menu.

XML

You can edit the Info.plist file to roll your own Services without much effort. You can edit this file with any text editor, although XML-support could be of help. You have to take care to escape any XML control characters in your Perl code (like < becoming &lt;). Also, please check the documentation about these files that came with your Developer Tools CD.

Info.plist for an Add-On-Service contains:

  • NSMenuItem: This is how your Service will be labeled. Use slashes to make submenus.
  • NSMessage: This needs to be processService. It is an internal PerlPad method name and you will just break things if you put something else here.
  • NSPortName: This should be PerlPad. If you change this, your request could end up with iTunes or Safari. Dont.
  • NSSendTypes:This describes the input types of your service. PerlPad understands only unformatted text.
  • NSReturnTypes:This describes the output types of your service. PerlPad does only unformatted text.
  • NSUserData:Now this is more interesting. This highly application-specific field gets passed through to the program implementing the service. In the case of PerlPad, put your Perl code here. PerlPad will try to evaluate it, feeding it the selected text and returning the evaluation result.
  • NSKeyEquivalent:This lets you define a keyboard shortcut.

And now go and build your own Service menu!

PerlPad [ Homepage | Project Summary page | Download | Source code ]
[ Back | Forward | Index ]
Built With WebMake SourceForge.net Logo