use AppleScript version "2.4" -- OS X 10.10 (Yosemite) or later use framework "Foundation" use scripting additions on braceExpansion(textForExpansion) -- Massage the text to pass to a shell script: - -- Single-quote it to render everything in it initially immune from brace and file-system expansion. -- Include a return at the end to get a new line at the end of each eventual expansion. set textForExpansion to quoted form of (textForExpansion & return) -- Switch to ASObjC text for a couple of regex substitutions. set textForExpansion to current application's class "NSMutableString"'s stringWithString:(textForExpansion) -- Increase the escape level of every instance of /two/ backslashes (represented by eight in the -- AppleScript string for the search regex) and isolate each result inside its own quote marks. tell textForExpansion to replaceOccurrencesOfString:("\\\\\\\\") withString:("''$0$0''") ¬ options:(current application's NSRegularExpressionSearch) range:({0, its |length|()}) -- UNquote every run of braces and/or commas not now immediately preceded by a backslash. tell textForExpansion to replaceOccurrencesOfString:("(?