Auto Fill Problem

Let me start out by saying that I am brand new to using applescript. I am trying to write a real simple apple script that fills out my web-based VPN login screen from work. The problem is that my user id on the web login page is formatted “domain\username”. My problem is that although the variable can be entered in the apple script editor using // for the backslash, I lose the backslash when it places the username in the correct field on the web page. It always outputs “domainusername” instead of “domain\username”. Any help would be greatly appreciated.

tell application “Safari”
open location “https://xxxx.xxxx.xxxx
set fullUserName1 to “domain\username” as text
set password1 to “password”
delay 1

do JavaScript "document.forms[0].elements[2].value =\"" & fullUserName1 & "\"" in document 1
do JavaScript "document.forms[0].elements[3].value = \"" & password1 & \"" in document 1

end tell

Using a double backslash in your AppleScript code gets a single backslash into the string that you program uses. But then you are putting that string into Javascript code, which has a similar requirement for double backslashes. So, to get a single backslash into the Javascript string, you need two backslashes in the Javascript string literal. That means your Applescript string needs two backslashes. To get that, you need to put four backslashes into your original AppleScript string literal. Generally: start with your goal string in your goal environment/language and work your way out, applying the string literal escaping rules for each environment/language on your way out.

To get a double quote into Javascript: Javascript string literal = “"”, AppleScript string literal “"\""”. :o
Or: Javascript string literal = ‘"’ (see the JavaScript string literal reference below), AppleScript string literal = “‘"’”.

If you are dealing with usernames and strings that are not part of your script (e.g. they come from user prompts, are read from a file, or are pulled from a keychain entry), then you will need code to do the escaping that Javascript string literals require (no need for AppleScript string literal escaping, since you already will have each string in a variable). Ideally you should handle nearly all the special characters listed in the Javascript syntax for string liternals. At a minimum you would need to add backslashes before any existing backslashes or double-quotes. If you do not already have a favorite search and replace handler for strings you could use this one: kai’s switchText (with Nigel Garvey’s tweak).

Thank you chrys. That is exactly what I needed:)