Page 1 of 1

User-Level Application Preferences using CoreFoundation

Posted: Mon Apr 27, 2009 6:41 am
by Airr

Code: Select all

;
; ------------------------------------------------------------
;
;   MacOS User-Level Application Preferences 
;
;   using the CoreFoundation Framework
;
;   example by AIR.
;
;   Prefs file is saved to ~/Library/Preferences automatically
; ------------------------------------------------------------
;

 textColorKey.s="defaultTextColor"
 app_plist.s="org.pcode.testapp"


ImportC "/System/Library/Frameworks/CoreFoundation.framework/CoreFoundation"
  CFPreferencesSetAppValue(l1,l2,l3)
  CFPreferencesCopyAppValue(l1,l2)
  CFStringCreateWithCString(l1,s1.s,l2)
  CFStringGetCStringPtr(l1,l2)
  CFPreferencesAppSynchronize(l1)
EndImport

Procedure SetProperty(key.s,value.s,plist.s)
  CFPreferencesSetAppValue(CFStringCreateWithCString(0,key,0),CFStringCreateWithCString(0, value, 0),CFStringCreateWithCString(0, plist ,0))
EndProcedure

Procedure SavePlist(plist.s)
  CFPreferencesAppSynchronize(CFStringCreateWithCString(0, plist, 0))
EndProcedure

Procedure.s GetProperty(key.s,plist.s)
  chkVal = CFPreferencesCopyAppValue(CFStringCreateWithCString(0,key,0),CFStringCreateWithCString(0, plist ,0))
  
  If chkVal
    *tmpPtr = CFStringGetCStringPtr(chkVal,0)  
    *tmp.String = @*tmpPtr
    ProcedureReturn *tmp\s
  Else
    ProcedureReturn ""
  EndIf  
EndProcedure


SetProperty(textColorKey,"GREAN is MISSPELLED!",app_plist)
SetProperty("Author","Armando I. Rivera",app_plist)
SavePlist(app_plist)

Debug GetProperty(textColorKey,app_plist)
Debug GetProperty("Author",app_plist)
You can double-check the values from a terminal with:

Code: Select all

defaults read org.pcode.testapp
AIR.

Posted: Sun May 03, 2009 11:50 pm
by Fred
Nice to see some native OS X calls in PB :)

Re: User-Level Application Preferences using CoreFoundation

Posted: Wed May 11, 2011 10:39 am
by jamirokwai
Airr wrote:

Code: Select all

;
; ------------------------------------------------------------
;
;   MacOS User-Level Application Preferences 
;
;   using the CoreFoundation Framework
;
;   example by AIR.
;
;   Prefs file is saved to ~/Library/Preferences automatically
; ------------------------------------------------------------
Grrrrreat!
Will come in handy soon, I suppose :-)
Thanks!