Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Help Escaping Escape Characters in AppleScript?

Trying to convert my existing shell script to AppleScript to configure our Macs to Active Directory. Stuck on the following, which appears to be a case of escaping escape characters. Thoroughly lost. Anyone with experience in this? If I remove the space between the double-backslash and escaped quotations to follow, the script fails. Eliminating that space is just what I need to do to get the correct syntax.


set domain to "MYDOMAIN"

set admins to "MYADMINS"


do shell script "dsconfigad -groups " & domain & "\"\\ \"" & admins with administrator privileges


Thanks in advance for any advice here. Feel like this must be something obvious I a missing here.


Cliff

Posted on Aug 28, 2012 7:01 AM

Reply
8 replies

Aug 28, 2012 9:25 AM in response to twtwtw

Thanks. That's a good way at visualizing the result I had not considered. Definitely a quoting issue, which I had seen as well in viewing script events/replies. Unfortunately the result still leaves me puzzled on logistics of how to both escape backslash and ensure proper quoting, since removing the quotes kills ability to escape the the backslash. Seems like a catch 22.


Here's result of above:


dsconfigad -groups MYDOMAIN"\ "MYADMINS


If it helps, absent use of the variables, I have no problem with the following:


do shell script "dsconfigad -groups \"MYDOMAIN\\deptAdmins\"" with administrator privileges

Aug 28, 2012 9:30 AM in response to Clif H

Should also note that I started at trying to define a variable that combined the other two variables, delimited by that backslash. Still hit a wall due to need to escape the backslash:


set domain to "MYDOMAIN"

set admins to "MYADMINS"

setadmingroupstodomain & "\"\\ \"" & admins


Result:

MYDOMAIN\"\\ \"MYADMINS

Aug 28, 2012 9:35 AM in response to Clif H

try:


do shell script "dsconfigad -groups " & quoted form of (domain & "\\" & admins) with administrator privileges


That won't work if there are any wildcard expansions (quoted form of adds single quotes).


As a rule, you only need to escape two characters in applescript: " and \ (escaped as \" and \\). it can get confusing if you look in the events log, though, because the editor displays the code with the escapes.

Aug 28, 2012 9:45 AM in response to twtwtw

No dice. That gets the correct backslash in (good), but appears to be failing due to the need for the entire do shell script needing to be encapsulated in quotes(assuming).


Events/Results:


tell current application


do shell script "dsconfigad -groups 'MYDOMAIN'\\'MYADMINS'" with administrator privileges


--> error "/bin/sh: -c: line 0: unexpected EOF while looking for matching `''

/bin/sh: -c: line 1: syntax error: unexpected end of file" number 2

Result:

error "/bin/sh: -c: line 0: unexpected EOF while looking for matching `''

/bin/sh: -c: line 1: syntax error: unexpected end of file" number 2

Aug 28, 2012 9:56 AM in response to Clif H

You may be over-thinking this, especially if you're looking at the result in your Script Editor.


If a text object contains a \ then the script editor will add a leading backslash when it displays the result, but that doesn't mean it's there - it's only added for display purposes.


Your script should work fine with the right quoting:


do shell script "dsconfigad -groups 'MYDOMAIN\\MYADMINS'" with administrator privileges

Note the single quotes around the entire domain specifier. If using variables, just substitute in the appropriate places:


do shell script "dsconfigad -groups '" & domainName & "\\" & groupName & "'" with administrator privileges


(Note where the single quotes are)

Aug 28, 2012 10:18 AM in response to Camelot

Thanks so much. I was quite sure I was overthinking the script and that was a big reason why I posted back here for 2nd/3rd perspectives. Got hung up on assumption I had to wrap end of do shell script with a double quote, since the first example you have above is where I started from. Need to do more homework on how and when to use double vs single quoting, as that was an angle I completely overlooked. Thanks again.

Help Escaping Escape Characters in AppleScript?

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.