Dialog Toolkit Plus isMainThread Error

@Ed. The -1708 error message is shown in an appendix to the AppleScript Language Guide as shown below. I’m not sure what to make of either error message :frowning:

The script doesn’t understand the message. The event was not handled.

The ALG appendix is here

Curious to anyone else thoughts on this advice but …I would try a couple of things in order of what I believe will most likely resolve the issue:

  1. Have you considered the error you get is from the display dialog or the reason why the error is lacking info due to error trying to display the error? I had early issues 4-5 years ago mixing both inside apps and came to the conclusion that if I did use both in the same code, I would use System Events to display the dialogs in case the main thread was tied up for some reason. This was due to me always using apps to trigger scripts. Maybe fast scripts is triggering the script and the thread isn’t main… Also, I have had issues with specifying icon stop instead of icon 0 or note as icon 1 in Ventura when I tested my suggestion in your code or Perhaps it’s due to using System Events. Example:

     tell application "System Events"
     	activate
     	display dialog theText buttons {"OK"} cancel button 1 default button 1 with title "Search and Replace" with icon 0
     end tell
    
  2. Instead of using references for the results I typically assign variables. Maybe it is the long way but it’s worth a shot. Example:

instead of

return {item 6, item 4, item 1, item 2, item 3} of controlsResults

try

set searchString to (item 6 of controlResults) as string
set replaceString to (item 4 of controlResults) as string
set searchOption to (item 1 of controlResults) as boolean
set wordOption to (item 2 of controlResults) as boolean
set caseOption to (item.3 of controlResults) as boolean
return {searchString, replaceString, searchOption, wordOption, caseOption}

  1. use ‘my’ when calling subroutines

You could also wrap a ton of steps in try on errors to verify which part of your code fails to confirm its actually in Dialog Toolkit Plus.

Here’s my version of the same script (FWIW). See if this generates the same errors (Also requires PrefsStorageLib). This doesn’t do the search, just displays the dialog.

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

property lastSearchOption : ""
property lastSearchString : ""
property lastReplaceString : ""
property lastMatchWords : ""
property lastCaseSensitive : ""
property lastSearchScope : ""

PersistentVariables()

repeat
	set {searchOption, searchString, replaceString, matchWords, caseSensitive, searchScope} to SearchDialog()
	set {lastSearchOption, lastSearchString, lastReplaceString, lastMatchWords, lastCaseSensitive, lastSearchScope} ¬
		to {searchOption, searchString, replaceString, matchWords, caseSensitive, searchScope}
	StorePersistentValues()
end repeat

on SearchDialog()
	if lastSearchOption is ("") then set lastSearchOption to ("Replace All")
	try
		set viewWidth to 335
		set spacer to 10
		set fieldIndent to (max width for labels {"Search:", "Replace:"}) + 5
		set allControls to {}
		set {theButtons, buttonWidth} to ¬
			create buttons {"Exit Search", "Find Next", "Replace & Find", "Replace All"} ¬
				cancel button 1 default button lastSearchOption
		
		if buttonWidth > viewWidth then set viewWidth to buttonWidth
		set {selectionAllMatrix, theTop} to create matrix {"Search All", "Search Selection"} ¬
			left inset fieldIndent ¬
			bottom 0 ¬
			max width viewWidth - 80 ¬
			arranged vertically false ¬
			initial choice lastSearchScope
		set the beginning of allControls to selectionAllMatrix
		set theBottom to theTop + spacer
		
		set {wordMatchCheckbox, unusedTop, wordWidth} to create checkbox ("Match Words") ¬
			left inset fieldIndent ¬
			bottom theBottom ¬
			max width (viewWidth / 3) - 12 ¬
			initial state lastMatchWords
		
		set the beginning of allControls to wordMatchCheckbox
		
		set {caseSensitiveCheckbox, theTop, caseWidth} to create checkbox ("Consider Case") ¬
			left inset (fieldIndent + wordWidth + 58) ¬
			bottom theBottom ¬
			max width (viewWidth / 3) ¬
			initial state lastCaseSensitive
		
		set the beginning of allControls to caseSensitiveCheckbox
		
		set {replaceField, replaceLabel, theTop, fieldIndent} to create side labeled field (lastSearchString) ¬
			placeholder text (" replace with...") left inset 3 bottom (theTop + spacer) ¬
			total width viewWidth label text ("Replace:") field left fieldIndent
		set the beginning of allControls to replaceField
		set the end of allControls to replaceLabel
		
		set {searchField, searchLabel, theTop, fieldIndent} to create side labeled field (lastReplaceString) ¬
			placeholder text (" search for...") left inset 3 bottom (theTop + spacer) ¬
			total width viewWidth label text ("Search:") field left fieldIndent
		set the beginning of allControls to searchField
		set the end of allControls to searchLabel
		
		set {searchOption, controlsResults} to ¬
			display enhanced window ("Search and Replace") ¬
				buttons theButtons acc view width viewWidth ¬
				acc view height theTop acc view controls allControls ¬
				initial position {} without align cancel button
		set {searchString, replaceString, wordMatch, caseSensitive, searchScope} to controlsResults
		
		return {searchOption, searchString, replaceString, wordMatch, caseSensitive, searchScope}
	on error errMsg number errNum partial result partialError
		local partialError
		partialError
		if errNum ≠ -128 then display dialog errMsg & ("Error number: ") & errNum
		error errMsg number errNum
	end try
