I extended Adam V’s TextMate Greasemonkey bundle 1.0 with some useful commands, modified the snippets and template slightly and also added
GM_setValue() and friends to the language grammar.
Download (16 KB).
The grammar and the snippets are mainly by Adam.
This is what the bundle currently contains, other than the grammar.
File > New From Template > Greasemonkey > Userscript to create a new script from template. The template outputs this:
1 2 3
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.
header bit is to be used with a snippet. Hopefully, TextMate will support snippety placeholders in templates directly at some point.
header snippet outputs a userscript header with placeholders, like this:
1 2 3 4 5 6
If you set a
TM_NAMESPACE shell variable in the TextMate preferences, this will be used as the default
Within the header, the snippets
@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.
Continue Header URL
Available when writing
@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”.
⌘D (conveniently next to the
S of saving fame) to update the metadata from the values in the script file.
Greasemonkey stores the
@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.
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.
⌘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.
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
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.