Mimsy Were the Borogoves

Hacks: Articles about programming in Python, Perl, Swift, BASIC, and whatever else I happen to feel like hacking at.

SilverService and Taskpaper

Jerry Stratton, December 22, 2007

A couple of days ago someone on the Taskpaper forum posted a feature request: “How many of us would like the projects to be sortable taking all children along. I would!”

That sounds interesting, I thought. The gears immediately began to click into place. Sort text; Mac GUI application; text editor. The slot machine came up SilverService, Perl, and the Mac OS X services menu.

Taskpaper formats its display differently than a text editor does, but at its heart it is a text editor. That makes it a perfect target for command-line services.

So far, I’ve posted a Python bridge between Django and Taskpaper and a PHP script for web display of Taskpaper documents. Either of those scripting languages would allow sorting by project. However, when I look at a problem I look at it in terms of which tool is most suited to it rather than, how can I force this tool to solve it. In this case the problem is basically a text filter, something Perl excels at.

I’m going to describe the solution step-by-step using SilverService and Smultron. SilverService is a very cool way of taking command-line programs and turning them into services under the Services menu.

The problem is, how to sort by project when each project is also followed by notes and tasks that must remain with their project.

Here is the script:

[toggle code]

  • #!/usr/bin/perl
  • #sort a Taskpaper-style task list alphabetically by project
  • #compile the list of projects
  • while (<>) {
    • chomp;
    • if (/^(.*):$/) {
      • $project = $1;
      • $projects{$project} = "";
    • } else {
      • $projects{$project} .= $_ . "\n";
    • }
  • }
  • #display the projects sorted alphabetically
  • foreach $project (sort keys %projects) {
    • print $project, ":\n" if $project;
    • print $projects{$project}
  • }

Paste that script into Smultron. Save it in a folder called “bin” in your home directory. Name it “sortProjects”. You may have to “Create folder” to create the “bin” folder.

After you’ve saved it, pull down Smultron’s “Tools” menu, go to “Commands”, and then “Other”, and choose “Make executable”. A blank window will come up if everything goes okay; dismiss it.

Since SilverService services are command-line programs, it can help to test them through the command line. Open the Terminal program and type “bin/sortProjects”. When you press “return”, you should just get a blank line back. Type:

  • House:
  • - Fix garage roof
  • - Replace kitchen cabinets
  • Blog:
  • - Complain about Word
  • - Post photos of pets
  • - Post photos of children
  • - Post photos of children and pets

Press return at the end of each line; if you copied and pasted, make sure you press return after the last line. Note that this project list is not in alphabetical order. Type Control-D, and the script will sort the projects while keeping the tasks intact beneath each project:

  • Blog:
  • - Complain about Word
  • - Post photos of pets
  • - Post photos of children
  • - Post photos of children and pets
  • House:
  • - Fix garage roof
  • - Replace kitchen cabinets

If the script doesn’t return the projects sorted like that, you’ll want to fix the script before going any further. Once the script is working, you can set up a service in SilverService using it.

Go into SilverService and open its Preferences. Choose the “Add Service” button. For service name, call it “Sort Taskpaper Projects”. For shell command, type “/Users/YOURHOMEDIR/bin/sortProjects”. For input, choose “stdin”, and for output, choose “service”. Close the preferences window.

If this is the first time you’ve used SilverService, you may have to log out and log in again. You should then have a “SilverService” submenu in your Services menu. These services will work in any service-supporting application, such as Safari, Smultron, Nisus Writer, or Taskpaper.

If you paste the same project lines for House and Blog into Taskpaper, and select those projects and tasks, you can sort them by project name by choosing the “Sort Taskpaper Projects” service item.

SilverService works with any command line program, whether they are scripts you write or commands built in to Mac OS X.

  1. <- Web Taskpaper
  2. Django Authentication ->