toderov
toderov

Reputation: 11

Using Applescript and Javascript: get checkbox IDs from webpage, loop over, if end matches string click checkbox

I'm trying to use applescript and javascript to click all the checkboxes on https://calendar.google.com/calendar/syncselect whos element IDs end with the relevant domain.
ElementID example is "[email protected]"

This works when you know the exact ID and can specify it first:

tell application "Safari"
    open location "https://calendar.google.com/calendar/syncselect"
    delay 3
    do JavaScript "
    document.getElementById('[email protected]').click();
    " in document 1
end tell

This is a recycled attempt at trying to loop:

to goToWebPage(theWebPage)
    tell application "Safari"
        activate
        set URL of document 1 to theWebPage
    end tell
end goToWebPage

to clickID(theId) --creates a function that we can use over and over again instead of writing this code over and over again

    tell application "Safari" -- lets AppleScript know what program to controll

        do JavaScript "document.getElementById('" & theId & "').click();" in document 1 -- performs JavaScript code that clicks on the element of a specific id

    end tell -- tells Applescript you are done talking to Safari

end clickID -- lets AppleScript know we are done with the function

goToWebPage("https://calendar.google.com/calendar/syncselect")
clickID("[email protected]")

based on the example: http://www.cubemg.com/how-to-click-a-button-on-a-web-page-with-applescript/

Basically the list of calendars to sync will change so I would like to make an array/list of all the checkbox IDs. Loop through them, if any of the IDs ends with domain.com. click that box.

Upvotes: 0

Views: 220

Answers (1)

toderov
toderov

Reputation: 11

Found a solution this morning thanks for the documentation here: https://developer.mozilla.org/en-US/docs/Web/API/NodeList

tell application "Safari"
    open location "https://calendar.google.com/calendar/syncselect"
    delay 3
    do JavaScript "

    var list = document.querySelectorAll('input[type=checkbox]');
    for (var checkbox of list) {
     checkbox.checked = true;
    };
    " in document 1

document 1
end tell

Upvotes: 1

Related Questions