Difference between revisions of "Mac Preferences Read and Write"

From Lazarus wiki
Jump to navigationJump to search
(New Page about "Mac Preferences Read and Write".)
 
(Changed intro sentence wording from "this can be done by the following methods:" to "this can be done using the following methods:")
Line 1: Line 1:
For those who want to read and write preferences for an application on the Mac, this can be done by the following methods:
+
For those who want to read and write preferences for an application on the Mac, this can be done using the following methods:
  
 
{| class="wikitable"
 
{| class="wikitable"

Revision as of 06:24, 22 July 2012

For those who want to read and write preferences for an application on the Mac, this can be done using the following methods:

CODE FOR READING PREFERENCES
 uses MacOSAll, CFPreferences;
 var
   IValid: Boolean;
   Pref: Integer;
 procedure TForm1.FormCreate(Sender: TObject);
 begin
   try
      Pref := CFPreferencesGetAppIntegerValue(CFStr('Check1'),kCFPreferencesCurrentApplication,IValid);
      if (Pref = 1) then
      begin
         CheckBox1.Checked := true;
      end
      else begin
         CheckBox1.Checked := false;
      end;
      Pref := CFPreferencesGetAppIntegerValue(CFStr('Check2'),kCFPreferencesCurrentApplication,IValid);
      if (Pref = 1) then
      begin
         CheckBox2.Checked := true;
      end
      else begin
         CheckBox2.Checked := false;
      end;
      Pref := CFPreferencesGetAppIntegerValue(CFStr('Check3'),kCFPreferencesCurrentApplication,IValid);
      if (Pref = 1) then
      begin
         CheckBox3.Checked := true;
      end
      else begin
         CheckBox3.Checked := false;
      end;
      Pref := CFPreferencesGetAppIntegerValue(CFStr('Check4'),kCFPreferencesCurrentApplication,IValid);
      if (Pref = 1) then
      begin
         CheckBox4.Checked := true;
      end
      else begin
         CheckBox4.Checked := false;
      end;
   except
     on E : Exception do
       ShowMessage(E.ClassName+' error raised, with message : '+E.Message);
   end;
 end;

Prefs1.png


CODE FOR WRITING PREFERENCES
 uses MacOSAll, CFPreferences;
 var
   ItemName: CFStringRef;
   ItemVal: CFPropertyListRef;
 procedure TForm1.FormClose(Sender: TObject; var CloseAction: TCloseAction);
 begin
   try
      if (CheckBox1.Checked) then
      begin
         ItemName := CFStr('Check1');
         ItemVal := CFStringCreateWithPascalString(kCFAllocatorDefault,'1',kCFStringEncodingUTF8);
         CFPreferencesSetAppValue(ItemName,ItemVal,kCFPreferencesCurrentApplication);
      end
      else begin
         ItemName := CFStr('Check1');
         ItemVal := CFStringCreateWithPascalString(kCFAllocatorDefault,'0',kCFStringEncodingUTF8);
         CFPreferencesSetAppValue(ItemName,ItemVal,kCFPreferencesCurrentApplication);
      end;
      if (CheckBox2.Checked) then
      begin
         ItemName := CFStr('Check2');
         ItemVal := CFStringCreateWithPascalString(nil,'1',kCFStringEncodingUTF8);
         CFPreferencesSetAppValue(ItemName,ItemVal,kCFPreferencesCurrentApplication);
      end
      else begin
         ItemName := CFStr('Check2');
         ItemVal := CFStringCreateWithPascalString(kCFAllocatorDefault,'0',kCFStringEncodingUTF8);
         CFPreferencesSetAppValue(ItemName,ItemVal,kCFPreferencesCurrentApplication);
      end;
      if (CheckBox3.Checked) then
      begin
         ItemName := CFStr('Check3');
         ItemVal := CFStringCreateWithPascalString(kCFAllocatorDefault,'1',kCFStringEncodingUTF8);
         CFPreferencesSetAppValue(ItemName,ItemVal,kCFPreferencesCurrentApplication);
      end
      else begin
         ItemName := CFStr('Check3');
         ItemVal := CFStringCreateWithPascalString(kCFAllocatorDefault,'0',kCFStringEncodingUTF8);
         CFPreferencesSetAppValue(ItemName,ItemVal,kCFPreferencesCurrentApplication);
      end;
      if (CheckBox4.Checked) then
      begin
         ItemName := CFStr('Check4');
         ItemVal := CFStringCreateWithPascalString(kCFAllocatorDefault,'1',kCFStringEncodingUTF8);
         CFPreferencesSetAppValue(ItemName,ItemVal,kCFPreferencesCurrentApplication);
      end
      else begin
         ItemName := CFStr('Check4');
         ItemVal := CFStringCreateWithPascalString(kCFAllocatorDefault,'0',kCFStringEncodingUTF8);
         CFPreferencesSetAppValue(ItemName,ItemVal,kCFPreferencesCurrentApplication);
      end;
      CFPreferencesAppSynchronize(kCFPreferencesCurrentApplication);
    except
    on E : Exception do
        ShowMessage(E.ClassName+' error raised, with message : '+E.Message);
    end;