0

I encounter a problem when writting osascript. I need to "tell" one java process (GUI) to do something but there are other java process with the same process name "java" (also GUI), so my below sample code will not work for me:

  osascript \
    -e "tell application \"System Events\"" \
       -e "tell process \"java\"" \
          -e "click button \"MyButton\" of tab group 1 of window \"MyWindow\"" \
       -e "end tell" \
    -e "end tell"

So my question is how to distinct different java process in such scenario?

Daniel
  • 3
  • 2
  • You will need to know something unique about the process you are targeting. Does the button have a specific name or is the title of the window unique? Maybe there's certain text in the window you can look for. You need something you can count on to find you the right process. Only you can answer that. Once you find it come back and ask again about how you can target that specific unique property. – regulus6633 Jan 24 '13 at 14:13
  • Yes, the button has a unique name "Update Now" and the window has a unique title "Java Control Panel". Thanks in advance for your help! – Daniel Jan 25 '13 at 01:38

1 Answers1

0

Based on your response to my comment, I would do something like the following. Note that I did not test this so you'll probably have to tweak it but it shows how you can check for those specific names. Good luck.

tell application "System Events"
    set javaProcesses to processes whose name is "java"
    repeat with aJavaProcess in javaProcesses
        tell aJavaProcess
            try
                set windowName to name of window 1
                set buttonNames to title of buttons of tab group 1 of window 1
                if windowName is "Java Control Panel" and "Update Now" is in buttonNames then
                    click (first button of tab group 1 of window 1 whose title is "Update Now")
                    exit repeat
                end if
            end try
        end tell
    end repeat
end tell

EDIT: maybe you can get at the proper process like this...

tell application "System Events"
    set javaIDs to unix id of processes whose name is "java"
    repeat with i from 1 to count of javaIDs
        set aJavaProcess to (first process whose unix id is (item i of javaIDs))
        tell aJavaProcess
            -- do the stuff in the tell block from the code above
        end tell
    end repeat
end tell
regulus6633
  • 18,848
  • 5
  • 41
  • 49
  • I tried your code, but it seems when "tell aJavaProcess", it will always take the first lauched java application. Say, in this loop, each round output is the same. – Daniel Jan 25 '13 at 03:06
  • If there's only 1 java process then that one process probably contains all of the windows of all the java apps... probably. So just get a list of the windows of the java process and repeat through that looking for the correct window name and button title. – regulus6633 Jan 25 '13 at 04:06
  • Yes, I agree with your assumption, but in reality there are TWO java process. That is the source of all trouble. – Daniel Jan 25 '13 at 04:34
  • So you're saying you can't get the second java process with "set javaProcesses to processes whose name is "java"". Then its name must be something else. Look in activity monitor and figure out its name or pid or something that will let you get at it. – regulus6633 Jan 25 '13 at 04:55
  • No, you misunderstood what I mean. Actually javaProcesses indeed contains two elements: both "java" in literature, but when applying to "tell aJavaProcess", it seems that applescript picks up the first matched "java" process in order (in order, I mean, the process list order got from "....... to get every process") – Daniel Jan 25 '13 at 05:18
  • Well, take an example: You have two java GUI apps, one window's title is "AWindow" and have a button "AButton", another window's title is "BWindow" and have a button "BButton". And what you want to do is to click BButton while both java process are running. Then we can have two simple tests: The first one is, open B, open A, then run the code you wrote, OK, it works. The second one is, open A, open B, then run the code, and a error message will appear saying that cannot get BWindow of process java. – Daniel Jan 25 '13 at 05:26
  • I added an Edit section to my post with another method... maybe it will work. I understand what you have explained but that's why I put a try block around the code to eliminate errors when it can't find what you are looking for. But in any case maybe this different approach will work. That's the only other way I can think to get at them. – regulus6633 Jan 25 '13 at 06:21
  • Hmmmmmmmmmmmmmmm, I don't want to hurt your feeling but it still does not work. :-( – Daniel Jan 25 '13 at 07:40
  • Though your code does not work it has helped me to finally find out the solution! Thanks a lot! – Daniel Jan 29 '13 at 06:45