MKK
MKK

Reputation: 2753

How can I execute JavaScript function in AppleScript?

I tried this apple script with Script Editor. But it doesn't take me to the page where it usually goes when it's clicked manually. It seems doing nothing :(

do JavaScript "doCallerPage('FORM_PAGE', 'FR_AN', '/as/an/Check.do');return false;" in document 1

The Link in HTML source

<a id="LNK0001" href="javascript:void(0);" onclick="doCallerPage('FORM_PAGE', 'FR_AN', '/as/an/Check.do');return false;">Check Page</a>

JavaScript Function

function doCallerPage(formName, target, actionPath, _action, _t) {
        if (actionPath == "") {
            return false;
        }

        if (target && target != "_self") {
            checkSubWindow(target);
        }
        if (document.FORM_PAGE.ctrlflg.value == "") {
            if (_action) {
                document.FORM_PAGE._ACTION.value = _action;
            }
            else if (!_action) {
                document.FORM_PAGE._ACTION.value = "";
            }
        }
        if (_t) {
            document.FORM_PAGE._T.value = _t;
        }
        else {
            document.FORM_PAGE._T.value = "";
        }
        document.FORM_PAGE.action = actionPath;
        document.FORM_PAGE.target = target;
        document.FORM_PAGE.submit();
        document.FORM_PAGE._P.value = "";
        document.FORM_PAGE._T.value = "";
        document.FORM_PAGE._ACTION.value = "";
        document.FORM_PAGE.ctrlflg.value = "";
}

Upvotes: 1

Views: 3227

Answers (1)

roberthuttinger
roberthuttinger

Reputation: 1194

The syntax is a little different and it has to be javascript. Using jQuery on Chrome can be done like so:

tell application "Google Chrome" activate set jq to do shell script "curl https://code.jquery.com/jquery-latest.min.js" execute front window's active tab javascript jq execute front window's active tab javascript "jQuery('body').css({'color':'blue'});" end tell

Vanilla JS:

tell application "Google Chrome" activate execute front window's active tab javascript "if (window.console) console.log( 'hi' );" end tell

You can be doing other things in other applications while this is happening the script will execute as long as the browser window is left as is until the script is complete.

Upvotes: 2

Related Questions