Turning Perl snippets into System 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.
Create a folder
~/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"
12: <string>PerlPad/Sum up the numbers</string>
27: <string>my $sum=0; $sum+=$_ foreach split; $sum;</string>
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.
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 <). 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!