The macosxhints Forums

The macosxhints Forums (http://hintsforums.macworld.com/index.php)
-   OS X Developer (http://hintsforums.macworld.com/forumdisplay.php?f=27)
-   -   applescripting a javascript form (http://hintsforums.macworld.com/showthread.php?t=100814)

djeans 04-22-2009 10:58 PM

error seems to be coming from the get menu part
"System Events got an error: Can’t get menu 1 of pop up button 1 of group 3 of UI element 1 of scroll area 1 of UI element 1 of scroll area 2 of UI element 1 of scroll area 1 of group 3 of window . . . . "

Tried the shorter script above, but it won't compile, with the error
"Expected end of line but found identifier." at the web area part with area highlighted.

I found that if I put in a long enough delay to select a location manually and then add in

get value of pop up button 1 of group 3 of UI element 1 of scroll area 1 of UI element 1 of scroll area 2 of UI element 1 of scroll area 1 of group 3 of window "Main" of application process "Safari"

It returns whichever location that I selected.

I tried changing the get value to set value, but that had no effect.

D

djeans 04-23-2009 04:26 AM

Partial success:

I couldn't get anything to work, so I thought about a different approach. Using a loop and down arrow to scroll through all of the locations until I hit the right one.

Here is the script so far

set menu_item to "Honolulu" as string (* my location *)

tell application "Safari"
activate
tell (make new document) to set URL to "https://intranet.locations/"
delay 5

tell application "System Events"

repeat until value of pop up button 1 of group 3 of UI element 1 of scroll area 1 of UI element 1 of scroll area 2 of UI element 1 of scroll area 1 of group 3 of window "Main" of application process "Safari" is menu_item
tell application "System Events" to click pop up button 1 of group 3 of UI element 1 of scroll area 1 of UI element 1 of scroll area 2 of UI element 1 of scroll area 1 of group 3 of window "Main" of process "Safari"
key code 125 (* down arrow *)
keystroke return (* confirm choice of menu item *)
delay 0.5
end repeat


beep
end tell
end tell

This works, albeit very slowly. For some reason, there is a long pause after the popup button is pressed (about 5 seconds) before it arrows down to the next selection even though there is no delay statement between the two actions.

If you see anything I can change to clean up the code to make it faster, let me know.

The next task will be getting the information that is returned in table form copied into an email - if you have any suggestions in that regard :)

D

tw 04-23-2009 06:49 PM

I don't understand why you're UI scripting this when it's much easier to use Safari's Do Javascript command. write a short javascript in an applescript variable and fire it off, like the following:
Code:

Set JScode to "themenu = document.getElementById('LocationName');
for (i=0; i<themenu.length; i++) {
    if ( themenu.options[i].value == 'Honalulu' ) {
          themenu.selectedIndex = i;
          GetRecords();
    }
}"
tell application "Safari" to do javascript JScode in document 1;

I haven't tested this, of course. you might not need to call GetRecords() explicitly (I don't know if javascript changes register with the onchange handler), and I might have bugs in the javascript (can you set a menu value by setting the selectedIndex property? I don't remember). but still...

djeans 04-23-2009 07:10 PM

tw,

thanks for the reply.

I am not very familiar with javascript, but I did try dropping in your script, but it didn't appear to do anything. Does in need to have a dojavascript in front of it?

Originally I was trying to avoid UI scripting in favor of javascript, but was not having any luck.

Thanks
D

tw 04-23-2009 07:20 PM

it's really hard to debug these things without access to the code. did you check console, or the safari error log (if you have it enabled) to see if it's throwing any errors?

tw 04-23-2009 08:10 PM

ok, a little playing around, and I realized that I had the reference form wrong. try this version instead:

Code:

set JScode to "themenu = window.frames[0].document.forms[0].LocationName
for (i=0; i<themenu.length; i++) {
    if ( themenu.options[i].value == 'Honolulu' ) {
          themenu.selectedIndex = i;
          window.frames[0].GetRecords()
    }
}"
tell application "Safari" to do JavaScript JScode in document 1

assuming that it's the first form on the first frame of the page (and that I spelled Honolulu correctly).

djeans 04-23-2009 08:57 PM

Thanks again for the reply and trying to help.

I tried your new code, with same results. The page has 2 frames, as far as I can tell, with the only form on the page being in the second frame so I did change the frames[0] to frames [1] and tried that as well. Still nothing happening after the page loads.

D

djeans 04-23-2009 08:58 PM

Quote:

Originally Posted by tw (Post 530086)
it's really hard to debug these things without access to the code. did you check console, or the safari error log (if you have it enabled) to see if it's throwing any errors?

Haven't checked those yet, will try and see what comes up.

roncross@cox.net 04-23-2009 08:59 PM

If you know who developed the web page, you might try asking the programming what the elements/documents are for what you are trying to do. Ask them, how do they access this with javascript.

djeans 04-23-2009 09:01 PM

Ok, looked at Safari error log, there is this:

TypeError: Result of expression 'window.frames[1].document' [undefined] is not an object.

tw 04-23-2009 09:45 PM

well, that's not very helpful. :D what does the html of the frames page look like? not the page you posted, which is what goes in the frame, but the outer page...

djeans 04-24-2009 03:14 AM

Ok, here is the html for the main page

<html>
<head>
<title>Main</title>
</head>
<frameset rows='70,*' border='0'>
<frame src='/header.asp' name='Header' marginheight='0' marginwidth='0' scrolling='no' noresize>
<frame src='http://intranet.locations/' name='Report' marginheight='0' marginwidth='0' scrolling='auto'>
</frameset>
</html>

Hope that helps

d

tw 04-24-2009 11:56 AM

alright, do this. copy the following code:
Code:

set JScode to "themenu = window.frames['Report'].document.forms['locations'].LocationName
for (i=0; i<themenu.length; i++) {
        alert( "index " + i + ": " + themenu.options[i].value);
        if ( themenu.options[i].value == 'Honolulu' ) {
                alert("Found Honolulu at index " + i);
                themenu.selectedIndex = i;
                window.frames['Report'].GetRecords()
        }
}"
tell application "Safari" to do JavaScript JScode in document 1

and paste it into the Script editor. then open the web page in Safari, go back to the script editor and click run. it should give you an annoying alert for every list item it finds, and a different one if it finds Honolulu. see it if does anything, and either way tell me what errors you see in Safari and the Script editor, and what the alerts say (they ought to give a menu index number and the value of the menu item).

djeans 04-24-2009 03:51 PM

Quote:

Originally Posted by tw (Post 530160)
alright, do this. copy the following code:
Code:

set JScode to "themenu = window.frames['Report'].document.forms['locations'].LocationName
for (i=0; i<themenu.length; i++) {
        alert( "index " + i + ": " + themenu.options[i].value);
        if ( themenu.options[i].value == 'Honolulu' ) {
                alert("Found Honolulu at index " + i);
                themenu.selectedIndex = i;
                window.frames['Report'].GetRecords()
        }
}"
tell application "Safari" to do JavaScript JScode in document 1

and paste it into the Script editor. then open the web page in Safari, go back to the script editor and click run. it should give you an annoying alert for every list item it finds, and a different one if it finds Honolulu. see it if does anything, and either way tell me what errors you see in Safari and the Script editor, and what the alerts say (they ought to give a menu index number and the value of the menu item).

Gives me an error that says
A property can’t go after this “"”.
with "Index " highlighted when trying to compile. Won't let me run the script.

D

tw 04-24-2009 04:47 PM

Quote:

Originally Posted by djeans (Post 530177)
Gives me an error that says
A property can’t go after this “"”.
with "Index " highlighted when trying to compile. Won't let me run the script.

D

ugh, I hate it when I make stupid errors. the double-quotes - " - on the two lines that have alert statements should be single-quotes.

djeans 04-24-2009 06:19 PM

Ok,

Thanks!

It compiles now, but when run, nothing happens.

Error log returns the same error:
TypeError: Result of expression 'window.frames['Report'].document' [undefined] is not an object.

tw 04-24-2009 07:49 PM

well, I don't know what to tell you. I duplicated the structure of the two pages you posted and the script works fine, so that tells me that something is off in what you sent, or some kind of DOM manipulation is happening in the javascripts that I can't see. the fact that it says the document is not an object tells me that that frame is not loaded when the script runs, but not why...

if it were me, I'd start playing with Safari's Web Inspector (right click on the menu and select 'Inspect Element') to see if I could figure out the correct way to reference the menu and scripts (which is where we're stuck). nothing more I can do on this end, though (except guess), without access to the site.

djeans 04-24-2009 08:17 PM

I think I may have stumbled on something, although I'm not sure if it is the problem, or if it is, if it can be worked around.

found another error that says:
Unsafe JavaScript attempt to access frame with URL https://extranet.mycompany.com/web/t.../px/locations/ from frame with URL http://www.mycompany.com/px/locations/Locations.asp. Domains, protocols and ports must match.

I have edited out my company name out of paranoia, and I could perhaps provide you with a url, but don't want it on the forum, for same paranoia reasons.

I appreciate your help thus far, and understand if you are not interested in helping further, but if you are, let me know and I'll give you a url to test it out via email or private msg.

d

tw 04-24-2009 08:39 PM

you can send me a private message with the info on the forum, no problems. just hover your mouse over my user name and click from the auto-dropdown -menu.

djeans 04-25-2009 05:32 PM

TW, sent you a private message yesterday, but can't find it in my sent messages. Did you get it?

D


All times are GMT -5. The time now is 05:35 AM.

Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2014, vBulletin Solutions, Inc.
Site design © IDG Consumer & SMB; individuals retain copyright of their postings
but consent to the possible use of their material in other areas of IDG Consumer & SMB.