Applescript to Continue Only If Item In Scroll Area Exists

I have this Applescript that uses Audio MIDI Setup to connect through MIDI to other computers.

Right now the script works for the most part, it connects to the computer, but at the moment I have it connecting to every computer that is populated (up to three, these are auto populated by bonjour). The problem is that list of computers is never in the same order, it gets populated differently every time.

What I want to do is move down the list like the Applescript is already doing, but only click the “connect” button if an item in the list has the correct words in it.

I have included an image of the window. The area under “Directory” is where the list gets populated with multiple computers.

			display dialog ("MIDI Starting Up" & return & return & "Please do not touch mouse or keyboard till startup confirmation.") buttons "" giving up after 8
			delay 1
			showMIDINetworkSetup()
			delay 1
			activateTheMidiSession(2)
			patchTheMidiSession(2, 1)
			patchTheMidiSession(2, 2)
			patchTheMidiSession(2, 3)

			delay 3

			tell application "System Events"
if exists window "Midi Network Setup" of process "Audio MIDI Setup" then
	keystroke "w" using command down
end if
if exists window "MIDI Studio" of process "Audio MIDI Setup" then
	keystroke "w" using command down
end if
if exists window "Audio Devices" of process "Audio MIDI Setup" then
	keystroke "w" using command down
end if
			end tell

			delay 1

			activate
			display dialog ("MIDI succesfully started, you can now close this window." & return & return & "DO NOT QUIT AUDIO MIDI SETUP APP") default button "OK"

			on activateTheMidiSession(2)
activate application "Audio MIDI Setup"
tell application "System Events"
	tell process "Audio MIDI Setup"
		try
			
			set theCheckbox to checkbox 1 of UI element 1 of row 2 of table 1 of scroll area 1 of group 1 of window "MIDI Network Setup"
			tell theCheckbox
				set checkboxStatus to value of theCheckbox as boolean
				if checkboxStatus is false then click theCheckbox
			end tell
		end try
	end tell
end tell
			end activateTheMidiSession


			on patchTheMidiSession(2, theResponderNumber)
activate application "Audio MIDI Setup"
tell application "System Events"
	tell process "Audio MIDI Setup"
		try
			select row 2 of table 1 of scroll area 1 of group 1 of window "MIDI Network Setup"
			select row theResponderNumber of table 1 of scroll area 1 of group 3 of window "MIDI Network Setup"
			click button "Connect" of group 3 of window "MIDI Network Setup"
			
		end try
	end tell
end tell
			end patchTheMidiSession

			on showMIDINetworkSetup()
try
	activate application "Audio MIDI Setup"
	tell application "System Events"
		tell process "Audio MIDI Setup"
			try
				click menu item "Show MIDI Studio" of menu 1 of menu bar item "Window" of menu bar 1
			end try
			delay 3
			try
				click menu item "Open MIDI Network Setup…" of menu 1 of menu bar item "MIDI Studio" of menu bar 1
			end try
		end tell
	end tell
on error
	return false
end try
			end showMIDINetworkSetup

I’ve never used “Audio MIDI Setup” before.
How do I get to this window “MIDI Network Setup”.

Also one should not use “keystroke” command to do things unless no other option is available. It would fail if the target application should accidentally move to not being the frontmost app. Better to use “click” command on button to close

tell application "System Events"
	tell process "Audio MIDI Setup"
		click button 1 of window "Midi Network Setup"
	end tell
end tell

Thanks for the advice.

You can get to the MIDI Network Setup by clicking on Show MIDI Studio in the menu bar and then once that window is up, there is a globe/world icon in the top right corner of that window that will open up MIDI Network Setup.

Here is a modified version of the close function

tell application "System Events"
	tell process "Audio MIDI Setup"
		if exists window "MIDI Network Setup" then
			click button 1 of window "MIDI Network Setup"
		end if
	end tell
end tell

Here is code to get to the list

