GUI scripting - making a particular Finder window active

There are a number of windows open which I want to process in turn. They are called “Test 1”, “Test 2”, “Test 3” etc. Processing the first window i.e. “window 1” works. But, I can’t find the verb which switches the process to the next window. I want the next window to be the “selected” or “active” window. I have tried a couple of verbs without luck. eg.

select window 2
tell window 2 to activate

Here is an example:

tell application "System Events"
	tell application process "Finder"
		set frontmost to true
		tell window 2 to activate
	end tell
end tell

This doesn’t work. Window 1 is always active. Window 2 is not.

I have also tried sending the command+shift+~ keystroke which does switch windows but often it switches to the wrong window or to the Desktop.

Can anyone help ?

Is there some good reference material on GUI scripting available ?

And should not work. Because as soon as some “Movies” window has index 2, and you do it the front one, the Finder immediately makes its index = 1. Only the window name does not change, so only window names should be used for your task.

No need GUI scripting.
But the scripts that I provided earlier in this topic contained a significant error: they did not open a selected window, but a window containing the selected window. So, I erased them.

The correct script is:


tell application "Finder"
	activate
	set windowsNames to name of every Finder window
	if windowsNames is {} then return
	try
		set chosenWindowName to item 1 of (choose from list windowsNames)
	on error
		display notification "User cancelled error." sound name "Frog"
		return
	end try
	activate Finder window chosenWindowName
end tell

delay 5 -- to see the result 

Simpler example:


tell application "Finder"
	activate
	activate Finder window "Movies"
end tell

delay 5 -- to see the result

To fully answer the OP’s question, we need to know what constitutes the next window.

If the OP wants to cycle through Finder windows by their index number in descending order, the following should work, although this is essentially the same as the keyboard shortcut (minus the desktop):

tell application "Finder"
	set the index of the front Finder window to the index of the last Finder window
	set the index of the front Finder window to 1
end tell

If the OP wants to cycle through Finder windows by their index number in ascending order then:

tell application "Finder"
	set the index of the last Finder window to 1
end tell

If the OP wants to select or cycle through Finder windows by Name, then KniazidisR’s suggestion or similar should be used.

The following contains a good discussion of the index numbers of Finder windows:

https://macosxautomation.com/applescript/firsttutorial/04.html

peavine, KniazidisR, many thanks for all this.

I think I need to cycle through windows by index. My purpose for all this is to create a command which moves all tabs in a window to separate windows in one step – the existing Finder command only does so one tab at a time.

I can’t find any way of doing that except by GUI scripting. Assuming the user has selected the window to be un-tabbed, the script will:

  • count the number of tabs
  • then repeat for the number of tabs
    • the menu function “Move Tab to New Window”
    • switch back to the tabbed window
  • end after count is reached.

I have had trouble getting Finder to switch back to the tabbed window. There are a lot of things I’ve not understood. For example, using the term “last”. I’ve not seen that mentioned in documentation (e.g. AppleScript Finder library). What does it mean ? Is the “last” window the previous window touched, or the last in the front-to-back hierarchy ? If it’s the latter, it will not help as I need the previous window from which the tab was taken. I have assumed that would be window 2 but, as mentioned, I haven’t found a way of making window 2 active.

Still you’ve helped a lot with more ideas to keep me going. And, I’d forgotten about the Beginner’s Tutorial.

Cheers.

P.S., how can we get BBCode to do lists in these posts ? The example help doesn’t work.

I think I have code that works:


tell application "System Events"
	tell application process "Finder"
		set frontmost to true
		set butt to count of radio buttons of tab group 1 of window 1
		repeat butt times
			click menu item "Move Tab to New Window" of menu 1 of menu bar item "Window" of menu bar 1
			my switcher()
		end repeat
	end tell
end tell

on switcher()
	tell application "Finder" to select window 2
end switcher

I could not get Finder to switch to any other window from within GUI scripting. So, I took it out into a handler and it works. Of course, if a non-tabbed window is selected this produces an error. So, some tidying up is needed.

Cheers.

  1. Well, now, having reached the 5th post, I finally learn that you have not windows, but tabs. Well, okay, that’s not the problem.

  2. The problem is that you use select window or activate window, Finder immediately changes the indexing of 2 windows in places, namely, the index of the 1st window becomes the index of the selected window, and the index of the selected window becomes = 1. And you should understand this. If you don’t want to use the tab names, then listen to what Peavine tells you.

  3. Your code does not work for a reason 2). Firstly, it is not one of the best, since it uses GUI scripting for nothing. But most importantly, it destroys window indexing. Try to return to window 1 (here - tab 1) after selecting the window 2 (here - tab 2). To understand what I mean.

  4. GUI scriptiing is not needed absolutely. I will repeat my code that works with both tabs and windows:


tell application "Finder"
	activate
	set windowsNames to name of every Finder window -- {"Pictures", "Library", "123"}
	activate Finder window "Library"
end tell

delay 5

Proper Peavine’s solution (that is, using indexes):


on switchToWindow(n)
	tell application "Finder"
		activate
		set index of Finder window 1 to n
		activate Finder window n
	end tell
end switchToWindow

tell application "Finder" to name of every Finder window
-- RESULT: {"Pictures", "Library", "123"}

switchToWindow(2) of me -- go to tab "Library"
delay 5 -- do something here
tell application "Finder" to activate Finder window 2 -- return to tab 1 (that is, "Pictures")
delay 5 -- to see only

FWIW, “last” is defined within the site linked in my post 3 above. As you surmise, it is the last position in the front-to-back hierarchy, and it therefore has an index number equal to the number of open Finder windows.

The AppleScript dictionaries are an excellent resource, but the AppleScript Language Guide is also important. In this instance, the section dealing with Reference Forms under the “Index” heading" is the one to consult.

https://developer.apple.com/library/archive/documentation/AppleScript/Conceptual/AppleScriptLangGuide/reference/ASLR_reference_forms.html#//apple_ref/doc/uid/TP40000983-CH4g-120522

Everybody, many thanks. I really appreciate the pointers to other sources – they will help a lot.

Sorry for not mentioning the context (playing with tabs). Eventually I might get the questions right.

Cheers.