User-Level Application Preferences using CoreFoundation

Mac OSX specific forum
User avatar
Airr
User
User
Posts: 49
Joined: Tue Oct 04, 2005 4:29 am
Contact:

User-Level Application Preferences using CoreFoundation

Post 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.
"Programming is like Poetry. Sometimes the words just escape you..." -me, to my manager.
Fred
Administrator
Administrator
Posts: 18162
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Post by Fred »

Nice to see some native OS X calls in PB :)
jamirokwai
Enthusiast
Enthusiast
Posts: 796
Joined: Tue May 20, 2008 2:12 am
Location: Cologne, Germany
Contact:

Re: User-Level Application Preferences using CoreFoundation

Post 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!
Regards,
JamiroKwai
Post Reply