Setting focus to a window by title

Hi,
I need to set focus to some window identified by its title. I’ve looked around this forum, but couldn’t find anything.
I also tried different versions of


tell application "System Environment"
    activate first window whose name is "window_name"
end tell

and that didn’t work. Also tried setting the variable AXFocused to true and again, no luck.
Thanks in advance,
-Andrew

Hi nyandrew24

something like the below maybe?

tell application “Finder” to open (every window whose name is “TEST”)

Sorry, no.

I am running several firefox applications (i have a separate firefox icon for each on my dashboard) and each of them have multiple windows open (not tabs; so one is a parent and the rest are children). Obviously each window has its own title. I need to set focus to a specific window by title. I still can’t get this to work. Could anyone help out?

Thanks
-Andrew

Hi Andrew,

if you want to script a browser, forget firefox :wink:

Look at this thread. If there’s no solution, threre’s none

Thanks - I’ve actually looked through that thread before.
I’m sure there must be a solution - I’ve used the following script before to give focus
to some window from firefox as long as there’s only one firefox application open.


tell application "Firefox"
	activate
	set visible of first window whose name contains "some_name" to true
end tell

The problem is that I really don’t know applescript at all. For multiple running firefox
applications, I feel the strategy would be to:

  • iterate through all running firefox applications
  • for each, if it has a window with title equal to whatever_title_i’m_looking_for
    • set that window to be focused (I’m guessing set visible = true?)
    • break out of both loops

I just don’t know how to put that into code.

-Andrew

you could use “index”, and then write your script around it.

tell application “Firefox”
set index of window 1 to 2
end tell

their is also this app which may assist.

http://www.manytricks.com/witch/

That doesn’t really help as, like I said above, I don’t know applescript at all, and there aren’t any good sources to learn it,
so I simply don’t know how to do that. I’ve just been playing around with random scripts.

Could someone explain why the following script gives me the error “System Events got an error: Can’t make visible of every window of process id 51380225 whose visible = true and name ≠"Google" into type reference.” ?


on run
	try
		tell application "System Events"
			set appList to (id of every process whose visible = true)
			set firefoxAppList to (id of every process whose name = "firefox-bin")
		end tell
		
		tell application "System Events"
			repeat with firefoxProcess in firefoxAppList
				set windowList to (title of every window of process id firefoxProcess)
				
				repeat with windowTitle in windowList
					tell process id firefoxProcess
						set (visible of every window whose visible is true and name is not "Google") to false
					end tell
				end repeat
				delay 1
			end repeat
			
		end tell
		
	on error err
		tell me to activate
		display alert "There was a problem running the script." as critical message err
	end try
end run

also - how would i do something simple like get title of currently focused window whatever the app may be?
I think a stupid way I could solve my problem is just instruct the computer to hit ctrl+F4, then check which window
has focus and if its title matches the one i want, break, else keep going.

-Andrew

“I don’t know applescript at all, and there aren’t any good sources to learn it,”

your in a forum to do with “Applescript”, excellent source,

these books are pretty useful. (to me they are)

http://www.oreilly.com/catalog/applescpttdg2/
The Definitive Guide by Matt Neuburg, second edition.

http://www.spiderworks.com/books/ashandbook.php
Danny Goodman’s AppleScript Handbook Mac OS X Edition.

http://www.oreilly.com/catalog/applescripttmm/
AppleScript: The Missing Manual.

been mucking around with you last posted script, getting some where I think

on run
try
tell application “System Events”
set appList to (id of every process whose visible = true)
set firefoxAppList to (id of every process whose name = “firefox-bin”)
end tell
tell application “System Events”
repeat with firefoxProcess in firefoxAppList
set windowList to (title of every window of process id firefoxProcess) --as alias

			--repeat with windowTitle in windowList
			--tell process id firefoxProcess
			--set (visible of every window whose visible is true and name is not "Google") to false
			--end tell
			if windowList contains "Google" then
				tell application "Firefox"
					set _new to get index of window "Google"
					set index of window _new to 1
				end tell
			end if
			--end repeat
			delay 1
		end repeat
	end tell
on error err
	tell me to activate
	display alert "There was a problem running the script." as critical message err
end try

end run

Just as an update to this (old) thread:

You can use code like the following to bring a window named SOME_WINDOW_NAME to the front for an app named SOME_APP:

tell application "System Events"
	tell application process "SOME_APP"		
		perform action "AXRaise" of (first window whose name contains "SOME_WINDOW_NAME")
	end tell
end tell

The ‘perform action’ command is a very useful one for UI Scripting.

Source: http://apple.stackexchange.com/questions/39204/script-to-raise-a-single-window-to-the-front