Sunday, November 19, 2017

#1 2017-07-10 10:51:31 am

TheREDNAVE
Member
Registered: 2015-11-21
Posts: 38

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?

Applescript:

$(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

Offline

 

#2 2017-07-11 12:18:00 pm

t.spoon
Member
From:: BFE, Massachusetts
Registered: 2013-01-13
Posts: 206

Re: Using JavaScript in AppleScript

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

But this works for me:

Applescript:

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

So I'm guessing it's just:

Applescript:

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


Hackintosh built February, 2012 |  Mac OS Sierra
GIGABYTE GA-Z68X-UD3H-B3 | Core i5 2500k | 16 GB DDR3 | GIGABYTE Geforce 1050 TI 4GB
250 GB Samsung 850 EVO | 4 TB RAID
Dell Ultrasharp U3011 | Dell Ultrasharp 2007FPb

Offline

 

#3 2017-07-12 10:38:10 am

t.spoon
Member
From:: BFE, Massachusetts
Registered: 2013-01-13
Posts: 206

Re: Using JavaScript in AppleScript

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:


Applescript:

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

Last edited by t.spoon (2017-07-12 10:45:26 am)


Hackintosh built February, 2012 |  Mac OS Sierra
GIGABYTE GA-Z68X-UD3H-B3 | Core i5 2500k | 16 GB DDR3 | GIGABYTE Geforce 1050 TI 4GB
250 GB Samsung 850 EVO | 4 TB RAID
Dell Ultrasharp U3011 | Dell Ultrasharp 2007FPb

Offline

 

#4 2017-07-12 10:58:27 am

odomtech
Administrator
Registered: 2017-06-30
Posts: 11

Re: Using JavaScript in AppleScript

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)

Offline

 

#5 2017-07-12 07:07:59 pm

Shane Stanley
Member
From:: Australia
Registered: 2002-12-07
Posts: 5175

Re: Using JavaScript in AppleScript

t.spoon wrote:

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



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.


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/

Online

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)