Tuesday, August 20, 2019

#26 2009-11-09 01:27:21 am

Shane Stanley
Member
From:: Australia
Registered: 2002-12-07
Posts: 5825

Re: user defaults

Defaults consist of key-value pairs -- the key is how you identify a particular default, and the value is, well, the value. registerDefaults_ wants them as a dictionary, and the AS equivalent is a record. You can pass them all in one go.

But registerDefaults_ values aren't saved anywhere: it's what you use to establish default values if the user hasn't set any.

Last edited by Shane Stanley (2009-11-09 01:27:42 am)


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/
latenightsw.com

Offline

 

#27 2010-06-14 05:46:06 pm

Dylan Weber
Member
From:: IL, USA
Registered: 2009-08-07
Posts: 539

Re: user defaults

Well... I'm having a problem and I can't figure it out. Here is my application. It is suppost to automaticly save your user information as you type it. The problem is that there are errors with the reading and writing of the username and password via NSUserDefaults that causes me to be confused. Can someone help?

It's giving me these errors:

2010-06-14 18:37:12.310 iTwitter[1168:4103] *** -[iTwitterAppDelegate standardUserDefaults]: unrecognized selector sent to object <iTwitterAppDelegate @0x2006444c0: OSAID(11)>


2010-06-14 18:37:12.318 iTwitter[1168:4103] *** -[iTwitterAppDelegate recoversettings]: *** -[iTwitterAppDelegate standardUserDefaults]: unrecognized selector sent to object <iTwitterAppDelegate @0x2006444c0: OSAID(11)> (error -10000)

Last edited by Dylan Weber (2010-06-14 05:47:18 pm)


I am addicted to this site.
Look, over 500 posts! I barley help! :O
My specialty is replacing common AppleScript commands with hard-to-work-with methods.

Offline

 

#28 2010-06-14 06:09:33 pm

leonsimard
Member
From:: Montreal, Canada
Registered: 2009-09-27
Posts: 531

Re: user defaults

Hmmm. Maybe you need to coerce them to string? I know that you need to do that more in ASOC than ASS ever needed. Not much help am I hmm...

Browser: Safari 531.22.7
Operating System: Mac OS X (10.6)

Offline

 

#29 2010-06-14 06:16:57 pm

leonsimard
Member
From:: Montreal, Canada
Registered: 2009-09-27
Posts: 531

Re: user defaults

After looking at your code, I noticed this:

Applescript:

on recoversettings()
       tell class "NSUserDefaults"
           tell its standardUserDefaults()
               set username to its stringForKey_("username")
               theusername's setStringValue_(username)
           end tell
       end tell
       beep
   end recoversettings

And my routine (that saves and retrieves strings fine) is a bit different:

Applescript:

on writeDefaults()
       tell NSUserDefaults
           tell its standardUserDefaults()
               its setObject_forKey_(theLogDataSource, "theLogDataSource")
               its setObject_forKey_(prefDefaultAppLang, "prefDefaultAppLang")
           end tell
       end tell
   end writeDefaults

Applescript:

on readDefaults()
       tell NSUserDefaults
           tell its standardUserDefaults()
               set theLogDataSource to its objectForKey_("theLogDataSource")
               set prefDefaultAppLang to its objectForKey_("prefDefaultAppLang")
           end tell
       end tell
   end readDefaults

NSUserDefaults has been defined at the top of the app delegate like this:

Applescript:

property NSUserDefaults : class "NSUserDefaults" of current application

Helps?

Browser: Safari 531.22.7
Operating System: Mac OS X (10.6)

Offline

 

#30 2010-06-14 06:39:50 pm

Dylan Weber
Member
From:: IL, USA
Registered: 2009-08-07
Posts: 539

Re: user defaults

DUHHHHH... I forgot to put "of current application."

tongue


I am addicted to this site.
Look, over 500 posts! I barley help! :O
My specialty is replacing common AppleScript commands with hard-to-work-with methods.

Offline

 

#31 2010-06-14 06:41:12 pm

leonsimard
Member
From:: Montreal, Canada
Registered: 2009-09-27
Posts: 531

Re: user defaults

So I guess it works now! wink

Offline

 

#32 2011-06-13 04:35:22 pm

crgand
Member
From:: Milano, Italia
Registered: 2008-11-15
Posts: 39
Website

Re: user defaults

I've followed #4 and #12 to manage check boxes defaults
This is what I did:

Applescript:


   --IBOutlets
   property engine00 : missing value
   property engine01 : missing value

   -- Bindings
   property theEngine00 : ""
   property theEngine01 : ""

   -- to set factory default at first open
   on setDefaults()
       set prefs to {theEngine00:true, theEngine01:true}
       set userDefaults to current application's class "NSUserDefaults"'s standardUserDefaults()
       userDefaults's registerDefaults_(prefs)
   end setDefaults

   -- to write defaults when quit
   on writeDefaults()
       tell NSUserDefaults
           tell its standardUserDefaults()
           its setBool_forKey_(theEngine00, "theEngine00")
           its setBool_forKey_(theEngine01, "theEngine01")
           end tell
       end tell
   end writeDefaults_

   -- to read defaults next open
   on readDefaults()
       tell NSUserDefaults
           tell its standardUserDefaults()
               set theEngine00 to its boolForKey_("theEngine00")
               my setTheEngine00_(theEngine00)
               set theEngine01 to its boolForKey_("theEngine01")
               my setTheEngine01_(theEngine01)
           end tell
       end tell
   end readDefaults

   -- to load defaults when app open
   on awakeFromNib()
       my setDefaults()
       my readDefaults()    
   end awakeFromNib

   -- to save defaults when app quit
   on applicationShouldTerminate_(sender)
       -- Insert code here to do any housekeeping before your application quits
       my writeDefaults()
       return true
   end applicationShouldTerminate_

I wish a button that revert to factory defaults, I did

Applescript:


   on revertDefaults_(sender)
       setDefaults_(me)
       readDefaults_(me)
       writeDefaults_(me)
   end revertDefault_

but nothing happen.
How can I set check box and its default to true/checked by code?

Edit: when I click on my revertDefault button I get: unrecognized selector sent to object

Last edited by crgand (2011-06-13 04:42:42 pm)

Offline

 

#33 2011-06-13 05:32:12 pm

Shane Stanley
Member
From:: Australia
Registered: 2002-12-07
Posts: 5825

Re: user defaults

Make userDefaults a property, and change the two cases of "tell NSUserDefaults" -- which you don't define anywhere -- to "tell userDefaults".


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/
latenightsw.com

Offline

 

#34 2011-06-14 06:20:17 am

crgand
Member
From:: Milano, Italia
Registered: 2008-11-15
Posts: 39
Website

Re: user defaults

Shane Stanley wrote:

"tell NSUserDefaults" -- which you don't define anywhere


I forget to write on my previous post that NSUserDefaults is defined at the beginning with:

Applescript:

property NSUserDefaults : class "NSUserDefaults" of current application

Sorry using tell  userDefault didn't work.

Last edited by crgand (2011-06-14 06:23:13 am)

Offline

 

#35 2011-06-14 07:29:03 am

StefanK
Member
From:: St. Gallen, Switzerland
Registered: 2006-10-21
Posts: 11578
Website

Re: user defaults

crgand wrote:

I wish a button that revert to factory defaults, I did

Applescript:


   on revertDefaults_(sender)
       setDefaults_(me)
       readDefaults_(me)
       writeDefaults_(me)
   end revertDefault_


Easiest solution: NSUserDefaultsController

Applescript:


script myAppDelegate
   property parent : class "NSObject"
   property NSDictionary : class "NSDictionary"
   property NSUserDefaults : class "NSUserDefaults"
   property NSUserDefaultsController : class "NSUserDefaultsController"
   
   --IBOutlets
   property engine00 : missing value
   property engine01 : missing value
   
   
   on awakeFromNib()
       set prefs to {theEngine00:true, theEngine01:false}
       set userDefaults to NSUserDefaults's standardUserDefaults()
       userDefaults's registerDefaults_(prefs)
       set revertDictionary to NSDictionary's dictionaryWithDictionary_(prefs)
       set sharedUserDefaultsController to NSUserDefaultsController's sharedUserDefaultsController()
       sharedUserDefaultsController's setInitialValues_(revertDictionary)    
   end awakeFromNib
   
   on revertDefaults_(sender)
       set sharedUserDefaultsController to NSUserDefaultsController's sharedUserDefaultsController()
       sharedUserDefaultsController's revertToInitialValues_(me)
   end revertDefaults_

end script

In InterfaceBuilder bind the value of check box engine00 to key theEngine00 and check box engine01 to key theEngine01 of Shared User Defaults Controller.

In awakeFromNib() the initial values are defined in the controller
When you press the revert button, the initial values are reset.
Further reading, writing and synchronizing is not needed. NSUserDefaultsController does the rest

Last edited by StefanK (2011-06-14 07:32:47 am)


regards

Stefan

Offline

 

#36 2011-06-14 02:33:22 pm

crgand
Member
From:: Milano, Italia
Registered: 2008-11-15
Posts: 39
Website

Re: user defaults

Thanks a lot StefanK, it works perfect. smile
I've also binded the Visible parameter of a Drawer object to retrive it state of opened or closed.

Offline

 

#37 2019-05-30 01:29:19 am

moritzz
Member
Registered: 2011-02-20
Posts: 2

Re: user defaults

Thanks @crgand!, you're input was very helpful to me. ????

Last edited by moritzz (2019-05-30 01:30:21 am)

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)