Skip to main content
Inspiring
June 27, 2023
Answered

How to save the current preferences in a dialog box so that its pre-filled.

  • June 27, 2023
  • 1 reply
  • 377 views

Hi Everyone,

I tried multiple ways to save the folder structure in the dialog box that was used previously while rerunning the script. But it doesn't work. Could anyone help me with this, please? I have pasted the UI below:

 

/**/

// remember the dialog modes
var saveDialogMode = app.displayDialogs;
app.displayDialogs = DialogModes.NO;


// Save the current preferences
var startRulerUnits = app.preferences.rulerUnits;
var startTypeUnits = app.preferences.typeUnits;
var startDisplayDialogs = app.displayDialogs;

// Set Photoshop to use pixels and display no dialogs
app.displayDialogs = DialogModes.NO;
app.preferences.rulerUnits = Units.PIXELS;
app.preferences.typeUnits = TypeUnits.PIXELS;

// DIALOG
// ======
var dialog = new Window("dialog");
dialog.text = "Auto Image Placement";
dialog.orientation = "column";
dialog.alignChildren = ["center", "top"];
dialog.spacing = 10;
dialog.margins = 16;

// TPANEL1
// =======
var tpanel1 = dialog.add("tabbedpanel", undefined, undefined, { name: "tpanel1" });
tpanel1.alignChildren = "fill";
tpanel1.preferredSize.width = 684;
tpanel1.margins = 0;

// TAB1
// ====
var tab1 = tpanel1.add("tab", undefined, undefined, { name: "tab1" });
tab1.text = "Single Story";
tab1.orientation = "column";
tab1.alignChildren = ["left", "top"];
tab1.spacing = 10;
tab1.margins = 10;

// MOCKUPGROUP
// ===========
var mockupGroup = tab1.add("panel", undefined, undefined, { name: "mockupGroup" });
mockupGroup.text = "Template File";
mockupGroup.preferredSize.width = 565;
mockupGroup.preferredSize.height = 64;
mockupGroup.orientation = "column";
mockupGroup.alignChildren = ["left", "top"];
mockupGroup.spacing = 10;
mockupGroup.margins = 10;

// GROUP1
// ======
var group1 = mockupGroup.add("group", undefined, { name: "group1" });
group1.orientation = "row";
group1.alignChildren = ["left", "center"];
group1.spacing = 10;
group1.margins = 0;


var mockupText = group1.add('edittext {properties: {name: "mockupText"}}');
mockupText.helpTip = "Select the template";
mockupText.preferredSize.width = 511;

var mockupButton = group1.add("button", undefined, undefined, { name: "mockupButton" });
mockupButton.text = "Select";
mockupButton.onClick = function () {
    var filter = "*.psd,*.tif,*.tiff";
    mockupFile = File.openDialog("Select Mockup File", filter);
    if (mockupFile) {
        mockupText.text = mockupFile.fsName;
    }
};


// INPUTGROUP
// ==========
var inputGroup = mockupGroup.add("panel", undefined, undefined, { name: "inputGroup" });
inputGroup.text = "Input Folder";
inputGroup.preferredSize.width = 565;
inputGroup.preferredSize.height = 64;
inputGroup.orientation = "column";
inputGroup.alignChildren = ["left", "top"];
inputGroup.spacing = 10;
inputGroup.margins = 10;

// GROUP2
// ======
var group2 = inputGroup.add("group", undefined, { name: "group2" });
group2.orientation = "row";
group2.alignChildren = ["left", "center"];
group2.spacing = 10;
group2.margins = 0;

var inputInput = group2.add('edittext {properties: {name: "inputInput"}}');
inputInput.helpTip = "Select the input image folder ";
inputInput.preferredSize.width = 540;

var inputButton = group2.add("button", undefined, undefined, { name: "inputButton" });
inputButton.text = "Select";

inputButton.onClick = function () {
    var inputFolder = Folder.selectDialog("Select Input Images Folder");
    if (inputFolder) {
        inputInput.text = inputFolder.fsName;
    }
};


