{"id":26,"date":"2005-08-24T09:51:13","date_gmt":"2005-08-24T16:51:13","guid":{"rendered":"http:\/\/www.red-sweater.com\/blog\/?p=26"},"modified":"2005-08-24T11:20:50","modified_gmt":"2005-08-24T18:20:50","slug":"rock-the-funky-finder-regex","status":"publish","type":"post","link":"https:\/\/redsweater.com\/blog\/26\/rock-the-funky-finder-regex","title":{"rendered":"Rock the Funky Finder Regex"},"content":{"rendered":"<p>I&#8217;m on a bit of a productivity kick lately. I&#8217;m trying to listen to the little voice in my head that speaks up when I&#8217;m doing something incredibly tedious and error-prone, and could clearly be automated.<\/p>\n<p>\nSome people would say that spending 3 hours cranking out a script that might save you at most a minute or two each time you use it is the opposite of productivity, but I beg to differ.  Yes, it may take a while to fully earn back the time I spend on such modest life improvements, but the little changes often clear the way for bigger changes down the road.\n<\/p>\n<p>\nHow many of you have ever stared at a folder in the Finder, teeming with files of varying types and names, and wished you could easily <i>select by regular expression<\/i>? Well, your reward for reading this blog is that as of today, you can!\n<\/p>\n<p>\nI was inspired by a project of mine whose directory hierarchy was &#8220;out of control.&#8221; Since the project started out small, I had colocated all of the source files, images, documentation, reference materials, etc., in one directory. Now I&#8217;m looking at it and thinking, &#8220;gosh, I&#8217;d like to at least put all the source files in a sub-folder, but it&#8217;s drag to select every &#8216;.m&#8217; and &#8216;.h&#8217; file by hand.&#8221;<\/p>\n<p>\nAt first I tried to accomplish the task in AppleScript alone. Among the many things missing from AppleScript is a built-in regular expression engine, so I knew I&#8217;d have to either install a scripting component, or resort to Mac OS X&#8217;s built-in command line tools. My script started out as an AppleScript that merely called out to perl (via &#8220;do shell script&#8221;) to handle the regular expression parsing:\n<\/p>\n<p><img decoding=\"async\" src=\"http:\/\/www.red-sweater.com\/blog\/images\/RegExSlow.png\"\/><\/p>\n<p>\nThis worked great, but as expected, those repeated &#8220;do shell script&#8221; calls dogged performance <i>big time<\/i>. The point of this is to automate a task so that it happens in the blink of an eye. If I have to wait for a long pause, then I&#8217;ve just replaced one frustration with another!\n<\/p>\n<p>\nHow to avoid the delay of the repeated &#8220;do shell script&#8221; commands? I thought about spitting out the names of the files to a temporary file, and running perl just once against that file. Yeah &#8211; complicated and I don&#8217;t like to use temporary files when it&#8217;s possible to avoid them.\n<\/p>\n<p>\nI decided it was time to approach the problem from the other angle. There&#8217;s no reason I can&#8217;t write the script in Perl and just call out to AppleScript as necessary to interface with the Finder. An added benefit is that I can easily use the Perl script <b>from the command line or a script menu<\/b>. I set the script up as a FastScripts Finder-specific script, and applied the keystroke &#8220;Cmd-Opt-S&#8221; as a shortcut. Now when I&#8217;m looking at a window like this:\n<\/p>\n<p><img decoding=\"async\" src=\"http:\/\/www.red-sweater.com\/blog\/images\/RegExBefore.png\" width=\"600\"\/><\/p>\n<p>\nI just hit Cmd-Opt-S to bring up this dialog, enter a regular expression:\n<\/p>\n<p><img decoding=\"async\" src=\"http:\/\/www.red-sweater.com\/blog\/images\/RegExDialog.png\"\/><\/p>\n<p>\nAnd end up with this!\n<\/p>\n<p><img decoding=\"async\" src=\"http:\/\/www.red-sweater.com\/blog\/images\/RegExAfter.png\" width=\"600\"\/><\/p>\n<p>\nCan&#8217;t wait to get your hands on this? Feel free to download a copy of &#8220;Select Regular Expression&#8221; from the <a href=\"http:\/\/www.red-sweater.com\/AppleScript\/\">Red Sweater scripts page<\/a>. Enjoy!<\/p>\n<p>\n<b>Update: <\/b><a href=\"http:\/\/www.red-sweater.com\/AppleScript\/SelectRegularExpression.zip\">Version 1.0.1<\/a> of the script saves and restores the regular expression you enter, so the dialog is always pre-populated with the last string you tried.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;m on a bit of a productivity kick lately. I&#8217;m trying to listen to the little voice in my head that speaks up when I&#8217;m doing something incredibly tedious and error-prone, and could clearly be automated. Some people would say that spending 3 hours cranking out a script that might save you at most a [&hellip;]<\/p>\n","protected":false},"author":10,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7,6],"tags":[],"class_list":["post-26","post","type-post","status-publish","format-standard","hentry","category-applescript","category-fastscripts"],"_links":{"self":[{"href":"https:\/\/redsweater.com\/blog\/wp-json\/wp\/v2\/posts\/26","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=26"}],"version-history":[{"count":0,"href":"https:\/\/redsweater.com\/blog\/wp-json\/wp\/v2\/posts\/26\/revisions"}],"wp:attachment":[{"href":"https:\/\/redsweater.com\/blog\/wp-json\/wp\/v2\/media?parent=26"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/redsweater.com\/blog\/wp-json\/wp\/v2\/categories?post=26"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/redsweater.com\/blog\/wp-json\/wp\/v2\/tags?post=26"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}