Safari Text Fields

My Applescripting abilities are limited but I have been able to cobble together a couple of scripts. One of which I am attempting to rewrite using less code. The area where I am having difficulty with is a section in which I am using System Events in order to set two text fields in Safari to the value of two variables that are defined elsewhere in the script.

A copy of the offending Applescript code is:

[code]set value of text field 1 of group 5 of UI element 1 of scroll area 1 of group 2 of window “XXXXXXXXXXXX” to account_number

set value of text field 1 of group 7 of UI element 1 of scroll area 1 of group 2 of window “XXXXXXXXXXXX” to meter_number[/code]
Where the varables are set to distinct values from a Microsoft Excel cell.

When the script opens the URL address, I can see the value of the variables being placed in the text fields. Upon hitting a search button after the data appears on the screen the site’s Javascript is supposed to search for data associated with the variables entered.

When the search button is clicked a Javascript window opens in Safari indicating that the text fields need data entered before the a “Search” button in the Safari Window can be executed. How can this be when I see what appear to be the values in the text fields?

Help!:frowning:


Model: Power Mac G5
Browser: Safari 412.5
Operating System: Mac OS X (10.4)

Look at the javascript? It may be checking that some event occured in those fields (like loosing focus) that your UI scripting hasn’t triggered even though the values are in there.

Thanks for your response. I am not familiar with Java but am including the source code (Java) from the URL address that I am attempting to enter data into the text fields, in hopes that you may be able to review and suggest some solutions. I believe there is a “do Javascript” command in Applescript but I am not to familiar with it but sense that it might be one way to skin this cat. Anyway here is what I believe is the ssential part of the Java code. Any help you could provide would be appreciated. Thanks!

function Load()
{
var strAcct;
strAcct = document.submitform.txtinputAcct.value;

if (strAcct.length > 9)
	document.submitform.txtinputMeter.focus();
else
	document.submitform.txtinputAcct.focus();

}

// CHECK FIELDS - NUMERIC DIGITS AND SPECIAL CHARACTERS ALLOWED
function checkNumber(checkString)
{
newString = “”; // REVISED/CORRECTED STRING
count = 0; // COUNTER FOR LOOPING THROUGH STRING

for (i = 0; i < checkString.length; i++) {
    ch = checkString.substring(i, i+1);

    if ((ch >= "0" && ch <= "9")) {
        newString += ch;
    }
}

if (checkString != newString) {
    return 1;
  } else {
    return 0;
  }

}

function checkTab(strValue)
{
switch (strValue) {
case 1 :
newString = document.submitform.txtinputAcct.value;
if (newString.length > 9)
{document.submitform.txtinputMeter.focus();}
break;
case 2 :
newString = document.submitform.txtinputMeter.value;
if (newString.length > 8)
{document.submitform.button1.focus(); /submitform_Submit();/}
break;
}
}

function submitform_Submit(){

var flag, flag2;
var submitprep;
var msgInvalid = “”;
var fieldsNeeded = “\nA value must be entered in the following field(s):\n\n\t”;
var Invalidfield = “\nThe Account Number you have entered\ncontains invalid characters:\n\n\t”;
var Invalidfield2 = “\nThe Meter Number you have entered\ncontains invalid characters:\n\n\t”;

submitprep = 0;
if (document.submitform.txtinputAcct.value == "")
{
	submitprep = 1;
    fieldsNeeded += "Account Number" + "\n\t";
}

if (document.submitform.txtinputMeter.value == "")
{
	submitprep = 1;
    fieldsNeeded += "Meter Number" + "\n\t";
}

if (submitprep == 1)
{
	alert(fieldsNeeded);
}

flag = checkNumber(document.submitform.txtinputAcct.value);
flag2 = checkNumber(document.submitform.txtinputMeter.value);

if (flag == 1)
{
	submitprep = 1;
    msgInvalid = Invalidfield;;
}		

if (flag2 == 1)
{
	submitprep = 1;
    msgInvalid = msgInvalid + Invalidfield2;
}		

if (flag == 1 || flag2 == 1)
{
	alert(msgInvalid);
}

// submitform.MType.value = document.radio1.Opt_Display(1).value;
// alert(document.radio1.Opt_Display(1).value);

if (submitprep == 0)
{
	document.submitform.submit();
}

}

//–>

I don’t know anything about it, but I know curl can submit some forms on webpages.

I’ve yet to see values set in this way stick, flounder. (They should do, but I suspect a general bug may currently be preventing the method from working properly - even when using the ‘confirm’ command.) :confused:

For a workaround, you might consider something like the routine below. For this to work, Safari must be frontmost, with the target window at the front. (Since I don’t know the site you are trying to access, I obviously haven’t been able to test it.)

to setValue for f to v
    tell application "System Events" to tell f
        set value of attribute "AXFocused" to true
        repeat until focused is true
            delay 0.2
        end repeat
        keystroke "a" using command down
        keystroke v
        repeat until value is v
            delay 0.2
        end repeat
    end tell
end setValue

tell application "Safari" to set index of window "XXXXXXXXXXXX" to 1 (* modify as required *)
tell application "System Events" to tell process "Safari"
    set frontmost to true
    tell UI element 1 of scroll area 1 of group 2 of window 1
        my (setValue for text field 1 of group 5 to account_number)
        my (setValue for text field 1 of group 7 to meter_number)
    end tell
    keystroke return (* or click the relevant button *)
end tell

thanks to all that replied…I have settled on GUI scripting to complete the forms.