User defaults

A quick question:

I have a tricky routine for storing an encrypted password that revolves around checking if a key in user defaults exists, that then sets another user default :

i

if default entry "txtfld" exists then
			try
				set textcryp to contents of default entry "txtfld" as string
				if textcryp is not equal to "" then
					set passwordtext to my decrypt(textcryp)
				else
					set passwordtext to ""
				end if
			end try
		else
			try
				if default entry "passwordtext" exists then
					set temptxt to contents of default entry "passwordtext" as string
					set passwordtext to temptxt
					if passwordtext is not equal to "" then
						set textcrypb to my encrypt(temptxt)
						set contents of default entry "passwordtext" to ""
					else
						set textcrypb to ""
					end if
				else
					set textcrypb to ""
				end if
			end try
			make new default entry at end of default entries with properties {name:"txtfld", contents:textcrypb}
              end if

I am not concerned about the above script so much but if it possible in ASOC to check if a key exists in defaults.

I know you can user registerDefaults as a way to initialize the defaults values but it will be tricky to use that for this. Any ideas? I looked in the docs but nothing about the key values themselves…

Thanks, Rob

Hi All,

I didn’t trust this at first but I guess it is the accepted method:

tell current applications class "NSUserDefaults"'s standardUserDefaults
set password to stringForKey_("passkey")
if password is in {null,missing value} then

--key does not exist , do accordingly

end if
end tell

That simple. I thought there would be a more direct way of checking if the key exists rather than basically throwing an error, but this works…

Rob