The Pug Automatic

Improved Greasemonkey bundle for TextMate

Written August 31, 2006. Tagged Greasemonkey, JavaScript, Ruby, OS X, TextMate, Firefox.

I extended Adam V's TextMate Greasemonkey bundle 1.0 with some useful commands, modified the snippets and template slightly and also added unsafeWindow, GM_setValue() and friends to the language grammar.

[Screenshot]

Download (16 KB).

The grammar and the snippets are mainly by Adam.

This is what the bundle currently contains, other than the grammar.

Template

Select File > New From Template > Greasemonkey > Userscript to create a new script from template. The template outputs this:

header

/* Your favorite functions go here */

You should change the template (File > New From Template > Edit Templates…) to include whatever functions you commonly use. I decided against including mine to make the bundle less controversial. I will probably blog them at a later date.

The header bit is to be used with a snippet. Hopefully, TextMate will support snippety placeholders in templates directly at some point.

Snippets

The header snippet outputs a userscript header with placeholders, like this:

// ==UserScript==
// @name Name
// @namespace http://www.example.com
// @description Description.
// @include *
// ==/UserScript==

If you set a TM_NAMESPACE shell variable in the TextMate preferences, this will be used as the default @namespace.

Within the header, the snippets i and e add @include and @exclude directives. Sadly, TextMate doesn't support snippets-within-snippets yet, so this does not work until after you've broken out of the header snippet. However, the "Continue Header URL" command works fine.

Commands

Continue Header URL

Available when writing @include and @exclude directives. Hitting will add another of the same directive on the next line, moving the caret as appropriate.

Install and Edit

If you start writing a new script and then hit ⌘B, the script will be installed, closed and the installed version opened for editing. This makes starting new scripts vastly less annoying.

Caveats: "Enable access for assistive devices" must be toggled on in the Universal Access prefpane, otherwise closing the old file will not work. The command assumes the file is unsaved. If it is not, you will hear a system beep as it fails to click "Don't Save Changes".

Update Metadata

Hit ⌘D (conveniently next to the S of saving fame) to update the metadata from the values in the script file.

Greasemonkey stores the @name, @include and @exclude values and friends when a script is installed. After that time, these values must be changed in the "Manage User Scripts" window – unless you use this command.

Caveat: The command replaces the metadata values with the script values. This means that if you've e.g. changed @includes in "Manage User Scripts" but not in the script itself, your modifications are lost.

Uninstall Script

Uninstalls the currently open script. Has no keyboard shortcut by default. Prompts for confirmation.

Open Installed Script…

Displays a dialog listing every installed userscript, ordered alphabetically. Select one and confirm to open it. Has no keyboard shortcut by default.

Reload Firefox

Hit ⌘R to activate Firefox and reload the current page. Caveats: Either "Enable access for assistive devices" must be on, or Firefox should not be set to open URLs from external applications in new tabs.

References

There are also three commands to look up more information on XPath expressions, the Gecko DOM or Greasemonkey-specific functions. These are all bound by default to ⌃⌥⌘H.

To do

Suggestions are very welcome. Some things I am considering adding are:

A command like "Open Installed Script..." but with the scripts sorted in reverse chronological order of installation, or modifying this command to offer that choice.

Submit (as new or an update) the current script to userscripts.org.

Updating metadata on ⌘S along with saving a script.

Some updates have been made.