Search code examples
applescriptosascript

How to distinct multiple java processes in AppleScript


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?


Solution

  • 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