I am trying to automate parsing a website (secured) and setting up my word document with pre-populated information. I considered 3 steps:
- Extracting info from the page (I don't control it and its dynamically generated)
- Passing this information to from safari to applescript
- 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