Search code examples
shellapplescriptcurly-braces

AppleScript throwing errors on curly braces in string


I'm trying to generate a shell script in AppleScript. When using the following pattern:

set tDur to "PT1H21M7S"
set tSh to "echo '" & tDur & "' | egrep -o '[0-9]+M' | sed s/M// | sed -e :a -e s/^.\{1,1\}$/0&/;ta"

...I get the error:

Expected “"” but found unknown token.

The cursor is landing on the first curly braces after the error is thrown, and I can escape the curly braces again, e.g.:

\\{1,1\\}

... but both escape characters end up in my shell script, which is bad.

If I remove the final sed statement:

set tSh to "echo '" & tDur & "' | egrep -o '[0-9]+H' | sed s/H//"

... AppleScript will accept the string. I've tried various configurations of AppleScript's quoted form of property (per Apple's documentation), but to no avail.

edit: working shell command I'm trying to get AppleScript to generate:

echo 'PT1H2M21S' | egrep -o '[0-9]+M' | sed s/M// | sed -e :a -e 's/^.\{1,1\}$/0&/;ta'

edit: also added the setting of the tDur variable for clarity.

Any help appreciated!


Solution

  • set tSh to "echo " & (quoted form of tDur) & " | egrep -o '[0-9]+H' | sed s/H//"
    

    If you're on 10.10, consider using NSTask via the AppleScriptObjC bridge. Unlike do shell script, it allows you to pass data via STDIN, which is the right way to do these things. (Though frankly none of this crap would be necessary in the first place if AppleScript had a standard library worth a damn.)