end SearchDialog

on PersistentVariables()
	prepare storage for (path to me)
	RetreiveStoredValues()
end PersistentVariables

on StorePersistentValues()
	assign value lastSearchOption to key "lastSearchOption"
	assign value lastSearchString to key "lastSearchString"
	assign value lastReplaceString to key "lastReplaceString"
	assign value lastMatchWords to key "lastMatchWords"
	assign value lastCaseSensitive to key "lastCaseSensitive"
	assign value lastSearchScope to key "lastSearchScope"
end StorePersistentValues

on RetreiveStoredValues()
	set my lastSearchOption to value for key "lastSearchOption"
	set my lastSearchString to value for key "lastSearchString"
	set my lastReplaceString to value for key "lastReplaceString"
	set my lastMatchWords to value for key "lastMatchWords"
	set my lastCaseSensitive to value for key "lastCaseSensitive"
	set my lastSearchScope to value for key "lastSearchScope"
end RetreiveStoredValues

@peavine
Which version of FastScripts 3 are you using?
If it’s not 3.2.4 (the latest, or at least 3.2.3) you should update.

But even though Foundation & AppKit are pre-loaded in FastScripts Runners, you will still have this issue from time to time…

That’s really only required for libs that load frameworks (so Myriad Tables, BridgePlus, etc).

1 Like

Thanks everyone for the responses. I’ve been working through dean’s suggestions, but the issue is so intermittant that it’s slow going.

I took dean’s advice and divided the displayDialog handler into try statements, and the script fails at the statement shown below. I have no idea why that would be the case. BTW, I’ve been running the script by way of a Script-Debugger key binding, so FastScripts is not involved.

	try
		set {theButtons, minWidth} to create buttons {"Cancel", "OK"} default button 2 cancel button 1 without equal widths
	on error
		display dialog "The buttons"
		error number -128
	end try

See if the error will return a partial result. That may help narrow things down more.

1 Like

The only thing I see different about the format we follow and code is

on create buttons buttonList button keys buttonKeysList : {} equal widths equalWidths : (false) default button okIndex : 0 cancel button cancelIndex : 0

has equal widths prior to the cancel and default buttons. It may be worth removing without equal widths to see if you still error.

Your problem is a known issue with AppleScriptObjC scripts on recents systems (not sure if it’s Catalina or Big Sur): the system randomly fails to load frameworks.

FastScript is not involved at all. But it has an advantage on other Script menus.
That’s why I recommended to check out your FastScripts version.

Do you remember this topic?

Thanks Jonas. I wasn’t aware (or didn’t recall) that there was a problem with recent systems failing to load frameworks. My script fails at the exact point where a Dialog Toolkit Plus command is first run, so what you say makes sense.

You guys jinxed me. Now Im having the same issue! @ShaneStanley — help! It’s only when calling a dialog using dialog toolkit plus v1.1.3. The error happens less when using 1.1.2.

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

The change in version is pure co-incidence.

it may seem like one but when I use:

use script “Dialog Toolkit Plus”

and compile/save. I get error. When I have the 1.1.2 library in Script Libraries and named it differently…and

use script “Dialog Toolkit Plusv2”

compile and save. I don’t get the error.

@ShaneStanley so I tried experiment…and changed my script at beginning to use AppleScript 2.4 instead of using AppleScript 2.5. Then both Libraries error’d. Then switched back and now they both do. Do u think its related?

In a word, no. Compilation issues are completely unrelated.

@ShaneStanley Thanks for your patience…I have GREAT news!

So in further testing. This is what I figured out:

  1. v 1.1.2 original file dated 12/8 on my Mac in ~/Script Libraries it works fine on my Mac
  2. v 1.1.3 original downloaded it does not.
  3. If I duplicate the v 1.1.2 and resave it on my Mac Ventura 13.1 adding to name resaved. it errors.

So I looked at the differences…your old one had empty foundation folder…added same to new one nope didn’t work. Looked at the plist files and bingo different. I tried your old plist file and updated the version to match the latest you had saved and it works great not one problem.

So…If I use your plist file from v 1.1.2 update it to 1.1.3. It WORKS Perfect!!!

The extras are the differences…
OSAAppleScriptObjCEnabled
TemplateForCFBundleIdentifier
and the 8 items under WindowState

I think the reason you’re unable to simulate/test is because a test script runs. But if you ran that script from another script it would not…ie: FastScripts or Menubar apps or Scripts running other scripts and gets the original error from the original version 1.1.3. I’m sending the updated version with the plist to u now.

dean. I don’t completely understand what changes you’ve made. Is there some way for me to test this? Or should I just wait while you and Shane resolve this? Thanks.

Here’s the zipped file you can test.

Dialog Toolkit PlusUpdated.zip (110.6 KB)

Thanks. I replaced the old one with the updated version, which seems to work fine. I only encounter this issue once or twice a day, and I’ll report back after a day or so,

1 Like