Is there a way to get a window object?

I’m trying to automate the Preferences of an application called ScreenFlow.

As you can see, the window is split into 4 tabs: General, Timeline, Advanced and Licenses.

Each time you switch tabs the title of the window also changes.

It seems to be impossible to get an Object Specifier which will work all the time. E.G.
Say I want to go to the timeline tab from the General tab:

var prefs = Application("System Events").applicationProcesses.byName("ScreenFlow").windows.byName("General")
var tabs = prefs.toolbars[0].entireContents()
tabs[1].click()

the above code snippet works fine. However now if I want to go to the 3rd tab:

tabs[2].click()

I get an error:

Error: Can't get object.

Which makes sense because the window is being referenced by it’s name. However I want this object to be accessible from anywhere all the time with a permanent object specifier… Is there a way to do this? On windows, for example, I would use a hWnd. Is there a mac equivalent of an hWnd?

A bit clunky, perhaps there’s a better way.

tell application "System Events"
	tell process "ScreenFlow"
		set allPrefWindowNames to {"General", "Timeline", "Advanced", "Licenses"}
		set windowNames to the name of every window
		repeat with aWindowName in windowNames
			if aWindowName is in allPrefWindowNames then
				set prefWindowName to the contents of aWindowName
				exit repeat
			else
				display dialog "Unable to locate ScreenFlow's \"Preferences\" window." buttons {"OK"} default button "OK"
			end if
		end repeat
		click button "Timeline" of toolbar 1 of window prefWindowName
	end tell
end telll

If ScreenFlow ever has any windows open with one of these titles that isn’t the Preferences window, it could break. But I downloaded ScreenFlow and tested it, and baring an exception like the one above, it does work.

Sorry it’s not in Javascript, but since Applescript is such a readable language, I assume it’s not hard to translate.

indeed, that’s why I didn’t want to do it that way :stuck_out_tongue: No problem with it not being in JS, it is easy to port. I ended up using the ‘accessibilityIdentifier’ of the window instead:


    //Try to find preferences in screenflow:
    For(var i = 0;i<SF.windows.length;i++){
        if(SF.windows[i].attributes['accessibilityIdentifier'].value() == 'PreferencesWindowIdentifier'){
        return SF.windows[i]
    }
}

That’s good, I hadn’t seen that.

If there was a possibility of other windows open with that name in your use case, my next suggestion was going to be to add a second criteria in the repeat loop - if the window name matched, then also check to see if that window contains a “toolbar1” that has those four buttons - at that point, you know you’ve got the preferences window.