Dialog Toolkit Plus isMainThread Error

@ShaneStanley I am receiving an error using Dialog Toolkit Plus v 1.1.3

Error -1708 : NSThread doesn’t understand the “isMainThread” message. it is in Enhanced App running in Script Debugger OR as an App.

I believe it is displaying the error instead of another error in a subroutine. Trying to narrow it down.

@dean. Did you every resolve this issue? I’ve encountered a similar problem with the same error message here

Thanks.

What I found is the error is not the real error. Reporting the error was failing because it wasn’t “isMainThread”. I tried forcing it to be the main thread on error and reported a cryptic error.

Instead I had to remove line by line the parts of the dialog I was creating. On mine, the real issue was in create labeled path control to a posix path of “” or “/” for some reason didn’t work. I pointed it to posix path of home as default and the error stopped. Basically what I really learned is if I want to use the library, I need to build dialogs line by line and test. A lot of the old scripts Shane created as an example have an incorrectly formed Buttons line as well. I believe he changed the format in recent versions.

1 Like

Thanks dean–that’s very helpful. I’ll look through my dialog handler to see if anything appears amiss.

I worked my way through my dialog handler, comparing it against the applicable dictionary and Shane’s example Dialog Toolkit Plus scripts. I made a few changes but none fixed the issue.

This is a script that I use many times every day, and I never encountered this issue until I got a new computer which came with Ventura. My previous computer had Monterey. So, perhaps something else is causing the issue.

FWIW, the following is my dialog handler. Run by itself, this handler has never presented an error.

use framework "Foundation"
use script "Dialog Toolkit Plus" version "1.1.0"
use scripting additions

displayDialog()

on displayDialog()
	set dialogWidth to 330
	set verticalSpace to 10
	set {theButtons, minWidth} to create buttons {"Cancel", "OK"} default button 2 cancel button 1 given «class btns»:2 -- tested without given «class btns»:2
	set {searchCheckbox, unusedTop, searchWidth} to create checkbox "Search All" left inset 0 bottom 0 max width (dialogWidth / 3)
	set {wordCheckbox, unusedTop, wordWidth} to create checkbox "Match Words" left inset (searchWidth + 8) bottom 0 max width (dialogWidth / 3)
	set {caseCheckbox, theTop, caseWidth} to create checkbox "Consider Case" left inset (searchWidth + wordWidth + 16) bottom 0 max width (dialogWidth / 3)
	set {replaceField, replaceLabel, theTop, fieldLeft} to create side labeled field "" placeholder text "replace with..." left inset 0 bottom (theTop + verticalSpace) total width dialogWidth label text "Replace:" field left 0
	set {searchField, searchLabel, theTop, fieldLeft} to create side labeled field "" placeholder text "search for..." left inset 0 bottom (theTop + verticalSpace) total width dialogWidth label text "Search:" field left 0
	set allControls to {searchCheckbox, wordCheckbox, caseCheckbox, replaceField, replaceLabel, searchField, searchLabel}
	set {buttonName, controlsResults} to display enhanced window "Search and Replace" buttons theButtons acc view width dialogWidth acc view height theTop acc view controls allControls initial position {} without align cancel button
	return {item 6, item 4, item 1, item 2, item 3} of controlsResults
end displayDialog
1 Like

The version I am now using is 1.1.2. You should download the latest. I guess should have asked that first.

Also, I have experienced at least concern if not issues and after updating modified all of my dialogs to follow the dictionary for ‘create buttons’ and would reformat the line to:

set {theButtons, minWidth} to create buttons {“Cancel”, “OK”} default button 2 cancel button 1 equal widths false

Assuming you know this but in case you didn’t, you can can drag and drop the functions like create buttons from the dictionary panel in SD directly to see the format to follow with all of its options.

2 Likes

Thanks dean. I changed the create buttons line and will see if that helps. I am using version 1.1.2. I wasn’t aware you could drag and drop functions like that–that’s a nice feature.

The code you posted used:

use script “Dialog Toolkit Plus” version “1.1.0”

Thanks. I just checked and I’m actually using Dialog Toolkit Plus 1.1.3. I’ll change the use statement in my script to see if that makes a difference.

Despite my best efforts, this issue persists, and it’s time to move on with other stuff. Thanks again dean for your help.

I’m 99% you have multiple libraries installed if the v 1.1.0 worked in your code. Is it in your bundle or script libraries or somewhere else? Search for Dialog Toolkit Plus on you Mac and remove all. Then remove any from your bundle if using a bundle or app. Then reboot and add v1.1.0 to your user/script libraries.

1 Like

@dean. I confused this issue by reporting incorrect information:

  • My script worked reliably on Monterey on a 2018 Mac mini with Dialog Toolkit Plus 1.1.3 installed in my home folder in the Script Libraries subfolder. The use-script statement in my script referenced Dialog Toolkit Plus version 1.1.0.

  • My script has never worked reliably on Ventura on a 2023 Mac mini with Dialog Toolkit Plus 1.1.3 installed in my home folder in the Script Libraries subfolder. The use-script statement in my script referenced Dialog Toolkit Plus version "1.1.0. I changed the use-script statement to Dialog ToolkitPlus version 1.1.3 and still received the error.

  • I did a search and found one rarely-used AppleScript applet that contained Dialog Toolkit Plus version 1.1.3 in its Resources folder, and I removed that. I did not find any other instances of Dialog Toolkit Plus on any internal or external drives (other than the one in my home folder). The script still does not work reliably.

I know it can be very frustrating. I have gone through this. The OS and any Saved Library calling the Dialog Toolkit Plus or Scripts can somehow store the library if not resaved. I would completely remove any Dialog Toolkit Plus from your Script Libraries subfolder. Also make sure if you have Shane’s Script Library Pack.app anywhere on your Mac that it is the current version. My money is on the OS Caching or an old Library Pack being on your Mac. Then I would compile your script to intentionally get an error of unable to compile to be sure it isn’t using another version somewhere. If it compiles, you have a copy somewhere. If not, place it into the Script Libraries folder and compile.

1 Like

That’s all good advice, but it should be pointed out that Dialog Toolkit Plus is not part of Shane’s Script Library Pack.

It has three script libaries:

  • “BridgePlus.scptd”
  • “SQLite Lib2.scptd”
  • “Myriad Tables Lib.scptd”

Also, any time you make changes to a script library (including moving or deleting) make sure you quit and relaunch Script Debugger, or it will use the previous library version from cache.

3 Likes

I have a question about how this error is displayed, because there are two modes for errors when using script libraries.

If the command with the error occurs in the library script, and the command is not within a try block, then the error message suggests opening the script library to track the error, and sometimes, that’s not very helpful.

But, if the command with the error is executed within a try block, then you’ll almost always get better information about the error.

If you haven’t already put your Dialog Toolkit script inside this kind of try block:

try
--dialog toolkit commands here
	on error errMsg number errNum partial result partialError
		set AppleScript's text item delimiters to {return}
		display dialog errMsg & ("Error number: ") & errNum & return & partialError as text
end try

Thanks Ed. I tried your suggestion and received the same error message–only it’s formatted a bit differently:

NSThread doesn’t understand the “isMainThread” message.Error number: -1708

BTW, I downloaded a new copy of Dialog Toolkit Plus and replaced the existing copy. My though was that something might be corrupted, but I still got the error message.

Still the same error code:
Mac Error Codes

error -1708 errAEEventNotHandled: the AppleEvent was not handled by any handler

1 Like

Since the dialog toolkit is editable maybe it’s worth opening and putting something like log “whatever” or display dialog “yep I’m using this library” inside the code to display the dialog. This way you’ll see it when you run the code. I’m not sure how it would run at all with an older version number then what was there.

@estockly i completely agree. That’s a good call!

Thanks @dean for the message and for the offer of assistance. I’ve included my script below (in case anyone else wants to have a look). I run the script by way of FastScripts 3, although I receive the same error if run by way of a Script Debugger keyboard shortcut. As mentioned before, the error is very intermittent, occurring 3 or 4 times a day, and the error occurs when the script is first run (the dialog is never shown).

The script does a search and replace with Script Debugger, and the script assumes that Script Debugger is running. In my use, the script is set in FastScripts as app specific, so it will not run if Script Debugger is not running. That’s why the script does not check to see if Script Debugger is running.

-- revised 2023.02.21

use framework "Foundation"
use script "Dialog Toolkit Plus" version "1.1.3"
use scripting additions

on main()
	set {searchString, replaceString, searchOption, wordOption, caseOption} to displayDialog()
	if searchString = "" then errorDialog("A search string was not entered")
	searchAndReplace(searchString, replaceString, searchOption, wordOption, caseOption)
end main

on displayDialog()
	set dialogWidth to 330
	set verticalSpace to 10
	set {theButtons, minWidth} to create buttons {"Cancel", "OK"} default button 2 cancel button 1 without equal widths
	set {searchCheckbox, unusedTop, searchWidth} to create checkbox "Search All" left inset 0 bottom 0 max width (dialogWidth / 3)
	set {wordCheckbox, unusedTop, wordWidth} to create checkbox "Match Words" left inset (searchWidth + 8) bottom 0 max width (dialogWidth / 3)
	set {caseCheckbox, theTop, caseWidth} to create checkbox "Consider Case" left inset (searchWidth + wordWidth + 16) bottom 0 max width (dialogWidth / 3)
	set {replaceField, replaceLabel, theTop, fieldLeft} to create side labeled field "" placeholder text "replace with..." left inset 0 bottom (theTop + verticalSpace) total width dialogWidth label text "Replace:" field left 0
	set {searchField, searchLabel, theTop, fieldLeft} to create side labeled field "" placeholder text "search for..." left inset 0 bottom (theTop + verticalSpace) total width dialogWidth label text "Search:" field left 0
	set allControls to {searchCheckbox, wordCheckbox, caseCheckbox, replaceField, replaceLabel, searchField, searchLabel}
	set {buttonName, controlsResults} to display enhanced window "Search and Replace" buttons theButtons acc view width dialogWidth acc view height theTop acc view controls allControls initial position {} without align cancel button
	return {item 6, item 4, item 1, item 2, item 3} of controlsResults
end displayDialog

on searchAndReplace(searchString, replaceString, searchOption, wordOption, caseOption)
	tell application id "com.latenightsw.ScriptDebugger8" to tell document 1
		if searchOption is false then
			set selectedText to selection
			set {c1, c2} to character range of selection
		else
			set selectedText to source text
			set selection to {1, (count selectedText)}
		end if
	end tell
	if selectedText = "" then errorDialog("A text selection was not found")
	set selectedText to current application's NSMutableString's stringWithString:selectedText
	
	if wordOption is true and caseOption is true then
		set searchPattern to "\\b" & searchString & "\\b"
		set optionText to "The match-words and consider-case search options were enabled."
	else if caseOption is true then
		set searchPattern to searchString
		set optionText to "The consider-case search option was enabled."
	else if wordOption is true then
		set searchPattern to "(?i)" & "\\b" & searchString & "\\b"
		set optionText to "The match-words search option was enabled."
	else
		set searchPattern to "(?i)" & searchString
		set optionText to "No search options were enabled."
	end if
	
	set searchStringMatches to (selectedText's replaceOccurrencesOfString:searchPattern withString:replaceString options:(current application's NSRegularExpressionSearch) range:{0, selectedText's |length|()})
	if searchStringMatches = 0 then errorDialog("The search string " & quote & searchString & quote & " was not found in the selected text. " & optionText)
	set selectedText to selectedText as text
	
	if replaceString = "" then
		if searchStringMatches = 1 then
			set dialogText to "Delete 1 instance of " & quote & searchString & quote
		else
			set dialogText to "Delete " & searchStringMatches & " instances of " & quote & searchString & quote
		end if
	else
		if searchStringMatches = 1 then
			set dialogText to "Replace 1 instance of " & quote & searchString & quote & " with " & quote & replaceString & quote
		else
			set dialogText to "Replace " & searchStringMatches & " instances of " & quote & searchString & quote & " with " & quote & replaceString & quote
		end if
	end if
	display dialog dialogText with title "Search and Replace" with icon note
	
	tell application id "com.latenightsw.ScriptDebugger8" to tell document 1
		set selection to selectedText
		if searchOption is false then
			set c2 to c2 + (((count replaceString) - (count searchString)) * searchStringMatches)
			set selection to {c1, c2}
		else
			set selection to {1, 0}
			set selection to {1, (count selectedText)}
		end if
	end tell
end searchAndReplace

on errorDialog(theText)
	display dialog theText buttons {"OK"} cancel button 1 default button 1 with title "Search and Replace" with icon stop
end errorDialog

main()