(archive 'newLISPer)

December 14, 2005

Running newLISP scripts in BBEdit/TextWrangler

Filed under: newLISP — newlisper @ 09:52

If you’re using BBEdit or TextWrangler on MacOS X, it’s easy to write newLISP scripts that process text windows. Store your scripts in the ~/Library/Application Support/BBEdit/Unix Support/Unix Filters/ folder.

A basic script looks like this:

(dolist (file-name (2 (main-args)))
    (set 'file (open file-name "read"))
    (while (read-line file)
        (println (current-line)))
    (close file))

Notice the 2 in (2 (main-args)): this extracts the name of the file from the list of arguments that BBEdit passes to newLISP. The arguments are as follows:

  1. argument 0: “/usr/bin/newlisp”
  2. argument 1: path to the Unix filter
  3. argument 2: path to BBEdit’s temporary copy of the document

So this script processes argument 2, the temporary version of your current document that BBEdit creates.

As it stands, this script simply replaces each line of the current window with itself. To make it do something more useful, just insert more code:

(dolist (file-name (2 (main-args))) ; 2
    (set 'file (open file-name "read"))
    (while (read-line file)
            (replace {(\d+)}
                (string (add (int $1) 1)) 0 )))
    (close file))

This adds 1 to every number in the text.

By the way, BBEdit/TextWrangler can run Unix filters on the current selection only, not just the whole file.


  1. >Perhaps a typo? The index for main-args should go after (main-args). The way it is written you will extract more than one argument:~> newlisp a b c d newLISP v.8.7.4 on OSX, execute ‘newlisp -h’ for more info.> (main-args)(“newlisp” “a” “b” “c” “d”)> (1 (main-args))(“a” “b” “c” “d”)> (2 (main-args))(“b” “c” “d”)> (3 (main-args))(“c” “d”)> (4 (main-args))(“d”)what you want goes with the index after (main-args):> ((main-args) 1)”a”> ((main-args) 2)”b”> ((main-args) 3)”c”> ((main-args) 4)”d”And there is a short-cut for ((main-args) idx):> (main-args 1)”a”> (main-args 2)”b”> (main-args 3)”c”> Thanks for this great tip how to use newLISP scripst to work with BBEdit or TextWrangler. This could be used to write text formatters etc.

    Comment by don Lucio — December 14, 2005 @ 13:28 | Reply

  2. >… turns out newLISPer’s code was right! Because he really wants the list of files and not the single elements … (“afile”) versus “afile”.I realized then trying is out it TextWrangler.But perhaps the little lesson about indexing in newLISP can help somebody else in a different problem ;)But we could shorten the program, because we are really only interested in one file name: “afile” versus (“afile”).#!/usr/bin/newlisp(set ‘file (open (main-args 2) “read”)) (while (read-line file) (println (replace {(\d+)} (current-line) (string (add (int $1) 1)) 0 ))) (close file)) (exit)

    Comment by don Lucio — December 14, 2005 @ 15:42 | Reply

  3. >a parenthesis too much in the last comment, here the correction:#!/usr/bin/newlisp(set ‘file (open (main-args 2) “read”))(while (read-line file) (println(replace {(\d+)}(current-line)(string (add (int $1) 1)) 0 ))) (close file)(exit) And here 2 useful scripts to comment and uncomment a section of code:comment.lsp:#!/usr/bin/newlisp(set ‘file (open (main-args 2) “read”))(while (read-line file) (println “;” (current-line)))(close file)(exit)uncomment.lsp#!/usr/bin/newlisp(set ‘file (open (main-args 2) “read”))(while (read-line file) (println (1 (current-line))))(close file)(exit)

    Comment by don Lucio — December 14, 2005 @ 16:10 | Reply

  4. >Thanks for the comments! Indeed, I should perhaps stress a bit more somewhere that I’m a beginner (perhaps ‘newlisper’ is too self-referential? :-)) and have just started learning this myself… The experts all live over at the main newLISP forum. :-)All corrections more than welcome!

    Comment by newlisper — December 14, 2005 @ 17:27 | 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 )

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

Blog at WordPress.com.

%d bloggers like this: