Hi,
I’ve both googled and looked over the tutorials forum and not found the answer to what is likely a simple nomenclature problem.
I am writing a script to transfer the data from an excel spreadsheet field by field onto a Safari form. The first ten of the Safari fields are actual fields into which I can paste data cut from the corresponding spreadsheet cell. After this, however, the data is input into the Safari page by clicking radio buttons. I am not the author of the form and do not know the name or title of these radio buttons. I’m a button newb. What I want to do when I get to datapoint 11 and onward is check the Excel cell and test it for “Yes” or “No”, then make the script set the corresponding Safari radio button state appropriately for “Yes” (row 1 of the radio button matrix) or “No” (row 2 of the radio button matrix), then rinse and repeat for datapoint 12 - 20. After that, the form goes back to actual fields for which c/p works fine. As there are 60 fields per form and multiple hundreds of forms, and the company hosting our dbase has quoted nearly $1000 to import the excel spreadsheet directly into the dbase (!), I am left with automating the transfer of Excel data into the Safari forms on the user interface end to avoid having to do the data entry by hand.
Can somebody guide me in identifying and setting the state of the correct “Yes” or “No” row per radio button? I suspect all I need is a way to correctly target each matrix by index in something like this:
set row 1 of matrix 11 of window 1 to 1
But I can’t get it to work. I would be grateful for any assistance.
Thank you.
You’re going to need to use javascript. Here’ a quick example. Open this web page here. At the very bottom of that web page is a group of radio buttons that we can use for this example.
Using javascript, in order to access a radio button group we need to know 1) the form name of the radio group and 2) the name of the radio group. So using that web page and the radio group of buttons at the bottom, you can view the source code for the web page and see that the form name is “f1” and the radio group name is “r1”. Therefore, to access that radio group using javascript we can use “document.f1.r1”. And if we wanted to access the first button in the radio group we use “[0]” to indicate the first one.
We can select a particular one like this… in this example we’ll select the one with the “SE” value… you can see the value of the different radio buttons in the group by looking at the source code. So run this applescript with that web page as the frontmost document in safari, and “SE” will be checked!
tell application "Safari"
tell document 1
-- get the number of radio buttons in the group
set buttonCount to do JavaScript "document.f1.r1.length;"
-- loop through the buttons and when we find "SE" we check it
repeat with i from 1 to buttonCount
set theValue to do JavaScript ("document.f1.r1[" & ((i - 1) as text) & "].value")
if theValue is "SE" then
do JavaScript "document.f1.r1[" & ((i - 1) as text) & "].checked = true"
exit repeat
end if
end repeat
end tell
end tell
That is, without a doubt, the most useful response I have ever received on any forum on any subject.
Now if I could only test this out at my Windows-based home before I get to Mac-based work on Monday morning. Oh well, until then I can study javascript for the first time. I’ve only been scripting a few years–and that mostly open source game code–and have never had cause to venture into it before, but this gives me a real world goal to do so.
Thank you.
Glad to help.
If you’re using cut/copy/paste to enter the text field values too I would suggest that you learn the javascript script commands for that too. It would be more reliable I think. In any case good luck.
As it turns out, I cannot learn javascript in a weekend. My self-confidence is dashed.
I was able to trim down your kindly provided code to a single line, however, as I have no need for the search loop or the button count. There are always only two choices, “Yes” or “No”. It defaults to “Yes”, so I merely have to test the Excel value for “No” and if it is “No,” then I can:
do JavaScript "document.publishForm.field_id_7[1].checked = true"
Except it doesn’t work. I’m fairly certain of the field name from this snippet of source code:
<div class="publish_field publish_radio" id="hold_field_7" >
<div class="handle"></div>
<label class="hide_field">
<span>
<img class="field_collapse" src="http://XXXXX.png" width="10" height="13" alt="" />
Kosher </span>
</label>
<div id="sub_hold_field_7" >
<fieldset class="holder">
<fieldset>
<label><input type="radio" name="field_id_7" value="Yes" /> Yes</label><label><input type="radio" name="field_id_7" value="No" /> No</label></fieldset> </fieldset>
</div>
</div>
I can’t seem to get the form name, though. I can’t find it in the source code and although this pulls up an ID (spoken and displayed for redundant debugging confirmation:
do JavaScript "oForm = document.forms[2]"
set JSTextTest2 to (do JavaScript "oText = oForm.id")
say JSTextTest2
display dialog JSTextTest2
changing it to:
do JavaScript "oForm = document.forms[2]"
set JSTextTest2 to (do JavaScript "oText = oForm.name")
say JSTextTest2
display dialog JSTextTest2
comes up blank. Could regulus or anybody tell me how to return the name of the current form?
Thank you again in advance.
Got it.
do JavaScript "document.forms[2].field_id_7[0].checked = true"
Looks fairly obvious in hindsight…and several hours of research later. A learning moment!
And another roadblock, this time with Checkboxes. How do I target a specific checkbox, for instance the first one in the source code snippet below “Corn”. The name of all of them appears to be “field_id_10[]”. I can’t seem to isolate any one of them via scripting. Thanks, as always, for guidance.
<div id="sub_hold_field_10" >
<fieldset class="holder">
<fieldset>
<label><input type="checkbox" name="field_id_10[]" value="Corn" /> Corn</label><label><input type="checkbox" name="field_id_10[]" value="Eggs" /> Eggs</label><label><input type="checkbox" name="field_id_10[]" value="FDC Food Coloring" /> FDC Food Coloring</label><label><input type="checkbox" name="field_id_10[]" value="Fish" /> Fish</label><label><input type="checkbox" name="field_id_10[]" value="Gluten" /> Gluten</label><label><input type="checkbox" name="field_id_10[]" value="Lactose" /> Lactose</label><label><input type="checkbox" name="field_id_10[]" value="Milk" /> Milk</label><label><input type="checkbox" name="field_id_10[]" value="Maltodextrin" /> Maltodextrin</label><label><input type="checkbox" name="field_id_10[]" value="Peanuts" /> Peanuts</label><label><input type="checkbox" name="field_id_10[]" value="Preservatives" /> Preservatives</label><label><input type="checkbox" name="field_id_10[]" value="Shellfish" /> Shellfish</label><label><input type="checkbox" name="field_id_10[]" value="Soy" /> Soy</label><label><input type="checkbox" name="field_id_10[]" value="Sugar" /> Sugar</label><label><input type="checkbox" name="field_id_10[]" value="Tree Nuts" /> Tree Nuts</label><label><input type="checkbox" name="field_id_10[]" value="Wheat" /> Wheat</label></fieldset> </fieldset>
</div>
</div>