ISSUE: Set Script Window Index to 1 Does NOT Change Focus/Frontmost

###ISSUE: Set Script Window Index to 1 Does NOT Change Focus/Frontmost

I’m really hoping for you guys to tell me what I am doing wrong in this script.

Same behavior in Script Debugger 6.0.4 (6A198) on macOS 10.11.6,
and 6.0.5 Beta.

If I set the index of a script window to 1, it does put that window on top, but it does NOT give it focus. The original window still has focus.

###How do I set the focus on a specific script window?
This would be the same as going to menu Window > [WindowName]

Every time you run the script it should toggle the frontmost window.

### Script Debugger 6.0.5 (6A203) on macOS 10.11.6 ###
### Script Debugger 6.0.4 (6A198) on macOS 10.11.6 ###

### ISSUE:  This changes the window index, but does NOT change the
--    window which is frontmost (has focus)


tell application "Script Debugger"
  
  set winList to script windows
  set win1Name to name of item 1 of winList
  log ("win1Name: " & win1Name)
  
  tell script window 1
    
    set winName to name
    set winID to id
    
  end tell
  
  set index of script window 2 to 1
  set winNameAfter to name of script window 1
  
  log ("winNameAfter: " & winNameAfter)
  
  
end tell


This is a macOS bug that we have no control over. It has come up before.

Looks like Chris (@ccstone) as a solution/workaround:

Mark, is this bug unique to SD? I don’t seem to recall having this issue with other apps, like the browsers.

Also, I’m confused. If the menu UI works for Window > [WinName], why can you use that process in a script? IOW, couldn’t you add a scripting command to “set focus on scripting window [#, ID, name]” ?

I wanted to see if changing the selection of the document brought to the front would force the focus to that document. It doesn’t.

But what I noticed is that when you send commands to a tell block for a script window, the commands will go to a different script window if you change the index.

This is a related bug, no? In most scriptable applications, once your commands are going to a specific document or window they keep going to that same document or window even if the index or reference has changed, right?

In this version all the commands in the tell winID2 block following the index change go to the wrong window.



tell application "Script Debugger"
	set winID1 to a reference to script window 1
	set winID2 to a reference to script window 2
	
	tell winID1
		set win1Name to name
		set win1ID to id
	end tell
	
	tell winID2
		set win2Name to name
		set win2ID to id
	
		set {selectStart, selectSize} to character range of selection
		set the index to 1
		set win2Name to name
		set win2ID to id
	
		set the character range of the selection to {1, 1}
		delay 1
		set the character range of the selection to {selectStart, selectSize}
	end tell
end tell

This changes the selection in the correct window, but does not change the focus…

tell application "Script Debugger"
   set winID1 to name of script window 1
   set winID2 to name of script window 2
   
   tell script window winID1
      set win1Name to name
      set win1ID to id
   end tell
   
   tell script window winID2
      set win2Name to name
      set win2ID to id
      
      set {selectStart, selectSize} to character range of selection
      set the index to 1
      set win2Name to name
      set win2ID to id
      
      set the character range of the selection to {1, 1}
      delay 1
      set the character range of the selection to {selectStart, selectSize}
   end tell
end tell

Index-based object references are unstable. Name is better, but is unstable (a) after you save the document to a new document, and (b) if there happen to be two documents with the same name. The best key form to use is ID as this will remain the same the entire time the document is open no matter what you do to it.

Here is your example script made stable via the use of ID key form:

tell application "Script Debugger"
	set winID1 to id of script window 1
	set winID2 to id of script window 2
	
	tell script window id winID1
		set win1Name to name
		set win1ID to id
	end tell
	
	tell script window id winID2
		set win2Name to name
		set win2ID to id
		
		set {selectStart, selectSize} to character range of selection
		set the index to 1
		set win2Name to name
		set win2ID to id
		
		set the character range of the selection to {1, 1}
		delay 1
		set the character range of the selection to {selectStart, selectSize}
	end tell
end tell

Note also that index based window and document references targeting SD are particularly bad when debugging because SD will keep moving the running script to the top each time the script pauses.

Thanks, Mark, and that all makes sense, but I think it’s the inconsistency where commands inside the same tell window tell block go to two different windows that seemed odd. I don’t think I’ve encountered that in scripting other apps.

I think you’re right, addressing windows by ID as opposed to name or index is much more stable.

This isn’t anything unique to SD. Here’s an example illustrating the problem in the Finder:

tell application "Finder"
	set winID1 to a reference to Finder window 1
	set winID2 to a reference to Finder window 2
end tell

tell winID1
	set win1Name1 to name
end tell

tell winID2
	set win2Name1 to name
	set index to 1
end tell

tell winID1
	set win1Name2 to name
end tell

tell winID2
	set win2Name2 to name
end tell

Now, this version of the same script works correctly:

tell application "Finder"
	set winID1 to Finder window 1
	set winID2 to Finder window 2
end tell

tell winID1
	set win1Name1 to name
end tell

tell winID2
	set win2Name1 to name
	set index to 1
end tell

tell winID1
	set win1Name2 to name
end tell

tell winID2
	set win2Name2 to name
end tell

The difference is that the Finder is given an opportunity to return an ID-based object reference which is stable. Look at the value of winID1 and winID2 in this latter version and you’ll see they are ID based while the references in the first version are index based.

I must point out that not all applications are good about returning ID-based object references. Some return the reference you provide, others return ID-based and others return index or name based. Many developers don’t appreciate the importance of the correct reference key forms.

Got it, thanks Mark!