{"id":3822,"date":"2022-03-11T17:01:11","date_gmt":"2022-03-11T22:01:11","guid":{"rendered":"https:\/\/redsweater.com\/blog\/?p=3822"},"modified":"2022-03-11T18:36:32","modified_gmt":"2022-03-11T23:36:32","slug":"fastscripts-3-1-streamlined-regular-expressions","status":"publish","type":"post","link":"https:\/\/redsweater.com\/blog\/3822\/fastscripts-3-1-streamlined-regular-expressions","title":{"rendered":"FastScripts 3.1: Streamlined Regular Expressions"},"content":{"rendered":"<p>FastScripts 3.1 is now available on the <a href=\"https:\/\/redsweater.com\/fastscripts\/\">FastScripts home page<\/a>, or by selecting \u201cCheck for Update\u201d from the FastScripts menu.<\/p>\n<p>This update introduces an expansion of FastScripts&#8217;s own built-in scripting additions, with three powerful new commands for searching, replacing, and splitting text with regular expressions.<\/p>\n<p>These can be used by any AppleScript on your Mac, whether you&#8217;re running the script from FastScripts or not. These new commands are also <strong>completely free of charge<\/strong>, so if you install and keep FastScripts running in the background, your scripts will always have access to these features.<\/p>\n<p>Previously it was cumbersome to use regular expressions in AppleScript, resulting in people commonly calling out to scripting languages with &#8220;do shell script&#8221;, invoking Cocoa directly via AppleScriptObjC interfaces, or installing third-party scripting libraries such as Shane Stanley&#8217;s wonderful <a href=\"https:\/\/latenightsw.com\/freeware\/\">RegExAndStuff<\/a> library.<\/p>\n<p>I want to thank <a href=\"https:\/\/daringfireball.net\/\">John Gruber<\/a> for reviewing and helping with with the design of the scripting interface, which I like to think represents the &#8220;Mac-ification&#8221; of providing regular expression support to AppleScript.<\/p>\n<p>So how does it work? In your AppleScript, be sure to wrap whatever text processing commands you use with a &#8220;tell&#8221; block addressing FastScripts:<\/p>\n<div style=\"background-color:#F0F0F0; padding: 0.5em; margin: 0.5em 0;\">\n<p style=\"font-size: 12px; line-height: normal; font-family: Verdana;\"><span style=\"font-variant-ligatures: no-common-ligatures;\"><b>tell<\/b> <\/span><span style=\"font-variant-ligatures: no-common-ligatures; color: #0000ff;\"><i>application<\/i><\/span><span style=\"font-variant-ligatures: no-common-ligatures;\"> &quot;FastScripts&quot;<\/span><\/p>\n<p style=\"font-size: 12px; line-height: normal; font-family: Verdana; color: #4c4e4e;\"><span style=\"font-variant-ligatures: no-common-ligatures;\">&#8212; Your script here<\/span><\/p>\n<p style=\"font-size: 12px; line-height: normal; font-family: Verdana;\"><span style=\"font-variant-ligatures: no-common-ligatures;\"><b>end<\/b> <b>tell<\/b><\/span><\/p>\n<\/div>\n<p>Your script can then use the <strong>search text<\/strong>, <strong>replace text<\/strong>, or <strong>split text<\/strong> commands to search or transform text as you see fit. Here are some simple examples:<\/p>\n<h3>Searching Text<\/h3>\n<p>The\u00a0<strong>search text<\/strong> command can be used to perform complex regular expression searches, but that includes simple searches for plain text strings:<\/p>\n<div style=\"background-color:#F0F0F0; padding: 0.5em; margin: 0.5em 0;\">\n<p style=\"  font-size: 12px; line-height: normal; font-family: Verdana;\"><span style=\"font-variant-ligatures: no-common-ligatures;\"><b>set<\/b> <\/span><span style=\"font-variant-ligatures: no-common-ligatures; color: #408000;\">woodstack<\/span><span style=\"font-variant-ligatures: no-common-ligatures;\"> <b>to<\/b> &quot;How much wood would a woodchuck chuck if a woodchuck could chuck wood?&quot;<\/span><\/p>\n<p style=\" font-size: 12px; line-height: normal; font-family: Verdana; color: #0000ff;\"><span style=\"font-variant-ligatures: no-common-ligatures; color: #000000;\"><b>set<\/b> <\/span><span style=\"font-variant-ligatures: no-common-ligatures; color: #408000;\">groups<\/span><span style=\"font-variant-ligatures: no-common-ligatures; color: #000000;\"> <b>to<\/b> <\/span><span style=\"font-variant-ligatures: no-common-ligatures;\"><b>search text<\/b><\/span> <span style=\"font-variant-ligatures: no-common-ligatures; color: #408000;\">woodstack<\/span> <span style=\"font-variant-ligatures: no-common-ligatures;\">matching pattern<\/span><span style=\"font-variant-ligatures: no-common-ligatures; color: #000000;\"> &quot;wood&quot;<\/span><\/p>\n<\/div>\n<p>Results in the AppleScript record:<\/p>\n<div style=\"background-color:#F0F0F0; padding: 0.5em; margin: 0.5em 0;\">\n<p style=\"margin: 0px; font-stretch: normal; font-size: 12px; line-height: normal; font-family: Verdana;\"><span style=\"font-variant-ligatures: no-common-ligatures;\">{{<\/span><span style=\"font-variant-ligatures: no-common-ligatures; color: #6c05d3;\">offset<\/span><span style=\"font-variant-ligatures: no-common-ligatures;\">:9, <\/span><span style=\"font-variant-ligatures: no-common-ligatures; color: #6c05d3;\">text<\/span><span style=\"font-variant-ligatures: no-common-ligatures;\">:&quot;wood&quot;}, {<\/span><span style=\"font-variant-ligatures: no-common-ligatures; color: #6c05d3;\">text<\/span><span style=\"font-variant-ligatures: no-common-ligatures;\">:&quot;wood&quot;, <\/span><span style=\"font-variant-ligatures: no-common-ligatures; color: #6c05d3;\">offset<\/span><span style=\"font-variant-ligatures: no-common-ligatures;\">:22}, {<\/span><span style=\"font-variant-ligatures: no-common-ligatures; color: #6c05d3;\">text<\/span><span style=\"font-variant-ligatures: no-common-ligatures;\">:&quot;wood&quot;, <\/span><span style=\"font-variant-ligatures: no-common-ligatures; color: #6c05d3;\">offset<\/span><span style=\"font-variant-ligatures: no-common-ligatures;\">:43}, {<\/span><span style=\"font-variant-ligatures: no-common-ligatures; color: #6c05d3;\">offset<\/span><span style=\"font-variant-ligatures: no-common-ligatures;\">:65, <\/span><span style=\"font-variant-ligatures: no-common-ligatures; color: #6c05d3;\">text<\/span><span style=\"font-variant-ligatures: no-common-ligatures;\">:&quot;wood&quot;}}<\/span><\/p>\n<\/div>\n<h3>Replacing Text<\/h3>\n<p>The\u00a0<strong>replace text<\/strong> 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:<\/p>\n<div style=\"background-color:#F0F0F0; padding: 0.5em; margin: 0.5em 0;\">\n<p style=\"margin-right: 0px; margin-bottom: 0px; margin-left: 83.2px; text-indent: -83.2px; font-stretch: normal; font-size: 12px; line-height: normal; font-family: Verdana;\"><span style=\"font-variant-ligatures: no-common-ligatures;\"><b>set<\/b> <\/span><span style=\"font-variant-ligatures: no-common-ligatures; color: #408000;\">woodstack<\/span><span style=\"font-variant-ligatures: no-common-ligatures;\"> <b>to<\/b> &quot;How much wood would a woodchuck chuck if a woodchuck could chuck wood?&quot;<\/span><\/p>\n<p style=\"  font-size: 12px; line-height: normal; font-family: Verdana; color: #0000ff;\"><span style=\"font-variant-ligatures: no-common-ligatures; color: #000000;\"><b>set<\/b> <\/span><span style=\"font-variant-ligatures: no-common-ligatures; color: #408000;\">cheesestack<\/span><span style=\"font-variant-ligatures: no-common-ligatures; color: #000000;\"> <b>to<\/b> <\/span><span style=\"font-variant-ligatures: no-common-ligatures;\"><b>replace text<\/b><\/span> <span style=\"font-variant-ligatures: no-common-ligatures; color: #408000;\">woodstack<\/span> <span style=\"font-variant-ligatures: no-common-ligatures;\">matching pattern<\/span><span style=\"font-variant-ligatures: no-common-ligatures; color: #000000;\"> &quot;(\\\\bw[^\\\\s]+?d)&quot; <\/span><span style=\"font-variant-ligatures: no-common-ligatures;\">replacement pattern<\/span><span style=\"font-variant-ligatures: no-common-ligatures; color: #000000;\"> &quot;cheese&quot;<\/span><\/p>\n<\/div>\n<p>Results in:<\/p>\n<div style=\"background-color:#F0F0F0; padding: 0.5em; margin: 0.5em 0;\">\n<p style=\"margin: 0px; font-stretch: normal; font-size: 12px; line-height: normal; font-family: Verdana;\"><span style=\"font-variant-ligatures: no-common-ligatures;\">&quot;How much cheese cheese a cheesechuck chuck if a cheesechuck could chuck cheese?&quot;<\/span><\/p>\n<\/div>\n<h3>Splitting Text<\/h3>\n<p>Finally, the\u00a0<strong>split text<\/strong> command is used to separate a string into parts based on a plain text delimiter\u00a0<em>or\u00a0<\/em>a complex regular expression pattern. For simplicity, this example just demonstrates how to separate all the components of a string based on white space:<\/p>\n<div style=\"background-color:#F0F0F0; padding: 0.5em; margin: 0.5em 0;\">\n<p style=\"margin-right: 0px; margin-bottom: 0px; margin-left: 83.2px; text-indent: -83.2px; font-stretch: normal; font-size: 12px; line-height: normal; font-family: Verdana;\"><span style=\"font-variant-ligatures: no-common-ligatures;\"><b>set<\/b> <\/span><span style=\"font-variant-ligatures: no-common-ligatures; color: #408000;\">woodstack<\/span><span style=\"font-variant-ligatures: no-common-ligatures;\"> <b>to<\/b> &quot;How much &nbsp;&nbsp;wood&nbsp;&nbsp;&nbsp;&nbsp;would a woodchuck\u00a0 chuck if a woodchuck could chuck wood?&quot;<\/span><\/p>\n<p style=\"  font-size: 12px; line-height: normal; font-family: Verdana; color: #0000ff;\"><span style=\"font-variant-ligatures: no-common-ligatures; color: #000000;\"><b>set<\/b> <\/span><span style=\"font-variant-ligatures: no-common-ligatures; color: #408000;\">splitstack<\/span><span style=\"font-variant-ligatures: no-common-ligatures; color: #000000;\"> <b>to<\/b> <\/span><span style=\"font-variant-ligatures: no-common-ligatures;\"><b>split text<\/b><\/span> <span style=\"font-variant-ligatures: no-common-ligatures; color: #408000;\">woodstack<\/span> <span style=\"font-variant-ligatures: no-common-ligatures;\">matching pattern<\/span><span style=\"font-variant-ligatures: no-common-ligatures; color: #000000;\"> &quot;\\\\s+&quot;<\/span><\/p>\n<\/div>\n<p>Results in:<\/p>\n<div style=\"background-color:#F0F0F0; padding: 0.5em; margin: 0.5em 0;\">\n<p style=\"margin: 0px; font-stretch: normal; font-size: 12px; line-height: normal; font-family: Verdana;\"><span style=\"font-variant-ligatures: no-common-ligatures;\">{&quot;How&quot;, &quot;much&quot;, &quot;wood&quot;, &quot;would&quot;, &quot;a&quot;, &quot;woodchuck&quot;, &quot;chuck&quot;, &quot;if&quot;, &quot;a&quot;, &quot;woodchuck&quot;, &quot;could&quot;, &quot;chuck&quot;, &quot;wood?&quot;}<\/span><\/p>\n<\/div>\n<h3>Beyond Simplicity<\/h3>\n<p>These new commands also support advanced features including the ability to use regular expression matching groups, specify case insensitivity, etc. Open FastScripts&#8217;s own scripting dictionary in your favorite script editor and explore the items in the\u00a0<strong>FastScripts Text Processing <\/strong>suite. Starting with FastScripts 3.1, an easy way to do this is simply to select <strong>FastScripts<\/strong> -&gt; <strong>Open FastScripts Scripting Dictionary<\/strong> from the FastScripts menu.<\/p>\n<h3>Other Changes<\/h3>\n<p>Text processing commands are the &#8220;big deal&#8221; 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:<\/p>\n<ul>\n<li>New built-in scripting support for regular expression commands &#8220;search text&#8221;, &#8220;replace text&#8221; and &#8220;split text&#8221;<\/li>\n<li>Updated &#8220;open web page&#8221; scripting command to support a new &#8220;replacing current page&#8221; parameter<\/li>\n<li>Added a new &#8220;Open FastScripts Scripting Dictionary&#8221; menu item<\/li>\n<li>When searching, recently used scripts are now prioritized in search results<\/li>\n<li>Fix a bug that caused recently added &#8220;invoke&#8221; support for paths to report a spurious error<\/li>\n<li>Fix a bug introduced in 3.0.6 that prevented &#8220;invoke&#8221; from working properly on a script item<\/li>\n<li>Alleviate console log warnings that were being printed when the menu bar icon is clicked<\/li>\n<\/ul>\n<p>If you enjoy FastScripts, please consider spreading the word on <a href=\"https:\/\/facebook.com\/fastscriptsapp\">Facebook<\/a> or <a href=\"http:\/\/twitter.com\/fastscripts\">Twitter<\/a>! Thanks for your support.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>FastScripts 3.1 is now available on the FastScripts home page, or by selecting \u201cCheck for Update\u201d from the FastScripts menu. This update introduces an expansion of FastScripts&#8217;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, [&hellip;]<\/p>\n","protected":false},"author":10,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6,16],"tags":[],"class_list":["post-3822","post","type-post","status-publish","format-standard","hentry","category-fastscripts","category-red-sweater-news"],"_links":{"self":[{"href":"https:\/\/redsweater.com\/blog\/wp-json\/wp\/v2\/posts\/3822","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/redsweater.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/redsweater.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/redsweater.com\/blog\/wp-json\/wp\/v2\/users\/10"}],"replies":[{"embeddable":true,"href":"https:\/\/redsweater.com\/blog\/wp-json\/wp\/v2\/comments?post=3822"}],"version-history":[{"count":15,"href":"https:\/\/redsweater.com\/blog\/wp-json\/wp\/v2\/posts\/3822\/revisions"}],"predecessor-version":[{"id":3837,"href":"https:\/\/redsweater.com\/blog\/wp-json\/wp\/v2\/posts\/3822\/revisions\/3837"}],"wp:attachment":[{"href":"https:\/\/redsweater.com\/blog\/wp-json\/wp\/v2\/media?parent=3822"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/redsweater.com\/blog\/wp-json\/wp\/v2\/categories?post=3822"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/redsweater.com\/blog\/wp-json\/wp\/v2\/tags?post=3822"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}