mercredi 25 novembre 2015

Applescript, click checkbox with do JavaScript

I'm trying to check or uncheck a checkbox using do JavaScript within an AppleScript. The checkbox appears within an iframe named mainFrame and within a form named postForm... here's the checkbox:

<input class="normal" id="apples" name="applesChk" value="<1>" style="visibility: visible" onclick="checkChk(this.checked, 1);" type="checkbox">

And here's my AppleScript... this'll be a little embarrassing:

tell application "System Events"
    tell process "Safari"

        set windowNumber to 1
        repeat the number of windows times
            if "(item)" is in name of window windowNumber then
                set tempWindowName to name of window windowNumber
            else
                set windowNumber to windowNumber + 1
            end if
        end repeat

    end tell
end tell

tell application "Safari"
    do JavaScript "document.['apples'].click()" in window tempWindowName
    do JavaScript "document.getElementsById('apples')[0].click()" in window tempWindowName
    do JavaScript "document.forms['postForm']['apples'].checked = true" in window tempWindowName
    do JavaScript "document.frames['mainFrame']['apples'].checked = true" in window tempWindowName
    do JavaScript "document.frames['mainFrame'].forms['postForm']['apples'].checked = true" in window tempWindowName
end tell

I get the window by looking for window names containing a certain phrase, then I work with that. Then you can see my five attempts at checking the box. I've really struggled to follow other examples successfully here... any advice appreciated, where am I going wrong? Thanks in advance.




Aucun commentaire:

Enregistrer un commentaire