// OUTPUTGROUP
// ===========
var outputGroup = mockupGroup.add("panel", undefined, undefined, { name: "outputGroup" });
outputGroup.text = "Output Folder";
outputGroup.preferredSize.width = 565;
outputGroup.preferredSize.height = 64;
outputGroup.orientation = "column";
outputGroup.alignChildren = ["left", "top"];
outputGroup.spacing = 10;
outputGroup.margins = 10;

// GROUP3
// ======
var group3 = outputGroup.add("group", undefined, { name: "group3" });
group3.orientation = "row";
group3.alignChildren = ["left", "center"];
group3.spacing = 10;
group3.margins = 0;

var outputInput = group3.add('edittext {properties: {name: "outputInput"}}');
outputInput.helpTip = "Select an output folder";
outputInput.preferredSize.width = 540;

var outputButton = group3.add("button", undefined, undefined, { name: "outputButton" });
outputButton.text = "Select";
outputButton.onClick = function () {
    var outputFolder = Folder.selectDialog("Select Output Folder");
    if (outputFolder) {
        outputInput.text = outputFolder.fsName;
    }
};

// GROUP4
// ======
var group4 = tab1.add("group", undefined, { name: "group4" });
group4.preferredSize.width = 660;
group4.orientation = "row";
group4.alignChildren = ["center", "center"];
group4.spacing = 14;
group4.margins = 0;

var okButton = group4.add("button", undefined, undefined, { name: "okButton" });
okButton.text = "Create";

var cancelButton = group4.add("button", undefined, undefined, { name: "cancelButton" });
cancelButton.text = "Cancel";
cancelButton.onClick = function () {
    dialog.close();
};


okButton.onClick = function () {



    var outputPath1 = outputInput.text;
    var mockupPath1 = mockupText.text;
    var inputPath1 = inputInput.text;


    dialog.close();
};


dialog.show();


Thanks in advance.

 

This topic has been closed for replies.
Correct answer c.pfaffenbichler

I would create a txt-file next to the Script and read it (if it already exists) to populate the dialog and write it anew when the dialog is OK-ed. 

It may be crude but it might also suffice. 

 

////// read prefs file //////
function readPref (thePath) {
    if (File(thePath).exists == true) {
    var file = File(thePath);
    file.open("r");
    file.encoding= 'BINARY';
    var theText = file.read();
    file.close();
    return String(theText).split(",")
    }
    };
////// function to write a preference-file storing a text //////
function writePref (theArray, thePath) {
    try {
    var thePrefFile = new File(thePath);
    thePrefFile.open("w");
    thePrefFile.write(theArray.join(","));
    thePrefFile.close()
    }
    catch (e) {};
    };

 

 

Edit: 

/**/
var theFile = String($.fileName);
var thePath = theFile.slice(0, theFile.lastIndexOf(".")) + "_pref.txt";
if (File(thePath).exists == true) {var theArray = readPref (thePath)}
else {var theArray = ["aaa"]};

// remember the dialog modes
var saveDialogMode = app.displayDialogs;
app.displayDialogs = DialogModes.NO;


// Save the current preferences
var startRulerUnits = app.preferences.rulerUnits;
var startTypeUnits = app.preferences.typeUnits;
var startDisplayDialogs = app.displayDialogs;

// Set Photoshop to use pixels and display no dialogs
app.displayDialogs = DialogModes.NO;
app.preferences.rulerUnits = Units.PIXELS;
app.preferences.typeUnits = TypeUnits.PIXELS;

// DIALOG
// ======
var dialog = new Window("dialog");
dialog.text = "Auto Image Placement";
dialog.orientation = "column";
dialog.alignChildren = ["center", "top"];
dialog.spacing = 10;
dialog.margins = 16;

// TPANEL1
// =======
var tpanel1 = dialog.add("tabbedpanel", undefined, undefined, { name: "tpanel1" });
tpanel1.alignChildren = "fill";
tpanel1.preferredSize.width = 684;
tpanel1.margins = 0;

// TAB1
// ====
var tab1 = tpanel1.add("tab", undefined, undefined, { name: "tab1" });
tab1.text = "Single Story";
tab1.orientation = "column";
tab1.alignChildren = ["left", "top"];
tab1.spacing = 10;
tab1.margins = 10;

// MOCKUPGROUP
// ===========
var mockupGroup = tab1.add("panel", undefined, undefined, { name: "mockupGroup" });
mockupGroup.text = "Template File";
mockupGroup.preferredSize.width = 565;
mockupGroup.preferredSize.height = 64;
mockupGroup.orientation = "column";
mockupGroup.alignChildren = ["left", "top"];
mockupGroup.spacing = 10;
mockupGroup.margins = 10;

// GROUP1
// ======
var group1 = mockupGroup.add("group", undefined, { name: "group1" });
group1.orientation = "row";
group1.alignChildren = ["left", "center"];
group1.spacing = 10;
group1.margins = 0;


var mockupText = group1.add('edittext {properties: {name: "mockupText"}}');
mockupText.helpTip = "Select the template";
mockupText.preferredSize.width = 511;

var mockupButton = group1.add("button", undefined, undefined, { name: "mockupButton" });
mockupButton.text = "Select";
mockupButton.onClick = function () {
    var filter = "*.psd,*.tif,*.tiff";
    mockupFile = File.openDialog("Select Mockup File", filter);
    if (mockupFile) {
        mockupText.text = mockupFile.fsName;
    }
};


// INPUTGROUP
// ==========
var inputGroup = mockupGroup.add("panel", undefined, undefined, { name: "inputGroup" });
inputGroup.text = "";
inputGroup.preferredSize.width = 565;
inputGroup.preferredSize.height = 64;
inputGroup.orientation = "column";
inputGroup.alignChildren = ["left", "top"];
inputGroup.spacing = 10;
inputGroup.margins = 10;

// GROUP2
// ======
var group2 = inputGroup.add("group", undefined, { name: "group2" });
group2.orientation = "row";
group2.alignChildren = ["left", "center"];
group2.spacing = 10;
group2.margins = 0;

var inputInput = group2.add('edittext {properties: {name: "inputInput"}}');
inputInput.helpTip = "Select the input image folder ";
inputInput.preferredSize.width = 540;

var inputButton = group2.add("button", undefined, undefined, { name: "inputButton" });
inputButton.text = "Select";

inputButton.onClick = function () {
    var inputFolder = Folder.selectDialog("Select Input Images Folder");
    if (inputFolder) {
        inputInput.text = inputFolder.fsName;
    }
};


// OUTPUTGROUP
// ===========
var outputGroup = mockupGroup.add("panel", undefined, undefined, { name: "outputGroup" });
outputGroup.text = "Output Folder";
outputGroup.preferredSize.width = 565;
outputGroup.preferredSize.height = 64;
outputGroup.orientation = "column";
outputGroup.alignChildren = ["left", "top"];
outputGroup.spacing = 10;
outputGroup.margins = 10;

// GROUP3
// ======
var group3 = outputGroup.add("group", undefined, { name: "group3" });
group3.orientation = "row";
group3.alignChildren = ["left", "center"];
group3.spacing = 10;
group3.margins = 0;

var outputInput = group3.add('edittext {properties: {name: "outputInput"}}');
outputInput.text = theArray[0];
outputInput.helpTip = "Select an output folder";
outputInput.preferredSize.width = 540;

var outputButton = group3.add("button", undefined, undefined, { name: "outputButton" });
outputButton.text = "Select";
outputButton.onClick = function () {
    var outputFolder = Folder.selectDialog("Select Output Folder");
    if (outputFolder) {
        outputInput.text = outputFolder.fsName;
    }
};

// GROUP4
// ======
var group4 = tab1.add("group", undefined, { name: "group4" });
group4.preferredSize.width = 660;
group4.orientation = "row";
group4.alignChildren = ["center", "center"];
group4.spacing = 14;
group4.margins = 0;

var okButton = group4.add("button", undefined, undefined, { name: "okButton" });
okButton.text = "Create";

var cancelButton = group4.add("button", undefined, undefined, { name: "cancelButton" });
cancelButton.text = "Cancel";
cancelButton.onClick = function () {
    dialog.close();
};


okButton.onClick = function () {



    var outputPath1 = outputInput.text;
    var mockupPath1 = mockupText.text;
    var inputPath1 = inputInput.text;
    writePref ([outputPath1, mockupPath1, inputPath1], thePath);

    dialog.close();
};


dialog.show();

////// read prefs file //////
function readPref (thePath) {
    if (File(thePath).exists == true) {
    var file = File(thePath);
    file.open("r");
    file.encoding= 'BINARY';
    var theText = file.read();
    file.close();
    return String(theText).split(",")
    }
    };
////// function to write a preference-file storing a text //////
function writePref (theArray, thePath) {
    try {
    var thePrefFile = new File(thePath);
    thePrefFile.open("w");
    thePrefFile.write(theArray.join(","));
    thePrefFile.close()
    }
    catch (e) {};
    };

1 reply

Vibi DevAuthor
Inspiring
June 29, 2023

Can someone please help me with this?

c.pfaffenbichler
Community Expert
c.pfaffenbichlerCommunity ExpertCorrect answer
Community Expert
June 29, 2023

I would create a txt-file next to the Script and read it (if it already exists) to populate the dialog and write it anew when the dialog is OK-ed. 

It may be crude but it might also suffice. 

 

////// read prefs file //////
function readPref (thePath) {
    if (File(thePath).exists == true) {
    var file = File(thePath);
    file.open("r");
    file.encoding= 'BINARY';
    var theText = file.read();
    file.close();
    return String(theText).split(",")
    }
    };
////// function to write a preference-file storing a text //////
function writePref (theArray, thePath) {
    try {
    var thePrefFile = new File(thePath);
    thePrefFile.open("w");
    thePrefFile.write(theArray.join(","));
    thePrefFile.close()
    }
    catch (e) {};
    };

 

 

Edit: 

/**/
var theFile = String($.fileName);
var thePath = theFile.slice(0, theFile.lastIndexOf(".")) + "_pref.txt";
if (File(thePath).exists == true) {var theArray = readPref (thePath)}
else {var theArray = ["aaa"]};

// remember the dialog modes
var saveDialogMode = app.displayDialogs;
app.displayDialogs = DialogModes.NO;


// Save the current preferences
var startRulerUnits = app.preferences.rulerUnits;
var startTypeUnits = app.preferences.typeUnits;
var startDisplayDialogs = app.displayDialogs;

// Set Photoshop to use pixels and display no dialogs
app.displayDialogs = DialogModes.NO;
app.preferences.rulerUnits = Units.PIXELS;
app.preferences.typeUnits = TypeUnits.PIXELS;

// DIALOG
// ======
var dialog = new Window("dialog");
dialog.text = "Auto Image Placement";
dialog.orientation = "column";
dialog.alignChildren = ["center", "top"];
dialog.spacing = 10;
dialog.margins = 16;

// TPANEL1
// =======
var tpanel1 = dialog.add("tabbedpanel", undefined, undefined, { name: "tpanel1" });
tpanel1.alignChildren = "fill";
tpanel1.preferredSize.width = 684;
tpanel1.margins = 0;

// TAB1
// ====
var tab1 = tpanel1.add("tab", undefined, undefined, { name: "tab1" });
tab1.text = "Single Story";
tab1.orientation = "column";
tab1.alignChildren = ["left", "top"];
tab1.spacing = 10;
tab1.margins = 10;

// MOCKUPGROUP
// ===========
var mockupGroup = tab1.add("panel", undefined, undefined, { name: "mockupGroup" });
mockupGroup.text = "Template File";
mockupGroup.preferredSize.width = 565;
mockupGroup.preferredSize.height = 64;
mockupGroup.orientation = "column";
mockupGroup.alignChildren = ["left", "top"];
mockupGroup.spacing = 10;
mockupGroup.margins = 10;

// GROUP1
// ======
var group1 = mockupGroup.add("group", undefined, { name: "group1" });
group1.orientation = "row";
group1.alignChildren = ["left", "center"];
group1.spacing = 10;
group1.margins = 0;


var mockupText = group1.add('edittext {properties: {name: "mockupText"}}');
mockupText.helpTip = "Select the template";
mockupText.preferredSize.width = 511;

