Date Conversion works on a stand alone basis but not as part of a larger routine

If I run the following routine by itself

this is the log result

(*WU240621C00010000*)
(*C, 1, 2, 6, 0, 4, 2*)
(*C126042*)

When I run it as part of a process
this is the result
(*WU240621C00010000*)
(*C, 1, 2, 6, 0, 4, 2*)
(*C",“1”,“2”,“6”,“0”,“4”,"2*)

I need to be able to manipulate the last line

This is the code when runs as a standalone which is of course the same when run as part of the larger routine

my GetOptionDate()

on GetOptionDate()
	tell application "Safari"
		activate
		set TableData to do JavaScript ¬
			"var secondTable = document.querySelectorAll('table')[0];
if (secondTable) {var rows = secondTable.getElementsByTagName('tr');
var tableData = [];for (var i = 0; i < rows.length; i++) {var rowData = [];
var cells = rows[i].getElementsByTagName('td');for (var j = 0; j < cells.length; j++) {
rowData.push(cells[j].innerText);}tableData.push(rowData);}tableData;}" in document 1
		log item 1 of item 2 of TableData
		set contractdate to items 9 thru 15 of (reverse of items of item 1 of item 2 of TableData)
		log contractdate
		log contractdate as string
	end tell
end GetOptionDate

Why are all the commands surrounded by asterisks?
Please learn how to post code.

Also, what web page is currently in Safari when you run this?

Hi @MitchBVI.

I’ve tidied up your post for you. AppleScript code needs to be posted as plain text, not with AppleScript formatting, otherwise MacScripter’s Discourse software puts asterisks around things.

Conversely, when you put single asterisks in the non-code portion of a post, such as where you listed your log results, Discourse renders what follows as italic text. So the asterisks have to be escaped when you’re posting the text:

(\*WU240621C00010000\*)

They’re then rendered correctly in the formatted post.

With regard to your script problem, the only thing I can think of off hand is that you’re coercing the list contractdate to string without setting AppleScript’s text item delimiters to “” first. If they happen to be set to “\”,\“” when the coercion’s done, the exact log result you’re seeing will be produced.

1 Like

It works for me, but I don’t have the same website URL in Safari as you, I suspect.

Nigel’s explanation seems most likely:

Sorry for the delay in replying and thank you all, I apologize for the incorrect posting style hopefully this is now correct. With a lot of help I her now got it working it still seems clumsy .

on GetOptionDate(TableData)
	tell application "Safari"
		activate
		--log item 1 of item 2 of TableData
		set rawContractDateList to items 9 thru 15 of (reverse of items of item 1 of item 2 of TableData)
		--log "Raw contractdate: " & rawContractDateList
		set cleanContractDate to my cleanList(rawContractDateList)
		--log "Cleaned contractdate: " & cleanContractDate
		set contractdateString to my concatenateList(cleanContractDate)
		log "Formatted contractdate: " & contractdateString
		set OptType to item 1 of cleanContractDate -- Extracting option type
		return {contractdateString, OptType}
	end tell
end GetOptionDate

on cleanList(myList)
	set cleanedList to {}
	repeat with anItem in myList
		set end of cleanedList to (anItem as string)
	end repeat
	return cleanedList
end cleanList

on concatenateList(myList)
	set concatenatedString to ""
	repeat with anItem in myList
		set concatenatedString to concatenatedString & anItem
	end repeat
	return concatenatedString
end concatenateList

Hi @MitchBVI.

Is this any more satisfying? I’ve assumed that item 1 of item 2 of TableData is a piece of text, in which case it’s more informative in the code to extract its ‘characters’ rather than its ‘items’. (The result’s exactly the same, but the code’s more self-explanatory.) It’s also possible to extract just the characters you need and reverse those, rather than getting all the characters, reversing the lot, and extracting what you want from the result.

The listToText() handler I’ve included sets AppleScript’s text item delimiters to a specified value (an empty text in this case), coerces the given list to text with this value as the delimiter, and restores the previous delimiter value before returning the text.

tell application "Safari"
	activate
	set TableData to do JavaScript ¬
		"var secondTable = document.querySelectorAll('table')[0];
if (secondTable) {var rows = secondTable.getElementsByTagName('tr');
var tableData = [];for (var i = 0; i < rows.length; i++) {var rowData = [];
var cells = rows[i].getElementsByTagName('td');for (var j = 0; j < cells.length; j++) {
rowData.push(cells[j].innerText);}tableData.push(rowData);}tableData;}" in document 1
end tell

GetOptionDate(TableData)

on GetOptionDate(TableData)
	set rawContractDateList to (reverse of characters -15 thru -9 of item 1 of item 2 of TableData)
	set contractDateString to listToText(rawContractDateList, "")
	set OptType to character 1 of contractDateString -- Extracting option type
	return {contractDateString, OptType}
end GetOptionDate

on listToText(myList, delim)
	set astid to AppleScript's text item delimiters
	set AppleScript's text item delimiters to delim
	set txt to myList as text
	set AppleScript's text item delimiters to astid
	return txt
end listToText