Reputation: 1232
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.
Upvotes: 1
Views: 989
Reputation: 7191
1: You must specify a document or a tab when you use the do javascript
command, like this:
do JavaScript "some command" in document 1 -- not -->window 1
do JavaScript "some command" in (current tab of window 1)
2: You can find a document (or the current tab) by name, like this:
tell application "Safari"
set tDoc to first document whose name contains "(item)"
do JavaScript "some command" in tDoc
-- or
--set tTab to current tab of (first window whose name contains "(item)"
--do JavaScript "some command" in tTab
end tell
3: frames
is a property of an window
, so you must use window.frames
(not document.frames
)
4: document.getElementsById('apples')[0].click()
is wrong
document.getElementById('apples').click()
is correctIf your information is correct, this script will work (if the iframe is not within another iframe):
tell application "Safari"
set tDoc to first document whose name contains "(item)"
do JavaScript "window.frames['mainFrame'].document.getElementById('apples').checked=true" in tDoc
end tell
This script has been tested on a HTML document which contains an iframe (<iframe name="mainFrame" ...
), this iframe contains a form (<form action="demo_form.asp" name="postForm" ...
), and this form contains the checkbox (<input class="normal" id="apples" ... type="checkbox">
).
Upvotes: 1