FastScripts 3.1: Streamlined Regular Expressions
March 11th, 2022FastScripts 3.1 is now available on the FastScripts home page, or by selecting “Check for Update” from the FastScripts menu.
This update introduces an expansion of FastScripts’s own built-in scripting additions, with three powerful new commands for searching, replacing, and splitting text with regular expressions.
These can be used by any AppleScript on your Mac, whether you’re running the script from FastScripts or not. These new commands are also completely free of charge, so if you install and keep FastScripts running in the background, your scripts will always have access to these features.
Previously it was cumbersome to use regular expressions in AppleScript, resulting in people commonly calling out to scripting languages with “do shell script”, invoking Cocoa directly via AppleScriptObjC interfaces, or installing third-party scripting libraries such as Shane Stanley’s wonderful RegExAndStuff library.
I want to thank John Gruber for reviewing and helping with with the design of the scripting interface, which I like to think represents the “Mac-ification” of providing regular expression support to AppleScript.
So how does it work? In your AppleScript, be sure to wrap whatever text processing commands you use with a “tell” block addressing FastScripts:
tell application "FastScripts"
— Your script here
end tell
Your script can then use the search text, replace text, or split text commands to search or transform text as you see fit. Here are some simple examples:
Searching Text
The search text command can be used to perform complex regular expression searches, but that includes simple searches for plain text strings:
set woodstack to "How much wood would a woodchuck chuck if a woodchuck could chuck wood?"
set groups to search text woodstack matching pattern "wood"
Results in the AppleScript record:
{{offset:9, text:"wood"}, {text:"wood", offset:22}, {text:"wood", offset:43}, {offset:65, text:"wood"}}
Replacing Text
The replace text command works very similarly. This time we introduce some actual regular expression syntax. Note that you have to double-backslash some terms to make AppleScript happy:
set woodstack to "How much wood would a woodchuck chuck if a woodchuck could chuck wood?"
set cheesestack to replace text woodstack matching pattern "(\\bw[^\\s]+?d)" replacement pattern "cheese"
Results in:
"How much cheese cheese a cheesechuck chuck if a cheesechuck could chuck cheese?"
Splitting Text
Finally, the split text command is used to separate a string into parts based on a plain text delimiter or a complex regular expression pattern. For simplicity, this example just demonstrates how to separate all the components of a string based on white space:
set woodstack to "How much wood would a woodchuck chuck if a woodchuck could chuck wood?"
set splitstack to split text woodstack matching pattern "\\s+"
Results in:
{"How", "much", "wood", "would", "a", "woodchuck", "chuck", "if", "a", "woodchuck", "could", "chuck", "wood?"}
Beyond Simplicity
These new commands also support advanced features including the ability to use regular expression matching groups, specify case insensitivity, etc. Open FastScripts’s own scripting dictionary in your favorite script editor and explore the items in the FastScripts Text Processing suite. Starting with FastScripts 3.1, an easy way to do this is simply to select FastScripts -> Open FastScripts Scripting Dictionary from the FastScripts menu.
Other Changes
Text processing commands are the “big deal” in this update, but there are a few smaller changes that may also be of interest! Here is the complete list of updates in this release:
- New built-in scripting support for regular expression commands “search text”, “replace text” and “split text”
- Updated “open web page” scripting command to support a new “replacing current page” parameter
- Added a new “Open FastScripts Scripting Dictionary” menu item
- When searching, recently used scripts are now prioritized in search results
- Fix a bug that caused recently added “invoke” support for paths to report a spurious error
- Fix a bug introduced in 3.0.6 that prevented “invoke” from working properly on a script item
- Alleviate console log warnings that were being printed when the menu bar icon is clicked
If you enjoy FastScripts, please consider spreading the word on Facebook or Twitter! Thanks for your support.