Can't get selection from some windows

I’m having a new issue that seems to have crept up in 7A41. (I’ll upgrade to 45 soon, but it doesn’t seem this has been addressed).

I’ve been running the script below for months. Today when I tried to copy all text in a script in the only script window open (using Select All), I got an error message that SD couldn’t get the selected text of the script.

The script worked on other script windows (including its own) but would not work on this other window with all the text selected. It did work if I had a single line selected, but not if I had a chunk of text selected.
The script window had not been saved so I saved it and it still didn’t work. Same error each time.

set whichWindow to 1
tell application "Script Debugger"
   properties of window whichWindow
   set selectedText to the selection of window whichWindow
   set entireScript to source text of current document of window whichWindow
end tell
set AppleScript's text item delimiters to {return}
set entireScript to {"```AppleScript", entireScript, "```", ""} as text
if selectedText is not "" then
   set selectedText to {"```AppleScript", selectedText, "```", ""} as text
   
   set dialogText to selectedText
   set userPrompt to {"Displaying Selection from Current Script", "", ¬
      "Edit as needed to post on Script Debugger Forum,", "", "Or Copy Entire Script", "", "Click \"Cancel\" to exit without changing the Clipboard"} as text
   set currentDefault to "Copy Selection"
   set displayMode to "Selection"
else
   set displayMode to "Entire Script"
end if
try
   tell application "Script Debugger"
      repeat
         if displayMode is "Selection" then
            set dialogText to selectedText
            set userPrompt to {"Displaying Selection from Current Script", "", ¬
               "Edit as needed to post on Script Debugger Forum,", "", ¬
               "Or Copy Entire Script", "", ¬
               "Click \"Cancel\" to exit without changing the Clipboard"} as text
            set currentDefault to "Use Selection"
            set dialogButtons to {"Cancel", "Use Selection", "Copy Entire Script"}
         else
            set dialogText to entireScript
            set userPrompt to {"Displaying entire current script", "", ¬
               "Edit as needed to post on Script Debugger Forum", "", ¬
               "Click \"Cancel\" to exit without changing the Clipboard"} as text
            set currentDefault to "Entire Script"
            set dialogButtons to {"Cancel", "Entire Script"}
            
         end if
         --DisplayDialog Full
         set userInput to display dialog userPrompt ¬
            default answer dialogText ¬
            buttons dialogButtons ¬
            default button currentDefault ¬
            with title ¬
            "Quote " & currentDefault
         set {userText, userButton} to {text returned of userInput, button returned of userInput}
         if the userButton is "Use Selection" then
            if displayMode is "Selection" then
               set the fourmText to userText
               exit repeat
            end if
         else if displayMode is "Entire Script" then
            
            set the fourmText to userText
            exit repeat
         else
            set dialogText to entireScript
            set displayMode to "Entire Script"
            
         end if
      end repeat
      set fourmText to my ReplaceText(tab, "   ", fourmText)
      set the clipboard to fourmText
   end tell
   return the clipboard
on error errMsg number errNum
   -- your error handler code goes here
end try

on ReplaceText(findString, replaceString, textToFix)
   set saveTID to AppleScript's text item delimiters
   set AppleScript's text item delimiters to {findString}
   set textToFix to every text item of textToFix
   set AppleScript's text item delimiters to {replaceString}
   set textToFix to textToFix as text
   set AppleScript's text item delimiters to saveTID
   return textToFix
end ReplaceText

–> Script Debugger 7.0.1 (7A41)
–> Script Debugger 6.0.8 (6A225)
–> Mac OS 10.11.6 (15G1611)

If it happens again, could you take a screenshot of the error? And double-check that only one version of SD is running at the time?

It happened again, after I updated. I made a new script window, pasted a script into it, edited the script, switched to debugging mode, stepped through it and ran it a few times, then tried to run the script. Got the same error.

So I launched QT and tried to do a screen recording, and it worked. Turned off debugging and it worked. So I quit QU, and tried again and got the same error message.

Opened another script, switched to debugging mode, tried to run the it again and got the error. Turned off debugging and it worked.

–> Script Debugger 7.0.1 (7A45)
–> Mac OS 10.11.6 (15G1611)

Did you manage to get a screenshot of the error?

If it helps, to get this screen shot I had a window with uncompiled text open. I tried the script several times and it worked each time (debugging off and on; no selection; one line selected the entire script selected). Then I opened a script I worked on yesterday. Worked fine with no selection and one line selected, but generated the error with the entire script selected. Same behavior with debugging on or off. The munged text in the error dialog only happened once. The error number is always (-1728)

The un-munged error message:

Correction, the munged error message seems to appear when debugging is on.

Thanks for the screenshots. In your code, you use selection of window whichWindow and current document of window whichWindow, whereas in both cases those are properties of script windows. I’m wondering if that’s the cause of the problem. Can you modify the script and try again?

So far so good. No errors.

In explorer selection and current document appear as properties of window (at least sometimes).

That happens when they are script windows. But when you say window 1, it may or may not be a script window (and recent Apple bugs make this even more of a lottery). So you should be specific unless you really don’t care what type of window it is.

Any idea what this window is? I have three windows open at the moment. Two script windows and the SD dictionary. The explorer lists a “window 4”. It’s bounds (if they were to be real) would make it roughly square and fill the bottom of the slot here to the left of the explorer window and the right of the script window. It may have come up after I did a search in the Info pane of a script.

No idea, but as @ShaneStanley suggests, use the script window collection instead of the windows collection and SD will filter out non-script windows for you.

Why don’t you ask it?

use AppleScript version "2.4" -- Yosemite (10.10) or later
use framework "Foundation"
use framework "AppKit"

current application's NSApp's orderedWindows()

My guess is it’s an LNSFindWrapAroundWindow, aka the window used to show the wraparound image when searching.

Oh, I forgot to mention, when I was doing the multi-dictionary search, the search process window popped up with “XXX” at the end of its text. (I don’t recall if the text was “Searching” or “Searching Dictionaries” or something else, I was going to try to recreate.

I’ll have time to look at it in an hour or two.

OK, the search progress window opens and says “SearchingXXX” that would get replaced by a text for apps whose dictionaries were loading.

The “SearchingXXX” is still present after updating

–> Script Debugger 7.0.1 (7A47)
–> Mac OS 10.11.6 (15G1611)