Friday, August 21, 2009

Using regular expressions in Applescript

I finally got around to fix the issue with escaped spaces in windows paths, se previous post.

This required some search and replace in strings, which oddly enough isn't implemented in Applescript. But since Applescript can run any shell command, this is easily circumvented. I use the "sed" command, as follows:
set escapedPath to do shell script "echo " & quoted form of MacPathString & " | sed 's/ /\\\\ /g'"

The syntax is "sed s/find/replacement/g source", where g is the regexp global flag. The "|" is the unix "pipe" operator which forwards previous output to the following command, i.e. the "echo" command results are piped into "sed".
blog comments powered by Disqus