1

I have a program I'm using to write xml with variables, and it passes through AppleScript and bash to form the final xml. However, one of my variables usually begins with "024", "054", or other three digit codes that paired with the "\" forms an octal character, like "," for "\054". Once they are passed through bash, they become replaced with these octal characters. These variables need to stay the way they are, because they are part of a file path the xml ultimately pulls from. Unfortunately, I can't change these variables because they're linked to many other things working properly, and the backslashes are obligatory too. I want to know if there is a way to stop AppleScript from implementing octal encoding on this script as it passes through. A colleague recommended using iconv, which I tried unsuccessfully, partially because I'm not sure how to use it properly. Here is the AppleScript for reference:

do shell script "echo \"<?xml version=\\\"1.0\\\" encoding=\\\"UTF-8\\\"?> <scratch project=\\\"TEST\\\"> <file>W:\\Sequences\\087_TLD_TL_Taking_The_Lead\\Shots\\TLD0640\\Versions\\MOV\\TLD0640_v0173.mov\\</file> </scratch>\" > ~/Desktop/Test.xml"

And the output:

<?xml version="1.0" encoding="UTF-8"?> <scratch project="TEST"> <file>W:\Sequences87_TLD_TL_Taking_The_Lead\Shots\TLD0640\Versions\MOV\TLD0640_v0173.mov\</file> </scratch>

Thank you for your help!

Edit: thanks Gordon Davisson for making the code viewable, and updated the codes to the bare minimum for reproducing the question.

  • It'd help a lot if you used code format (either indent code sections four spaces or enclose snippets in backticks) -- I tried to convert your question, but I'm not sure I got it right (it gives me an error in Script Editor). Also, please produce a minimal, reproducible example. That is, try to remove everything except what's necessary for someone else to reproduce the specific problem you're seeing, but leave in everything that's necessary. – Gordon Davisson Jul 12 at 2:03
  • Here is a minimal, reproducible example. do shell script "echo \"<?xml version=\\\"1.0\\\" encoding=\\\"UTF-8\\\"?> <scratch project=\\\"TEST\\\"> <file>W:\\Sequences\\087_TLD_TL_Taking_The_Lead\\Shots\\TLD0640\\Versions\\MOV\\TLD0640_v0173.mov\\</file> </scratch>\" > ~/Desktop/Test.xml" – Brandon Steenhoek Jul 12 at 2:08
1

It looks like do shell script is running the script under the sh shell, which is actually bash in compatibility mode; in this mode, the builtin echo command apparently thinks it needs to interpret some escape (backslash) sequences. This is one of several features of echo that aren't consistent between versions/modes/etc; as the POSIX standard for echo says, "It is not possible to use echo portably across all POSIX systems unless both -n (as the first argument) and escape sequences are omitted."

So, my first recommendation is to use printf '%s\n instead of echo (and the backslash will need to be doubled to get it past AppleScript):

do shell script "printf '%s\\n' \"<?xml version=\\\"1.0\\\" encoding=\\\"UTF-8\\\"?> <scratch project=\\\"TEST\\\"> <file>W:\\Sequences\\087_TLD_TL_Taking_The_Lead\\Shots\\TLD0640\\Versions\\MOV\\TLD0640_v0173.mov\\</file> </scratch>\" > ~/Desktop/Test.xml"

But even with echo getting into the act, you still have multiple confusing levels of quotes and escapes due to interpretation by AppleScript and the shell. I'd recommend storing the XML as an AppleScript variable (using just a single level of quoting/escaping, for consumption by AppleScript), then using quoted form of to quote/escape that for use in the shell command:

set XML_Content to "<?xml version=\"1.0\" encoding=\"UTF-8\"?> <scratch project=\"TEST\"> <file>W:\\Sequences\\087_TLD_TL_Taking_The_Lead\\Shots\\TLD0640\\Versions\\MOV\\TLD0640_v0173.mov\\</file> </scratch>"

do shell script "printf '%s\\n' " & (quoted form of XML_Content) & " > ~/Desktop/Test.xml"

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .