Skip to main content
Inspiring
June 22, 2020
Answered

persistent data

  • June 22, 2020
  • 1 reply
  • 1691 views

What I have been working on has variable fields like email and website address. Is there a way of storing those settings so that next time the script runs there is no need to enter them as they are already present?

This topic has been closed for replies.
Correct answer r-bin

what I did find was that my Photoshop prefs file has this string 5d9bdc30-d980-4d66-92b8-3fdb0eb270c5  and not 06704d4d-4e32-4c26-87ab-78d674f1eeab so I tried changing it but no difference


//app.bringToFront();

var settings = new File(app.preferencesFolder + "/{06704d4d-4e32-4c26-87ab-78d674f1eeab}.dat")
var myInstagram; if (myInstagram == undefined) myInstagram = "instagram name here";
try { if (settings.exists) $.evalFile(settings); } catch(e) { alert(e); }

if (!documents.length) {
    alert('There are no documents open.', 'No Document');
}

else {
    main();
}

function main() 
    {
    try {
        //  Ask user for input by showing prompt box and save inputted value to variable:

        var win = new Window('dialog','Text Input');
        win.orientation='column';   
        win.alignChildren = 'left';
    
        var grp3 = win.add ('group');   
        var pnl7 = grp3.add('panel',undefined,'Instagram', {borderStyle: 'black'} );
        pnl7.orientation='row';
        pnl7.alignChildren = 'left';
        var instaCB = pnl7.add('checkbox',[10,10,40,20],'On');
        var instagramDetail = pnl7.add('edittext', [50,10,220,20], myInstagram);
    
        var grp2 = win.add ('group');
        var b1 = win.add("button",  undefined, "OK");
        b1.preferredSize = [70,20];

        b1.onClick = function ()  
            {    
            try {
                myInstagram = instagramDetail.text;   

                save_settings();  

                win.close()    
                }
            catch (e) { alert(e); }        
            } 
    
        var myReturn = win.show();
        }
    catch (e) { alert(e); }        
    }

function save_settings()  
    {  
    try {
        settings.open("w");  

        settings.writeln("var myInstagram = " + myInstagram.toSource())  

        settings.close();  
        }
    catch (e) { alert(e); }        
    }
As they say, find 10 differences.
Always use try-catch.
Without this, Photoshop CC very often simply won’t show your mistakes.

The variable myInstagram and settings should be available both in the main function and in the save_settings function. Therefore, they must be defined outside of these functions and before calling these functions.

Using the intermediate variable myInstagram_value is not advisable.
 

1 reply

Kukurykus
Legend
June 22, 2020

Read about custom options.

Inspiring
June 22, 2020

many thanks, that would appear to be the solution but I can only find 2 examples and I cant implement either in my code so going come back to that later

Inspiring
June 22, 2020

This is where I am lost

written the simplest of code, it runs fine but when I search the windows registry I cant find a related key

 

const kMyInstagram = app.stringIDToTypeID( '@DorsetPhoto' );
const kMySettings = "mySettings";

saveSettings ()

function saveSettings()
{
  var desc = new ActionDescriptor();
  desc.putString(kMyInstagram, true);

  // "true" means setting persists across Photoshop launches.
  app.putCustomOptions( kMySettings, desc, true );
}