Select a menu item of a window instead of a process using Applescript

applescript

I'm quite new to applescript (and a beginner programmer) and I've hit a dead end. I figured out how to access a menu item of a process but I do not know how to do it once I have multiple windows. Thanks for any help you can give!

background:
I'm trying to use Applescript with Fiji Is Just ImageJ (FIJI) to process a couple hundred image files at a time. I need to use the "Plugins>Segmentation>Simple Neurite Tracer " plugin but it's not working well with FIJI's native macro language so I am trying applescript.

What I've tried:
I've already set up something that will loop through the images one by one and lunch the plugin but after that I can't figure out how to access the new menus on the windows that pop up.

tell application "Fiji" to activate
delay 3
menu_click({"Fiji", "Image", "Type", "8-bit"})


-- `menu_click`, by Jacob Rus, September 2006
-- 
-- Accepts a list of form: `{"Finder", "View", "Arrange By", "Date"}`
-- Execute the specified menu item.  In this case, assuming the Finder 
-- is the active application, arranging the frontmost folder by date.

on menu_click(mList)
local appName, topMenu, r

-- Validate our input
if mList's length < 3 then error "Menu list is not long enough"

-- Set these variables for clarity and brevity later on
set {appName, topMenu} to (items 1 through 2 of mList)
set r to (items 3 through (mList's length) of mList)

-- This overly-long line calls the menu_recurse function with
-- two arguments: r, and a reference to the top-level menu
tell application "System Events" to my menu_click_recurse(r, ((process appName)'s ¬
    (menu bar 1)'s (menu bar item topMenu)'s (menu topMenu)))
end menu_click

on menu_click_recurse(mList, parentObject)
local f, r

-- `f` = first item, `r` = rest of items
set f to item 1 of mList
if mList's length > 1 then set r to (items 2 through (mList's length) of mList)

-- either actually click the menu item, or recurse again
tell application "System Events"
    if mList's length is 1 then
        click parentObject's menu item f
    else
        my menu_click_recurse(r, (parentObject's (menu item f)'s (menu f)))
    end if
end tell
end menu_click_recurse

I used accessibility inspector to try and figure out the names of the windows so I can use the same process i did to launch the plugin but I wasn't able to comprehend it. The name of the window itself is predictable so I can take the name of the file I open and add "Tracing for:" before it.

enter image description here

enter image description here
I was at another page which was similar but I couldn't adapt it either sadly – no button but still clicking a menu item in a window instead of a process: How do I click a button using AppleScript?

activate application "Fiji"
tell application "System Events" to tell process "Fiji"
click menu item "Quit" of menu "File" of menu bar 1 of window 1
--click button "Quit" of window 1 of window 1
end tell

I then tried to use MouseTools but it does not seem to work consistently. The first mouse click registers perfectly but the second works sometimes and sometimes not.

-- move the mouse to the x/y coordinates (as measured from the top-left part of the screen) and perform a mouse left-click
set mouseToolsPath to (path to home folder as text) & "MouseTools"
set x to 60
set y to 44

do shell script quoted form of POSIX path of mouseToolsPath & " -x " & (x as text) & " -y " & (y as text) & " -doubleLeftClick"


set x to 219
set y to 598

do shell script quoted form of POSIX path of mouseToolsPath & " -x " & (x as text) & " -y " & (y as text) & " -doubleLeftClick"

Best Answer

I must say that your question is rather elaborate and takes quite some reading and thinking ...

But here is a version of your last but one applescript that just MIGHT work a little better for you:

activate application "Fiji"
tell application "System Events" to tell process "Fiji"
    click menu item "Quit" of menu of menu bar item "File" of menu bar 1
end tell

1) What you called "menu finder" actually is menu bar item "File"

2) Menu bars with Apple (normally) don't belong to "window 1" (but to applications). You'd have to get a specific window to the front in order to process just that one.
But anyway, "Quitting" will close all of your windows ...