0

I am trying to automate parsing a website (secured) and setting up my word document with pre-populated information. I considered 3 steps:

  1. Extracting info from the page (I don't control it and its dynamically generated)
  2. Passing this information to from safari to applescript
  3. Using search and replace with my template

I got the first par to work and the extraction script works. I needed to attach jquery (site doesn't use it) and I know I call and attach it twice, but I don't know how to pass one parameter from javascript to applescript, let alone 2.

Anyways, my problem is I get empty variables in applescript, but the javascript portion works very well (I tested and it gets the right values).

Thanks for any tips on how to get the javascript values that findMe returns to an applescript variable and if you can let me also know how to simplify the script to one call with 2 arguments that would be awesome too.

to getClaimFile(needle)

tell application "Safari"
    set input to do JavaScript "


    (function () {

            function loadScript(url, callback) {

            var script = document.createElement('script')
         script.type = 'text/javascript';

        if (script.readyState) { //IE
                script.onreadystatechange = function () {
                 if (script.readyState == 'loaded' || script.readyState == 'complete') {
                             script.onreadystatechange = null;
                            findMe('" & needle & "')
                    }
                };
        } else { //Others
        script.onload = function () {
    findMe('" & needle & "')
        };
    }

    script.src = url;
    document.getElementsByTagName('head')[0].appendChild(script);
}

loadScript('https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js',     function () {

     //jQuery loaded
     console.log('jquery loaded');

});

})();





function findMe(squery) {

        return $('iframe#app_win').contents().find('iframe#app_subwin').contents().find('label:contains('+squery+')').parent().next('td').text()
}
" in current tab of window 1
end tell
return input

end getClaimFile

set claimNum to getClaimFile("Claim #:")
set fileNum to getClaimFile("File Number:")
log claimNum
log fileNum

1 Answer 1

1

Returning values in AppleScript is funky. You don't specify the 'return' statement as you would in JavaScript; instead, the last variable in your statement is simply returned as-is.

Here is a simplified version of what you're trying to do:

  1. Create a function that executes JavaScript and returns a value
  2. Grab the return value of the function and use it in your AppleScript

to getTagName(searchterm)

    tell application "Safari"
        set myreturnvalue to do JavaScript "
        var tags = document.querySelectorAll('." & searchterm & "');
        var tag = (tags.length) ? tags[0].innerHTML : 'not found in JS'; 
        tag; " in tab 1 of window 1
    end tell

myreturnvalue

end getTagName

set myrealtag to getTagName("post-tag")
log myrealtag

set myfaketag to getTagName("foobar")
log myfaketag

If you run this while visiting a StackOverflow page, the first test (myrealtag) will return the name of the first element with the class .post-tag found on the page. The second test (foobar) uses a search string that won't be found on the page, and therefore will return "not found in JS" (as instructed by the JS snippet above).

There is a small cascade of return values at play here. The getTagName AppleScript function is configured to return myreturnvalue. myreturnvalue is a variable that gets populated by the JavaScript. The JavaScript is configured to return the value of tag.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.