18

I'm looking for a method w/ AppleScript to select/focus a specific window of an application without actually doing an "activate" which brings all windows to the front.

Here's what I have so far:

tell application "Google Chrome"
    set windowTitle to title of first window whose title contains "whatever"
end tell
tell application "System Events" to tell process "Google Chrome"
    click menu item windowTitle of menu 1 of menu bar item "Window" of menu bar 1
end tell
tell application "Google Chrome" to activate    

The problem is the "activate" in the last line. That brings everything to the front, but I just want the one window.

15

You're right that the activate command raises all windows. The open shell command only raises one window though:

tell application "Google Chrome" to set index of window 1 where title contains "whatever" to 1
delay 0.05
do shell script "open -a Google\\ Chrome"
  • Ah, this is great. It comes close, unfortunately, if Chrome is already focused the "whatever" window only flashes to the front and the currently focused window wins out. If I use the "click menu item" technique along w/ the open -a, it seems to work, but that requires accessibility. Any ideas to get around that? – Aaron Jensen Jan 20 '14 at 5:14
  • 1
    Just adding a "delay 0.05" in between your two lines seems to work perfectly. Thanks again! – Aaron Jensen Jan 20 '14 at 5:23
  • I couldn't reproduce it, but you could also try tell application "System Events" to tell process "Google Chrome" to perform action "AXRaise" of window 1. – Lri Jan 21 '14 at 6:02
  • 3
    I'm getting: execution error: The variable title is not defined. (-2753) – redolent Mar 24 '16 at 20:31
  • 3
    Re: execution error. Change title to name. – vaughan May 17 '17 at 13:46

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .