Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Why am I unable to register default values in NSUserDefaults, Applescript Objc?

No matter what I do, the values that I register as default, using NSUserDefaults in my Cocoa-Applescript application (in Xcode), return "missing value" when I try to retrieve the values associated with their key.


Here's the code I'm using:


my RegisterKeyDefault:{"Username", "red_menace"}

on RegisterKeyDefault:{Key_ as text, Value_ as text}
  tell standardUserDefaults() of (current application)'s NSUserDefaults to registerDefaults:{Key_:Value_}
end RegisterKeyDefault:


Keep in mind that in Xcode, the call to the function is in the function called at launch, and the function I made is outside of that function. I don't get any compiling errors or errors while running the app.


I also checked the saved values using the "defaults" command in Terminal, and none of them were saved there either.


What am I doing wrong?


Edit:

When I arbitrarily set the the value of the key, then it saves.

Posted on Jul 4, 2015 8:53 PM

Reply
Question marked as Best reply

Posted on Jul 4, 2015 10:11 PM

Regular AppleScript can't use variables for record keys, so your handler isn't setting what you think it is. You can pass a regular record to your handler, or use something like NSDictionary's dictionaryWithObject:forKey: to create a dictionary/record item from your variables and use that, for example:


on RegisterKeyDefault:{Key_ as text, Value_ as text}
  set example to NSDictionary's dictionaryWithObject:Value_ forKey:Key_
  tell standardUserDefaults() of (current application)'s NSUserDefaults to registerDefaults:example
end RegisterKeyDefault:
1 reply
Question marked as Best reply

Jul 4, 2015 10:11 PM in response to Apple_For_The_Win

Regular AppleScript can't use variables for record keys, so your handler isn't setting what you think it is. You can pass a regular record to your handler, or use something like NSDictionary's dictionaryWithObject:forKey: to create a dictionary/record item from your variables and use that, for example:


on RegisterKeyDefault:{Key_ as text, Value_ as text}
  set example to NSDictionary's dictionaryWithObject:Value_ forKey:Key_
  tell standardUserDefaults() of (current application)'s NSUserDefaults to registerDefaults:example
end RegisterKeyDefault:

Why am I unable to register default values in NSUserDefaults, Applescript Objc?

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.