var mockupButton = group1.add("button", undefined, undefined, { name: "mockupButton" });
mockupButton.text = "Select";
mockupButton.onClick = function () {
    var filter = "*.psd,*.tif,*.tiff";
    mockupFile = File.openDialog("Select Mockup File", filter);
    if (mockupFile) {
        mockupText.text = mockupFile.fsName;
    }
};


// INPUTGROUP
// ==========
var inputGroup = mockupGroup.add("panel", undefined, undefined, { name: "inputGroup" });
inputGroup.text = "";
inputGroup.preferredSize.width = 565;
inputGroup.preferredSize.height = 64;
inputGroup.orientation = "column";
inputGroup.alignChildren = ["left", "top"];
inputGroup.spacing = 10;
inputGroup.margins = 10;

// GROUP2
// ======
var group2 = inputGroup.add("group", undefined, { name: "group2" });
group2.orientation = "row";
group2.alignChildren = ["left", "center"];
group2.spacing = 10;
group2.margins = 0;

var inputInput = group2.add('edittext {properties: {name: "inputInput"}}');
inputInput.helpTip = "Select the input image folder ";
inputInput.preferredSize.width = 540;

var inputButton = group2.add("button", undefined, undefined, { name: "inputButton" });
inputButton.text = "Select";

inputButton.onClick = function () {
    var inputFolder = Folder.selectDialog("Select Input Images Folder");
    if (inputFolder) {
        inputInput.text = inputFolder.fsName;
    }
};


// OUTPUTGROUP
// ===========
var outputGroup = mockupGroup.add("panel", undefined, undefined, { name: "outputGroup" });
outputGroup.text = "Output Folder";
outputGroup.preferredSize.width = 565;
outputGroup.preferredSize.height = 64;
outputGroup.orientation = "column";
outputGroup.alignChildren = ["left", "top"];
outputGroup.spacing = 10;
outputGroup.margins = 10;

// GROUP3
// ======
var group3 = outputGroup.add("group", undefined, { name: "group3" });
group3.orientation = "row";
group3.alignChildren = ["left", "center"];
group3.spacing = 10;
group3.margins = 0;

var outputInput = group3.add('edittext {properties: {name: "outputInput"}}');
outputInput.text = theArray[0];
outputInput.helpTip = "Select an output folder";
outputInput.preferredSize.width = 540;

var outputButton = group3.add("button", undefined, undefined, { name: "outputButton" });
outputButton.text = "Select";
outputButton.onClick = function () {
    var outputFolder = Folder.selectDialog("Select Output Folder");
    if (outputFolder) {
        outputInput.text = outputFolder.fsName;
    }
};

// GROUP4
// ======
var group4 = tab1.add("group", undefined, { name: "group4" });
group4.preferredSize.width = 660;
group4.orientation = "row";
group4.alignChildren = ["center", "center"];
group4.spacing = 14;
group4.margins = 0;

var okButton = group4.add("button", undefined, undefined, { name: "okButton" });
okButton.text = "Create";

var cancelButton = group4.add("button", undefined, undefined, { name: "cancelButton" });
cancelButton.text = "Cancel";
cancelButton.onClick = function () {
    dialog.close();
};


okButton.onClick = function () {



    var outputPath1 = outputInput.text;
    var mockupPath1 = mockupText.text;
    var inputPath1 = inputInput.text;
    writePref ([outputPath1, mockupPath1, inputPath1], thePath);

    dialog.close();
};


dialog.show();

////// read prefs file //////
function readPref (thePath) {
    if (File(thePath).exists == true) {
    var file = File(thePath);
    file.open("r");
    file.encoding= 'BINARY';
    var theText = file.read();
    file.close();
    return String(theText).split(",")
    }
    };
////// function to write a preference-file storing a text //////
function writePref (theArray, thePath) {
    try {
    var thePrefFile = new File(thePath);
    thePrefFile.open("w");
    thePrefFile.write(theArray.join(","));
    thePrefFile.close()
    }
    catch (e) {};
    };
Vibi DevAuthor
Inspiring
June 29, 2023

This is working perfectly. Thank you so much for your time and effort on this.