Error when getting NSUserDefaults from within DEVONthink

Hi, in Script: Split RTF(D) at Font Sizes I get DEVONthink’s rich text font and font size via NSUserDefaults. This works if run from Script Debugger but now that I finished the script I found that it doesn’t seem to be possible to get NSUserDefaults if ran from the scripts menu,

This line …

set theDefaults to current application's NSUserDefaults's alloc()'s initWithSuiteName:"com.devon-technologies.think3"

… returns missing value which leads to error

1

It doesn’t seem to be a general problem, it’s e.g. possible to get DEVONagent’s NSUserDefaults from DEVONthink’s script menu.

Test - DEVONagent's NSUserDefaults
-- Test - DEVONagent NSUserDefaults from within DEVONthink

use AppleScript version "2.4"
use framework "Foundation"
use scripting additions

try
	set theDefaults to current application's NSUserDefaults's alloc()'s initWithSuiteName:"com.devon-technologies.agent"
	set theTest to ((theDefaults's dictionaryRepresentation())'s stringForKey:"AppleInterfaceStyle")
	display dialog (theTest as string)
	
on error error_message number error_number
	activate
	if the error_number is not -128 then display alert "Error: \"Test NSUserDefaults\"" message error_message as warning
	error number -128
end try

But this fails

-- Test - NSUserDefaults from within DEVONthink

use AppleScript version "2.4"
use framework "Foundation"
use scripting additions

try
	set theDefaults to current application's NSUserDefaults's alloc()'s initWithSuiteName:"com.devon-technologies.think3"
	set theFontName to ((theDefaults's dictionaryRepresentation())'s stringForKey:"RichFontName")
	
	--> Error: missing value versteht die Nachricht „dictionaryRepresentation“ nicht.
	
	display dialog (theFontName as string)
	
on error error_message number error_number
	activate
	if the error_number is not -128 then display alert "Error: \"Test NSUserDefaults\"" message error_message as warning
	error number -128
end try

Any idea what’s going on?

Did you try to explicitly use DEVONthink instead of the current application?

Nope, but code below works. Found the solution at the Script Debugger forum Storing Persistent Values in Preferences. However Apple’s documentation doesn’t mention this :man_shrugging: Thanks and have a good weekend!

-- Test - NSUserDefaults from within DEVONthink

use AppleScript version "2.4"
use framework "Foundation"
use scripting additions

try
	if current application's id = "com.devon-technologies.think3" then -- https://forum.latenightsw.com/t/storing-persistent-values-in-preferences/864
		set theDefaults to current application's NSUserDefaults's standardUserDefaults()
	else
		set theDefaults to current application's NSUserDefaults's alloc()'s initWithSuiteName:"com.devon-technologies.think3"
	end if
	
	set theFontName to ((theDefaults's dictionaryRepresentation())'s stringForKey:"RichFontName")
	display dialog (theFontName as string)
	
on error error_message number error_number
	activate
	if the error_number is not -128 then display alert "Error: \"Test NSUserDefaults\"" message error_message as warning
	error number -128
end try

Thanks, have a nice and hopefully sunny one too!

Umm…

tell application "System Events"
	tell property list file "~/library/preferences/com.devon-technologies.think3.plist"
		display alert "Activity Pane Visible: " & value of property list item "RichFontName"
	end tell
end tell