tell application "System Events"
	if "Audio MIDI Setup" is not in name of application processes then return
	tell application process "Audio MIDI Setup"
		if exists window "MIDI Network Setup" then
			set flag to true
		else
			set flag to false
			if exists window "MIDI Studio" then
				tell window "MIDI Studio" to click checkbox 2 of group 1 of group 4 of toolbar 1
				if exists window "MIDI Network Setup" then set flag to true -- test to make sure window opened
			end if
		end if
		if flag then -- window is now open
			repeat with i from 1 to count (rows of table 1 of scroll area 1 of group 3 of window "MIDI Network Setup")
				tell UI element 1 of row i of table 1 of scroll area 1 of group 3 of window "MIDI Network Setup"
					if name does not contain "Test" then -- will uncheck any that don't have 'Test' in name
						if ((value of checkbox 1) = 1) then
							click checkbox 1
						end if
					end if
				end tell
			end repeat
		end if
	end tell
end tell
1 Like

I gave this a try, but I can’t seem to get it to work. My thought would be because there isn’t actually a checkbox to check in that scroll area and thus it can’t check or uncheck it.

Thoughts? Maybe I’m doing it wrong?

**EDIT I had an answer here for a different question. I removed it but below is correct

It works on my Mac running Monterey
Did you copy it correctly.

Also did you notice my script will uncheck any item in list that “doesn’t” have ‘test’ in the name

Also my MIDI programs has checkboxes

Hmmm. That’s interesting, I’ve tried this program in both Monterey and Ventura. I’ll dig in see if there is a setting to show checkboxes.

I figured it out, when you allow other midi devices to auto populate, then they show up without checkboxes. I’ll go about adding them myself to get the checkboxes and see how that goes.

Interesting.
So if it auto populates, there’s no way to select or deselect each item?

BTW, what are the ‘correct words’ for the ones you want to connect?

Correct, the “typical” way would be to click on an item on the list and click the “connect” button then do the same for another you would want connected.

My thought would be to move down the list one by one and if one of the item’s in the list met the name criteria then click the “connect” button, otherwise move on.

The correct words are pretty fluid, something like “lighting” would be fine.

Easy to modify my script to not bother with the checkbox and instead check the name/title instead. Will do so when I get back. (Sight-seeing)

Also since I don’t have a midi setup, what happens after you click connect? Is there a dialog or other window that appears that I would have to contend with before I move on to the next connection?
Or is there a visual indication that the connection happened lick a check mark or bullet character appearing in the name?

here is the edited version that clicks the connect button

tell application "Audio MIDI Setup" to activate
tell application "System Events"
	if "Audio MIDI Setup" is not in name of application processes then return
	tell application process "Audio MIDI Setup"
		if exists window "MIDI Network Setup" then
			set flag to true
		else
			set flag to false
			if not (exists window "MIDI Studio") then
				click menu item "Show MIDI Studio" of menu "Window" of menu bar 1
				repeat until exists window "MIDI Studio"
					delay 1
				end repeat
			end if
			tell window "MIDI Studio"
				repeat until exists checkbox 2 of group 1 of group 5 of toolbar 1
					delay 1
				end repeat
				click checkbox 2 of group 1 of group 5 of toolbar 1
			end tell
			repeat until exists window "MIDI Network Setup"
				delay 1
			end repeat
			set flag to true -- test to make sure window opened
		end if
		if flag then -- window is now open
			repeat with aRow in rows of table 1 of scroll area 1 of group 3 of window "MIDI Network Setup"
				set aRow to contents of aRow
				if (name of UI element 1 of aRow) contains "Test" then -- will uncheck any that don't have 'Test'
					set selected of aRow to true
					repeat until enabled of button "Connect" of group 3 of window "MIDI Network Setup"
						delay 0.5
					end repeat
					delay 1
					click button "Connect" of group 3 of window "MIDI Network Setup"
					delay 3
					if description of window 1 is "alert" then
						click button "OK" of window 1
						delay 1
					end if
					set selected of aRow to false -- not needed, but I do it for the visuals
				end if
			end repeat
		end if
	end tell
end tell