Using JavaScript in AppleScript

So after attempting to check specific boxes on an internal work website (using Safari), It seemed unreliable to use strictly AppleScript to do it and I was referred to explore Javascript.

With the help of a friend who knows Javascript, I was able to inspect the element of the website, then paste in the below javascript in the Console menu, which works to check off the boxes needed! (a long list of "last name, first name"s)

But now, how would I even begin to merge the javascript into my existing AppleScript so I don’t have to paste into the website inspector Console?

$(document).ready(function() {

var arr = ["Last1, First1","Last2, First2","Last3, First3"];
 
jQuery.each( arr, function( i, val ) {  
  jQuery('span[title*="' + val + '"]').closest('.tree-node').find(':input').click();
});

});

I know nothing about Javascript and my friend knows nothing about AppleScript.

Any guidance would be appreciated

I don’t have a website your code would work on, so I can’t test.

But this works for me:

tell application "Safari" to tell the front document to set theText to do JavaScript "document.body.innerText"

So I’m guessing it’s just:

tell application "Safari" to tell the front document
do JavaScript "
$(document).ready(function() {

var arr = [\"Last1, First1\",\"Last2, First2\",\"Last3, First3\"];
 
jQuery.each( arr, function( i, val ) {  
  jQuery('span[title*=\"' + val + '\"]').closest('.tree-node').find(':input').click();
});

});
"
end tell

Lost in the server migration, TheREDNAVE had asked about using an Applescript variable containing the result of a “Choose From List” for the Javascript variable “arr.”

The problem was that an Applescript list can not be plugged in directly in Javascript code to become a Javascript list.

To Applescript, the Javascript code is all just text. When using an “&” to concatenate the variable returned from the Choose From List to the Javascript code, the Applescript list is coerced to text, and that text is not the Javascript code you want.

In Javascript, the variable “arr” is set to:

["Last1, First1","Last2, First2","Last3, First3"]

But the Applescript list as text is:

{“Last1, First1”, “Last2, First2”, “Last3, First3”}

You need to plug in exactly the text that the Javascript would expect as code.

Again, I can’t test with the web page, but I think this should work:

use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions

set ASNamesList to choose from list {"Last1, First1", "Last2, First2", "Last3, First3"} with multiple selections allowed
set JSNamesList to AS_list_to_JS_list(ASNamesList)

tell application "Safari" to tell the front document
	do JavaScript "
$(document).ready(function() {

var arr = " & JSNamesList & ";
 
jQuery.each( arr, function( i, val ) {  
  jQuery('span[title*=\"' + val + '\"]').closest('.tree-node').find(':input').click();
});

});
"
end tell

on AS_list_to_JS_list(ASlist)
	set itemCount to count of ASlist
	set JSlist to "[\""
	repeat with i from 1 to itemCount
		set currentItem to item i of ASlist
		if i ≠ itemCount then
			set JSlist to JSlist & currentItem & "\", \""
		else
			set JSlist to JSlist & "\"]"
		end if
	end repeat
	return JSlist
end AS_list_to_JS_list

t.spoon
Testing reply from my end to see what happens in this post

edit: seems to not give me an error. if it continues I am making live changes to the site as a whole after a test code snippets. If you continue to get errors please try emptying your cache( I know this is a pain to continually do as I do this during development to keep css styles from caching in my browser)

FWIW, I think this is something new in Sierra. At least, the reverse is true – you can’t return an array/list from js in Sierra, although you could in previous versions of the OS. IOW, it looks like a bug.