Cant copy this specific String to a Safari text box

The script shown below takes an ID followed by text from a txt file. It creates list of list {{ID1,text1},{ID2,text2},…}
Then it takes the ID and copies it to Safari. Then it takes the text and copies it to Safari. My problem is that the script works fine in every part, except the text fetched “Tdesc” doesnt get copied to the text field in Safari. if I replaced it by a string it gets copied and if I display “Tdesc” in the results in Applescript it gets displayed. However the “Tdesc” variable doesnt get copied I dont know why.
Here is the text file:https://www.dropbox.com/s/v1yzl1qhyoe312f/description.txt?dl=0


on run
set theText to read file ((path to desktop as text) & "description.txt") as «class utf8» -- replace the HFS path with the actual path
set {TID, text item delimiters} to {text item delimiters, ("~~" & linefeed)}
    set theMatches to text items of theText
    set text item delimiters to TID
    set theResult to {}
    repeat with aMatch in theMatches
        if length of aMatch > 1 then
        tell aMatch
            set end of theResult to {text 1 thru 4, text 7 thru -1}
        end tell
        end if
    end repeat
    repeat with theItem in theResult
        set TID to theItem's first item as integer
        set Tdesc to theItem's second item as string
        inputByName("search_keyword", 0, TID)
        delay 2
        clickName("submit1", 0)
        delay 3
        clickClassName("ico edit", 0)
        delay 3
        inputByName("eng_description", 0, Tdesc)
        delay 2
        clickName("submit1", 0)
        repeat
            tell application "System Events" to tell process "Safari"
                if exists (button "OK" of front window) then
                    click (button "OK" of front window)
                    exit repeat
                end if
            end tell
        end repeat
        delay 2
    end repeat
end run

to inputByName(theName, num, theValue)
    tell application "Safari"
        do JavaScript "
          document.getElementsByName('" & theName & "')[" & num & "].value ='" & theValue & "';" in document 1
    end tell
end inputByName

to clickID(theId)
    tell application "Safari"
        do JavaScript "document.getElementById('" & theId & "').click();" in document 1
    end tell
end clickID

to clickName(theName, elementnum)
    tell application "Safari"
        do JavaScript "document.getElementsByName('" & theName & "')[" & elementnum & "].click();" in document 1
    end tell
end clickName

to clickClassName(theClassName, elementnum)
    tell application "Safari"
        do JavaScript "document.getElementsByClassName('" & theClassName & "')[" & elementnum & "].click();" in document 1
    end tell
end clickClassName

Hi seifkhalil. Welcome to MacScripter.

Quite a few of your Tdesc values contain apostrophes, which probably needs to be allowed for when enquoting them in the JavaScript command. Unfortunately, I don’t know how you’d do that in JavaScript.

By the way, the preferred tags when posting AppleScript code in this forum are [applescript] and [/applescript]. I think they’re unique to this site. They cause the code to be displayed with a clickable link which opens it in people’s default script editors, like this:

on run
	set theText to read file ((path to desktop as text) & "description.txt") as «class utf8» -- replace the HFS path with the actual path
	-- etc.
end run

Does anyone know how could I manage the text conatining apostrophe (') by javascript, to be written in a textfield in Safari?

Hi seifkhalil.

I’ve found this page on Wikipedia this morning, which has a section about dealing with nested quotes in JavaScript. According to this, you should be able to escape the apostrophe characters in the texts by placing backslashes in front of them, in the same way that characters are escaped in AppleScript strings. To represent a backslash character in the AppleScript string containing the JavaScript command, the backslash itself has to be escaped with another backslash. Thus: “\”.

For your purposes, the escaping is probably best done in your inputByName() handler:

to inputByName(theName, num, theValue)
	if (theValue's class is text) then -- theValue's an integer on alternate calls!
		-- Escape any apostrophes or single quotes in the text value.
		set astid to AppleScript's text item delimiters
		set AppleScript's text item delimiters to "'"
		set theValue to theValue's text items
		set AppleScript's text item delimiters to "\\'"
		set theValue to theValue as text
		set AppleScript's text item delimiters to astid
	end if
	
	tell application "Safari"
		do JavaScript "
document.getElementsByName('" & theName & "')[" & num & "].value ='" & theValue & "';" in document 1
	end tell
end inputByName

Your texts also contain double quotes, but they don’t need special treatment here.

I hope this works for you, but do post again if it doesn’t.

It didnt work :frowning:
But thank you anyways Nigel!

Hmm. Looking again at your text file, I see that some of the apostrophes are already escaped, some of them are double-escaped, and some are rendered as the HTML entity “'”. Have the various parts of it been obtained from different sources?

Maybe this version will be more successful. It catches apostrophes preceded by three backslashes, apostrophes preceded by one backslash, apostrophes not preceded by three or one backslashes, and HTML entities representing apostrophe characters. It may not work with future documents if they contain even longer runs of backslashes. It may then be better to use regex rather than text item delimiters:

to inputByName(theName, num, theValue)
	if (theValue's class is text) then -- theValue's an integer on alternate calls!
		-- Escape any apostrophes or single quotes in the text value.
		set astid to AppleScript's text item delimiters
		-- Some apostrophes may already be escaped or double-escaped, or may be HTML entities.
		-- To make sure they're all caught, arrange the search delimiters in this order.
		-- The HTML entity is in concatenated instalments here for Web site posting!
		set AppleScript's text item delimiters to {"\\\\\\'", "\\'", "'", "&" & "#39;"}
		set theValue to theValue's text items
		set AppleScript's text item delimiters to "\\'"
		set theValue to theValue as text
		set AppleScript's text item delimiters to astid
	end if
	
	tell application "Safari"
		do JavaScript "
document.getElementsByName('" & theName & "')[" & num & "].value ='" & theValue & "';" in document 1
	end tell
end inputByName

Edit: Removed spurious line caused by bad edit.

This gave me errors around “theVto”. Can you please Nigel look at it and tell me if something needs to be changed.
Thank you.

Hi Seifkhalil.

I do apologise! The line containing “theVto” shouldn’t be there. It seems to have been caused by a bad edit while I was posting. I didn’t notice it until I read the error you reported.

I’ve now deleted it in my post (post #6) above and the handler shown is what I meant to post.

Again. Apologies for wasting your time. I hope the script works after all that .!