(archive 'newLISPer)

January 26, 2007

ThisService: a cool MacOS X utility

Filed under: newLISP — newlisper @ 23:03


Situation now understood! Read this post.


Since writing this I’ve been having problems with service scripts ignoring line endings in some applications, which prevents these services working correctly on multi-line input. Until I understand it a bit better, be aware that multi-line input might not be processed correctly!

Original post

From John Gruber at Daring Fireball comes news of a cool new utility called ThisService. This donation-ware application does one thing brilliantly: it creates system services out of scripts. (This is a MacOS X post…)

A service, on MacOS X, is a small piece of code that can be launched while you’re in any program. Services appear on the application menu, and typically do one useful job, either with the selection, or with the frontmost document. For example, if you’ve selected a path name in a text document, the Finder offers the service of creating a new window and showing you that file’s icon. And if you select an icon in the Finder, applications such as BBEdit offer the service of opening that file in that application. Simple, but effective. Services can be provided by the big applications, or can be smaller stand-alone services.

Up to now it hasn’t been easy to develop your own services. Now, though, with ThisService, you can take your scripts, written in a language of your choice (which is newLISP, obviously :-)), and create a system-wide service.

There are three basic possibilities. You can write a service that works on selected text, creates new text, or does both.

Here’s how the “Create” option might work. Here’s my newLISP script:

(println (date (date-value) 0 "%Y-%m-%d %H:%M:%S"))

which I save in a file called “insert-iso8601.lsp”. Then, using ThisService, I create a new service that runs this script when it’s invoked. Because this script doesn’t act on existing text, I just select the “Produces output” button. If I want to, I can create a keyboard shortcut for it too.

And that’s it. This script is now available to me in every application that supports services (and that’s most of them, with some predictable exceptions).

If you want to write a service to process selected text, you write a script that reads STDIN and writes to STDOUT. Here, for example, is a very simple filter:

(while (read-line)
  (println (lower-case (current-line)))) 

which converts the selected text to lower-case. Here, you would specify that the service both creates output and acts on input.

For something like a word-counting script, you can specify that only input is required:

(set 'words '())
(while (read-line)
   (set 'words (append words (parse (current-line) "\s" 0))))

 (clean empty? words)
(exec (format "/usr/local/bin/growlnotify %d -m \"word count \"" (length words)))

The word count is output using Growl.

Here’s another quick script – a rewrapping command for rewrapping selected text:


(set 'words '())
(while (read-line) (set 'words (append words (parse (current-line) "\s" 0)))) (clean empty? words) (clean (fn (x) (= "\r" x)) words) (set 'cursor 0) (dolist (w words) (print w " ") (inc 'cursor (+ 1 (length w))) (if (> cursor 40) (begin (set 'cursor 0) (println)))) (exit)

I haven’t yet used this enough to decide whether this is a good way to re-wrap text. Improvements welcome!



  1. >This is the absolute essential application for newLISP scripting on the Mac. Thanks for the discovery and examples.

    Comment by don Lucio — January 27, 2007 @ 00:04 | Reply

  2. >Hi Cormullion,Thank you for this post! As Don Lucio said, this is a must for Mac users. I’ve already used it to solve the problem of how to generate the style sheets for the individual pages at neglOOk through the browser. Once I’m in the text area of the style sheet page I’m creating (using edit=), I can just type the hex color (e.g., 3a5) for the background of the page the style sheet is for, highlight it, select the service and, wow, there’s the complete text of the style sheet, ready to be saved! This program is immensely helpful to me, since I now do most of the text-editing associated with neglOOk within the text area of a browser. Not the most powerful editor in the world, but it does do bracket matching. With services and newLISP, any text field can be a super-editor!I’m really excited about newLISP wiki’s potential. After maintaining our site using a few different methods now, doing it in newLISP wiki feels like playing! It’s so easy to update pages. From making site-wide changes to tweaking individual pages, the effort is minimal and the results are rewarding. I’m constantly experiencing those moments when it hits me how much of a “Right Thing” newLISP wiki is.Thanks again, and I hope to begin contributing here again soon. Maybe I’ll write about newLISP wiki . . . m i c h a e l

    Comment by m i c h a e l — January 28, 2007 @ 00:13 | Reply

  3. >I was wondering if you had received any improvements to the rewrap script? It isn’t really working as expected, but I’m not the right person to offer assistance (also being a regexp n00b)

    Comment by patito — May 17, 2008 @ 02:33 | Reply

  4. >Hi patito. I have to say, rewrap works OK for me, in my testing. What problems do you have?I suspect that it’s more to do with CRs and LFs than the actual regex…

    Comment by newlisper — May 17, 2008 @ 08:32 | Reply

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

Create a free website or blog at WordPress.com.

%d bloggers like this: