(archive 'newLISPer)

July 4, 2009

newLISP and TextExpander for Mac

Filed under: newLISP — newlisper @ 09:45
Tags:

A mention of TextExpander on Creativityist‘s excellent blog reminded me that I haven’t talked about this cool MacOS X utility yet in relation to newLISP. So here’s a quick mention.

Perhaps, like me, you write text in many different applications. Sometimes you’ll find me in Scrivener, mainly for its awesome long document handling, and other times in ecto, the excellent blog post editor, ideal for WordPress. I also use BBEdit for all kinds of text and code, and VoodooPad, a container for all kinds of notes and stuff. I spend quite a lot of time typing in web pages such as comment boxes and forum pages, and composing email messages, too.

TextExpander works in every application you use on your Mac. The idea is simple: you type in a short abbreviation, and TextExpander expands it into a word or phrase. For example, if I want to insert a date stamp in ISO format into the current document or text area, I just type ,iso and TextExpander replaces it with the current date and time. (It didn’t happen just then, obviously – I cheated!) Another example is ‘newLISP’ – it’s so much quicker to type nl than it is to type newLISP, with that Shift key held down for the last four characters. I use various TextExpander snippets when I’m writing newLISP code, such as for writing the shebang line at the beginning of every script: It’s much quicker to type nl/ than #!/usr/bin/env newlisp.

There are lots of cool features in TextExpander. For example, you can control where the cursor is positioned in the expanded text – that’s great with code templates. You can synchronize multiple machines across the internet, so that you always have access to your snippets, no matter which computer you’re using. And you can subscribe to snippet files from an external URL, too – these are updated and maintained by the authors so you can easily benefit from their hard work. For example, there’s a large database of commonly mistyped words that will catch and correct those hard-to-spot typos. And for the whimsical, feel free to choose suitable sounds that accompany the expansion of some of your abbreviations (sounds from Myst add magic to even the most mundane memos).

What makes TextExpander particularly powerful, though, is that an abbreviation can run any newLISP script. This means that a short phrase or abbreviation can launch a script and insert its standard output into the current document. There’s no restriction on what you can put into the script, but it’s logical to generate some useful text using println, and/or use any text on the clipboard as raw input.

(In fact, TextExpander is script-language-agnostic, and lets you use any shell command or AppleScript. You might even be able to run those Common Lisp scripts. But naturally, newLISP is the best choice: quick to start up, fast in operation, and as powerful as you need it to be. Whatever shell script you choose, remember to set the Content Type to shell script and use the shebang line of your choice.)

Here’s an example of a newLISP script I use a lot. Suppose I’m writing about a web site, and viewing it in a browser window. I want to insert a link to the site in the document I’m writing. I’m probably using Daring Fireball: Markdown format, so I’ll want to insert both the web page title and the URL, the first enclosed in square brackets, the second in parentheses. In the document I type ,s, which runs the following newLISP script:

#!/usr/bin/env newlisp    
(define (get-safari-page-link)
  (exec
    (format
      [text]osascript -e 'tell application "Safari" to set urlLink to URL of front document
    return urlLink'[/text])))

(define (get-safari-page-title)
  (exec
    (format
      [text]osascript -e 'tell application "Safari" to set urlTitle to name of front document
    return  urlTitle'[/text])))

(set  'page-url   (get-safari-page-title)
      'page-title (get-safari-page-link))

(if-not (empty? page-url)
  (print  "["   (first page-url)
          "]("  (first page-title)
          ")")
  (println ""))

Yes, it’s a bizarre witches’ brew of newLISP and OSAscript, which is AppleScript ‘typed’ on the command line. I prefer to use osascript to run AppleScript commands, because it’s easier to switch between languages as required. Nevertheless, it quickly does the job, inserting details of the frontmost web page into the current document, and it doesn’t matter whether I’m writing in Ecto, BBEdit, or Scrivener.

Using the clipboard is currently the only way you can pass information to a script invoked by a shortcut. This is restrictive, I suppose, but it provides useful functionality nevertheless. Here’s a script that inserts a version of the text on the clipboard in which some HTML characters have been encoded:

#!/usr/bin/env newlisp

(set 'replacements '(
    ({&}  {&})
    ({>}   {>})
    ({<}   {<})))

(set 'text (exec "pbpaste"))

(dolist (line text)
    (dolist (i replacements)
       (replace (last i) line (first i)))
    (println line))

TextExpander keeps track of how many abbreviations you’ve used, and has a cute feature that tells you how much time you’ve saved, based on how many keys you haven’t had to press. Apparently I’ve saved hours of time that I would otherwise have spent typing. In reality, of course, I’ve probably just spent that time playing with TextExpander…!

Follow this link to find out how you can save 20% off the price of a TextExpander licence, till the end of July 2009.

Advertisements

Leave a Comment »

No comments yet.

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s

Create a free website or blog at WordPress.com.

%d bloggers like this: