10 Replies Latest reply on Dec 20, 2010 7:14 AM by gmitchel

    Load and save presets

    DavideBarranca Level 1

      Hello,

      I'm building a PS extension which has a GUI with several sliders, numeric steppers, etc. I've put a ComboBox (kind of a "presets chooser"), which sets all the GUI elements at once. All these values will then be passed to a PS script. My goal would be to implement a "save new preset" system - so I have to find a way to retrieve and store somewhere all the pair key/values (they can be up to 10), giving them a unique name - the one which will appear in the ComboBox in addition to the default presets that are built in.

       

      Instead of an ArrayCollection, I've used an XML (via XMLList, XMLListCollection and HTTPService) to populate the ComboBox and store all my 5 default presets - it's not clear to me how to put there the new ones that users may want to save.

       

      Maybe a system like the one shown in the MakeSideHeads, using CSXS Preferences? At first glance, to me it doesn't look like the right way, but chances are that I'm wrong.

      Any suggestion?

      Thanks in advance,

       

      Davide

        • 1. Re: Load and save presets
          Bob Stucky Adobe Employee

          Davide

           

          I would not use the CSXS Preferences feature for this. XML is a great choice, you could also use, say AMF and just write an object to file.

           

          I use this code to get a File object in the User's application data area:

           

          var prefsFile: File;

          var path: String = "/Roaming/MyExtension/prefs.xml";
          if ( Utilities.isMac() )
          {
               path = "/Library/Application Support/MyExtension/prefs.xml";
          }
          prefsFile = new File( new File( File.userDirectory.nativePath ).url + path );

           

          Once the file is there, reading and writing is straightforward.

          Reading:

          var fs: FileStream = new FileStream();
          fs.open( prefsFile, FileMode.READ );
          var buf: String = fs.readUTFBytes( fsr.bytesAvailable );
          fs.close();
          var prefsXml: XML = new XML( buf );

           

          Writing:

          var fs: FileStream = new FileStream();
          fs.open( prefsFile, FileMode.WRITE );
          fs.WriteUTFBytes( prefsXml.toString() );
          fs.close();

           

          As for "put there the new ones the users might save", I guess that would depend on how you structured your xml.

           

          <presets>

               <preset src="me" name="preset1" >

                    <name1>value1</name1>

                    <name2>value2</name2> <!-- etc -->

               </preset>

               <preset src="me" name="preset2" >

                    <name1>value1</name1>

                    <name2>value2</name2> <!-- etc -->

               </preset>

               <preset src="user" name="user preset 1" >

                    <name1>value1</name1>

                    <name2>value2</name2> <!-- etc -->

               </preset>

          </presets>

          You would then use E4X to access, add, and remove presets.

           

          I hope I understood your question correctly!

           

          Bob

           

           

           

          • 2. Re: Load and save presets
            DavideBarranca Level 1

            Of course you did, Bob, thanks for that!

            I hope I'll understand your answer I'll try as soon as possible.

            Thanks again,

             

            Davide

            • 3. Re: Load and save presets
              DavideBarranca Level 1

              Bob,

              I got "Access of undefined property path" and "Access of undefined property prefsFile" in the line:

               

              prefsFile = new File( new File( File.userDirectory.nativePath ).url + path );

               

              I'm surely doing something wrong: how can path and prefsFile are undefined if they're declared in the previous lines?!

              Thanks,

               

              Davide

               

              EDIT -----

              Strangely enough, if I keep the two lines:

              var prefsFile:File;

              prefsFile = new File( new File( File.userDirectory.nativePath ).url + path );

               

              I got the error, while if I write:

               

              var prefsFile:File = new File( new File( File.userDirectory.nativePath ).url + path );

               

              everything runs fine.

              • 4. Re: Load and save presets
                gmitchel Level 1

                I use XML strings to save/load presets.I use an XMLList object. ON the UI, I use a ComboBox with the XMLList as the data provider.

                 

                My presets look like this:

                 

                PresetList =

                 

                new XMLList(

                 

                <presets>

                <preset label="(None)" settings="300,0.8,0,1,65,1,30,50,100,195,25,50,80,200" canDelete="false" canEdit="false" />

                </presets>

                 

                );

                PresetLabelList = XMLList(PresetList.preset.attribute(

                 

                "label"));

                 

                 

                Here is my code for handling presets:

                 

                 

                /** Handle presets **/

                 

                 

                 

                private function addPreset():void {

                 

                try {

                 

                var newPresetName:String = ComboPreset.text;

                 

                var params:String = makeParamsString();

                 

                var xml:XML = new XML(PresetList);

                 

                 

                if (newPresetName == "") {

                Alert.show(

                "Preset name was not entered.", "Error!", Alert.OK, Sprite(mx.core.Application.application));

                 

                return;

                }

                 

                if (presetNameExists(newPresetName) == true) {

                Alert.show(

                "A preset with that name already exists.", "Error!", Alert.OK, Sprite(mx.core.Application.application));

                 

                return;

                }

                 

                if (presetSettingsExists(params) == true) {

                Alert.show(

                "A preset with the current settings already exists.", "Error!", Alert.OK, Sprite(mx.core.Application.application));

                 

                return;

                }

                 

                 

                var xmlString:String = '<preset label="' + newPresetName + '" settings="' + params + '" canDelete="true" canEdit="false" />';

                 

                var settingsXML:XML = XML(xmlString);

                 

                 

                // Find insertion point

                 

                 

                var insertionPoint:int = -1;

                 

                if (xml.children().length() > DefaultPresetCount) {

                 

                for (var i:int=DefaultPresetCount; i<xml.children().length(); i++) {

                 

                if (newPresetName < PresetList.preset[i].attribute("label")) insertionPoint = i;

                }

                }

                 

                 

                // Insert preset

                 

                 

                if (insertionPoint > -1) xml.insertChildBefore(xml.preset[insertionPoint],settingsXML);

                 

                else xml.appendChild(settingsXML);

                 

                 

                // Update presets lists

                PresetList =

                 

                new XMLList(xml);

                PresetLabelList =

                new XMLList(PresetList.preset.attribute("label"));

                 

                 

                // Save presets list

                flushDefaultSettings();

                 

                 

                 

                // Make preset the active selection for preset combobox

                 

                 

                if (insertionPoint > -1) {

                PresetSetting = insertionPoint;

                ComboPreset.selectedIndex = insertionPoint;

                }

                 

                else {

                PresetSetting = PresetLabelList.length() - 1;

                ComboPreset.selectedIndex = PresetLabelList.length() - 1;

                }

                }

                 

                catch(error:Error) {

                Alert.show(

                "Error in addPreset(): " + error.message, "Error!", Alert.OK, Sprite(mx.core.Application.application));

                }

                }

                 

                 

                private function deletePreset():void{

                 

                try {

                 

                var i:int = ComboPreset.selectedIndex;

                 

                var xml:XML = new XML(PresetList);

                 

                 

                if (xml.preset[i].attribute("canDelete") == "true") {

                 

                delete xml.preset[i];

                 

                PresetList =

                new XMLList(xml);

                PresetLabelList =

                new XMLList(PresetList.preset.attribute("label"));

                 

                flushDefaultSettings();

                }

                 

                else Alert.show("Preset cannot be deleted.", "Error!", Alert.OK, Sprite(mx.core.Application.application));

                }

                 

                catch(error:Error) {

                Alert.show(

                "Error in deletePreset(): " + error.message, "Error!", Alert.OK, Sprite(mx.core.Application.application));

                }

                }

                 

                 

                private function presetSettingsExists(presetSettings:String):Boolean {

                 

                var returnValue:Boolean = false;

                 

                try {

                 

                for each (var xml:XML in PresetList) {

                 

                if (xml.preset.attribute("settings") == presetSettings) returnValue = true;

                }

                returnValue =

                false;

                }

                 

                catch(error:Error) {

                Alert.show(

                "Error in presetSettingsExit(): " + error.message, "Error!", Alert.OK, Sprite(mx.core.Application.application));

                }

                 

                finally {

                 

                return returnValue;

                }

                }

                 

                 

                private function presetNameExists(presetName:String):Boolean {

                 

                var returnValue:Boolean = false;

                 

                try {

                 

                for each (var xml:XML in PresetList) {

                 

                if (xml.preset.attribute("label") == presetName) returnValue = true;

                }

                returnValue =

                false;

                }

                 

                catch(error:Error) {

                Alert.show(

                "Error in presetNameExists(): " + error.message, "Error!", Alert.OK, Sprite(mx.core.Application.application));

                }

                 

                finally {

                 

                return returnValue;

                }

                }

                 

                 

                private function getPresetSettingsString():String {

                 

                return PresetList.preset[PresetSetting].attribute("settings");

                }

                 

                 

                private function getPresetCanDelete():Boolean {

                 

                var canDelete:Boolean = false;

                 

                var xmlAttribute: String;

                 

                xmlAttribute = PresetList.preset[ComboPreset.selectedIndex].attribute(

                "canDelete");

                 

                if (xmlAttribute.toLowerCase()== "true") canDelete = true;

                 

                else canDelete = false;

                 

                 

                return canDelete;

                }

                 

                 

                private function getPresetCanEdit():Boolean {

                 

                var canEdit:Boolean = false;

                 

                var xmlAttribute: String;

                 

                xmlAttribute = PresetList.preset[ComboPreset.selectedIndex].attribute(

                "canEdit");

                 

                if (xmlAttribute.toLowerCase()== "true") canEdit = true;

                 

                else canEdit = false;

                 

                 

                return canEdit;

                }

                 

                1 person found this helpful
                • 5. Re: Load and save presets
                  DavideBarranca Level 1

                  Hi gmitchel,

                  thanks!!

                   

                  I'm currently writing a test app based on your code - for sure I'm learning a lot.

                  A couple of questions if you don't mind:

                   

                  1) could you please give me makeParamsString() and flushDefaultSettings() functions as well?

                  2) I've put on stage a ComboBox like this one:

                  <mx:ComboBox x="378" y="314" id="ComboPreset" dataProvider="{PresetList}" labelField="{PresetLabelList}"></mx:ComboBox>

                  Is that correct in your opinion? I still have problems retrieving the ComboBox label.

                  Thanks again,

                   

                  Davide

                  • 6. Re: Load and save presets
                    gmitchel Level 1

                    Here you go:

                     

                     

                     

                    // Prepares comma-separated paramter string.

                     

                     

                    private function makeParamsString():String {

                     

                    var params:String;

                     

                    var useEnhancedMask:int;

                     

                     

                    // Convert Boolean to String to store in Params string

                     

                     

                    if (UseEnhancedMasksSetting) useEnhancedMask = 1;

                     

                    else useEnhancedMask = 0;

                     

                     

                    try {

                    params = SliderUSMAmount.value.toString() +

                    "," + SliderUSMRadius.value.toString() + "," +

                    SliderUSMThreshold.value.toString() +

                    "," + ComboBlend.selectedIndex.toString() + "," +

                    StepperOpacity.value.toString() +

                    "," + ComboMaskWidth.selectedIndex.toString() + "," +

                    SharpeningBlendIf1Setting.toString() +

                    "," +

                    SharpeningBlendIf2Setting.toString() +

                    "," + SharpeningBlendIf3Setting.toString() + "," +

                    SharpeningBlendIf4Setting.toString() +

                    "," + UnderlyingBlendIf1Setting.toString() + "," +

                    UnderlyingBlendIf2Setting.toString() +

                    "," + UnderlyingBlendIf3Setting.toString() + "," +

                    UnderlyingBlendIf4Setting.toString() +

                    "," + useEnhancedMask.toString();

                    }

                     

                    catch(error:Error) {

                    Alert.show(

                    "Error in getUSMParametersString(): " + error.message, "Error!", Alert.OK, Sprite(mx.core.Application.application));

                    }

                     

                     

                    return params;

                    }

                     

                     

                     

                     

                    private function flushDefaultSettings():void{

                     

                    try {

                     

                     

                    var csxs:CSXSInterface = CSXSInterface.getInstance();

                     

                    csxs.storePreference(

                    "TLRCapturePro.PanelVersion",panelVersion);

                     

                    csxs.storePreference(

                    "TLRCapturePro.PresetDefault",PresetSetting.toString());

                     

                    csxs.storePreference(

                    "TLRCapturePro.USMAmountDefault",USMAmountSetting.toString());

                    csxs.storePreference(

                    "TLRCapturePro.USMRadiusDefault",USMRadiusSetting.toString());

                    csxs.storePreference(

                    "TLRCapturePro.USMThresholdDefault",USMThresholdSetting.toString());

                     

                    csxs.storePreference(

                    "TLRCapturePro.LayerBlendDefault",LayerBlendSetting.toString());

                    csxs.storePreference(

                    "TLRCapturePro.LayerOpacityDefault",LayerOpacitySetting.toString());

                    csxs.storePreference(

                    "TLRCapturePro.MaskWidthDefault",MaskWidthSetting.toString());

                     

                    csxs.storePreference(

                    "TLRCapturePro.SharpeningBlendIf1Default",SharpeningBlendIf1Setting.toString());

                    csxs.storePreference(

                    "TLRCapturePro.SharpeningBlendIf2Default",SharpeningBlendIf2Setting.toString());

                    csxs.storePreference(

                    "TLRCapturePro.SharpeningBlendIf3Default",SharpeningBlendIf3Setting.toString());

                    csxs.storePreference(

                    "TLRCapturePro.SharpeningBlendIf4Default",SharpeningBlendIf4Setting.toString());

                     

                    csxs.storePreference(

                    "TLRCapturePro.UnderlyingBlendIf1Default",UnderlyingBlendIf1Setting.toString());

                    csxs.storePreference(

                    "TLRCapturePro.UnderlyingBlendIf2Default",UnderlyingBlendIf2Setting.toString());

                    csxs.storePreference(

                    "TLRCapturePro.UnderlyingBlendIf3Default",UnderlyingBlendIf3Setting.toString());

                    csxs.storePreference(

                    "TLRCapturePro.UnderlyingBlendIf4Default",UnderlyingBlendIf4Setting.toString());

                     

                     

                    csxs.storePreference(

                    "TLRCapturePro.UseEnhancedMasksDefault",UseEnhancedMasksSetting.toString());

                    csxs.storePreference(

                    "TLRCapturePro.RetainAlphaChannelsDefault",RetainAlphaChannelsSetting.toString());

                     

                    csxs.storePreference(

                    "TLRCapturePro.PresetList",PresetList.toXMLString());

                    }

                     

                    catch(error:Error) {

                    Alert.show(

                    "Error in flushDefaultSettings(): " + error.message, "Error!", Alert.OK, Sprite(mx.core.Application.application));

                    }

                    }

                     

                     

                    • 7. Re: Load and save presets
                      gmitchel Level 1

                      Here is the ComboBox code:

                       

                      <mx:ComboBox x="6" y="10" id="ComboPreset" width="210" selectedIndex="0" fontSize="9" editable="true"

                      close="closeComboPreset()" dataProvider="

                      {PresetLabelList}"/>

                       

                      You've got it all now. It's been bullet-proof so far in the CS4 version of the panel and the CS5 beta tests.

                       

                      Happy holidays!

                       

                      Mitch

                       

                      • 8. Re: Load and save presets
                        gmitchel Level 1

                        Here is the code to fetch the settings from Photoshop's Preferences cache.

                         

                         

                         

                        private function getDefaultSettings():void{

                         

                        try {

                         

                        var csxs:CSXSInterface = CSXSInterface.getInstance();

                         

                        var result:SyncRequestResult;

                         

                        result = csxs.retrievePreference(

                        "TLRCapturePro.PresetDefault");

                        PresetSetting = parseInt(result.data

                        as String);

                         

                        result = csxs.retrievePreference(

                        "TLRCapturePro.USMAmountDefault");

                        USMAmountSetting = parseInt(result.data

                        as String);

                        result = csxs.retrievePreference(

                        "TLRCapturePro.USMRadiusDefault");

                        USMRadiusSetting = parseFloat(result.data

                        as String);

                        result = csxs.retrievePreference(

                        "TLRCapturePro.USMThresholdDefault");

                        USMThresholdSetting = parseInt(result.data

                        as String);

                         

                        result = csxs.retrievePreference(

                        "TLRCapturePro.LayerBlendDefault");

                        LayerBlendSetting = parseInt(result.data

                        as String);

                        result = csxs.retrievePreference(

                        "TLRCapturePro.LayerOpacityDefault");

                        LayerOpacitySetting = parseInt(result.data

                        as String);

                        result = csxs.retrievePreference(

                        "TLRCapturePro.MaskWidthDefault");

                        MaskWidthSetting = parseInt(result.data

                        as String);

                         

                        result = csxs.retrievePreference(

                        "TLRCapturePro.SharpeningBlendIf1Default");

                        SharpeningBlendIf1Setting = parseInt(result.data

                        as String);

                        result = csxs.retrievePreference(

                        "TLRCapturePro.SharpeningBlendIf2Default");

                        SharpeningBlendIf2Setting = parseInt(result.data

                        as String);

                        result = csxs.retrievePreference(

                        "TLRCapturePro.SharpeningBlendIf3Default");

                        SharpeningBlendIf3Setting = parseInt(result.data

                        as String);

                        result = csxs.retrievePreference(

                        "TLRCapturePro.SharpeningBlendIf4Default");

                        SharpeningBlendIf4Setting = parseInt(result.data

                        as String);

                         

                        result = csxs.retrievePreference(

                        "TLRCapturePro.UnderlyingBlendIf1Default");

                        UnderlyingBlendIf1Setting = parseInt(result.data

                        as String);

                        result = csxs.retrievePreference(

                        "TLRCapturePro.UnderlyingBlendIf2Default");

                        UnderlyingBlendIf2Setting = parseInt(result.data

                        as String);

                        result = csxs.retrievePreference(

                        "TLRCapturePro.UnderlyingBlendIf3Default");

                        UnderlyingBlendIf3Setting = parseInt(result.data

                        as String);

                        result = csxs.retrievePreference(

                        "TLRCapturePro.UnderlyingBlendIf4Default");

                        UnderlyingBlendIf4Setting = parseInt(result.data

                        as String);

                         

                         

                        result = csxs.retrievePreference(

                        "TLRCapturePro.UseEnhancedMasksDefault");

                        UseEnhancedMasksSetting = Boolean(result.data

                        as String);

                        result = csxs.retrievePreference(

                        "TLRCapturePro.RetainAlphaChannelsDefault");

                        RetainAlphaChannelsSetting = Boolean(result.data

                        as String);

                         

                        result = csxs.retrievePreference(

                        "TLRCapturePro.PresetList");

                        PresetList =

                        new XMLList(result.data as String);

                        PresetLabelList =

                        new XMLList(PresetList.preset.attribute("label"));

                        }

                         

                        catch(error:Error) {

                        Alert.show(

                        "Error in getDefaultSettings(): " + error.message, "Error!", Alert.OK, Sprite(mx.core.Application.application));

                        }

                        }

                        • 9. Re: Load and save presets
                          DavideBarranca Level 1

                          Thanks Santa, ehm, Mitch ;-)

                           

                          I was elaborating your Flex forum post: I still can't find a way to access the other preset data.

                           

                          <presets>

                               <preset label="One" radius="100"/>

                               <preset label="Two" radius="200"/>

                          </presets>

                           

                          If I'd like to display the radius in a <mx:Text>, the following isn't working:

                           

                          <mx:Text text="{myComboBox.selectedItem.@radius}">

                          (of course, because the dataprovider is an XMLList made of labels... but I can't figure out how to get there)

                           

                          That's it:

                           

                          text="{LayerBlends.blend.(@label == myComboBox.selectedItem).@radius}"

                           

                          THANKS, thanks, thanks.

                          Long learning curve here...

                           

                          Davide

                          • 10. Re: Load and save presets
                            gmitchel Level 1

                            Glad I could help. That's what these forums are all about. Helping one another.

                             

                            Believe me, that code is the product of a couple of days worth of effort, as I flailed around trying to implement presets. What you were struggling with, isn't all that easy.

                             

                            Happy Holidays!

                             

                            Mitch