Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
0

Requesting Expert Assistance to Resolve Photoshop Scripting Issue: Action Command Execution Problem

New Here ,
Jul 11, 2023 Jul 11, 2023

Copy link to clipboard

Copied

I am reaching out to you today in the hope that you can assist me with a small issue I am facing while working on a Photoshop script. I must mention that I am not a programmer by profession, but I have put in sincere efforts to create this script by referring to blogs and educational videos from knowledgeable professionals like yourself.

The script I have developed is working perfectly fine, except for one problem that I am unable to resolve. Specifically, I am encountering an issue with the execution of the action command on line 236. Allow me to provide you with an overview of the script's functionality.

The objective of the script is to automate the creation of multiple mockups using a master file and a source folder. The master file and source folder names remain constant, while the subfolder names within the source folder vary. To handle this variability, I have utilized a CSV file that contains the subfolder names and the corresponding filenames of the source files in an Excel format.

MfileL1L2L3SfolderSFile1SFile2Sfile3Save As
M1.psdBedSheetPillow 1Pillow 2S1Bedsheet 1.psdPillow A.psdPillow BTest 1
M1.psdSource 1aPillow 1Pillow 2S2Source 1a.psdSource 2a.psdSource 3aTest 2

Here is a brief outline of the script's workflow:

  • 1. The script prompts an opening dialogue box to gather all necessary inputs.
  • 2. It opens the master file.
  • 3. It then proceeds to open the first source file described by the CSV file.
  • 4. The script executes a source action (predefined in Photoshop) as specified in the opening dialogue box. This action closes the source file itself.
  • 5. Subsequently, it performs a master action (also predefined in Photoshop).
  • 6. The script repeats steps 3 to 5 for the remaining two files in the source folder.
  • 7. Finally, it saves the master file with the name of the corresponding subfolder.

The script continues to loop through steps 2 to 7 until all subfolders are processed, resulting in a saved file for each subfolder. To provide a practical example, imagine I have a t-shirt mockup with three pictures on it (master file), and I have 100 sets, with each set comprising three pictures. Therefore, I need to create 100 mockups for each set.

I have invested considerable effort in developing this script, and I am confident that it has the potential to significantly streamline our workflow. However, I am currently stuck on the issue mentioned earlier, which prevents the script from properly executing the action command on line 236.

Considering your expertise in Photoshop scripting, I kindly request your guidance and assistance in resolving this problem. Any insights, suggestions, or corrections you can provide would be immensely valuable to me. I genuinely appreciate your time and consideration in helping me overcome this hurdle.

Thank you for your attention, and I eagerly await your response.

Below is the script:

(function () {

 

    // Script variables

    var abort;

    var title = "Adobe Script Tutorial 5";

  

    // Reusable UI variables

    var g; // group

    var p; // panel

    var w; // window

  

    // Permanent UI variables

    var btnCancel;

    var btnOk;

    var csvFile;

    var txtcsvFile;

    var btnMasterFolderInput;

    var txtMasterFolderInput;

    var btnSourceFolderInput;

    var txtSourceFolderInput;

    var btnSaveAsInput;

    var txtSaveAsInput;

    var sourceActionSet;

    var txtSourceActionSet;

    var masterActionSet;

    var txtMasterActionSet;

    var masterAction;

    var txtMasterAction;

    var sourceAction;

    var txtSourceAction;

  

    // SETUP

  

    // CREATE USER INTERFACE

  

    w = new Window("dialog", title);

    w.alignChildren = "fill";

  

    //csv file name and location input

    p = w.add("panel", undefined,"");

    g = p.add("group");

    btncsvFile = g.add("button", undefined, "csv FILE");

    txtcsvFile = g.add("edittext", undefined, "", {

        truncate: "middle"

    });

    txtcsvFile.preferredSize = [400, -1];

    

    //Master file name and location input

    p = w.add("panel", undefined, "");

    g = p.add("group");

    btnMasterFolderInput = g.add("button", undefined, "Master Folder");

    txtMasterFolderInput = g.add("edittext", undefined, "", {

        truncate: "middle"

    });

    txtMasterFolderInput.preferredSize = [400, -1];

   

    //Source file name and location input

    //p = w.add("panel", undefined,"");

    g = p.add("group");

    btnSourceFolderInput = g.add("button", undefined, "Source Folder");

    txtSourceFolderInput = g.add("edittext", undefined, "", {

        truncate: "middle"

    });

    txtSourceFolderInput.preferredSize = [400, -1];

  

    //Save as file name and location input

    p = w.add("panel", undefined,"");

    g = p.add("group");

    btnSaveAsInput = g.add("button", undefined, "Save Folder");

    txtSaveAsInput = g.add("edittext", undefined, "", {

        truncate: "middle"

    });

    txtSaveAsInput.preferredSize = [400, -1];

    

    //Source Action Set and Action input

    p = w.add("panel", undefined,"Source Action Inputs");

    g = p.add("group");

    sourceActionSet = g.add("statictext", undefined, "Source Action Set:");

    txtSourceActionSet = g.add("edittext", undefined, "");

    txtSourceActionSet.preferredSize = [400, -1];

  

    g = p.add("group");

    sourceAction = g.add("statictext", undefined, "Source Action:");

    txtSourceAction = g.add("edittext", undefined, "");

    txtSourceAction.preferredSize = [400, -1];

    

    //Master Action Set and Action input

    p = w.add("panel", undefined,"Master Action Inputs");

    g = p.add("group");

    masterActionSet = g.add("statictext", undefined, "Master Action Set:");

    txtMasterActionSet = g.add("edittext", undefined, "");

    txtMasterActionSet.preferredSize = [400, -1];

  

    g = p.add("group");

    masterAction = g.add("statictext", undefined, "Master Action:");

    txtMasterAction = g.add("edittext", undefined, "");

    txtMasterAction.preferredSize = [400, -1];

  

    g = w.add("group");

    g.alignment = "center";

    btnOk = g.add("button", undefined, "OK");

    btnCancel = g.add("button", undefined, "Cancel");

  

    // UI EVENT HANDLERS

  

    btncsvFile.onClick = function () {

        var f = File.openDialog();

        if (f) {

            txtcsvFile.text = f.fullName;

        }

    };

    btnMasterFolderInput.onClick = function () {

        var f = Folder.selectDialog();

        if (f) {

            txtMasterFolderInput.text = f.fullName;

        }

    };

  

    btnSourceFolderInput.onClick = function () {

        var f = Folder.selectDialog();

        if (f) {

            txtSourceFolderInput.text = f.fullName;

        }

    };

  

    btnSaveAsInput.onClick = function () {

        var f = Folder.selectDialog();

        if (f) {

            txtSaveAsInput.text = f.fullName;

        }

    };

    

    /*btnOk.onClick = function () {

        if (!txtcsvFile.text) {

            alert("Select CSV file", " ", false);

            return;

        }

        if (!txtMasterFolderInput.text) {

            alert("Select Master folder", " ", false);

            return;

        }

        if (!txtSourceFolderInput.text) {

           alert("Select Source folder", " ", false);

          return;

      }

        if (!txtSaveAsInput.text) {

           alert("Select Save as folder", " ", false);

          return;

      }

       

        w.close(1);

    };*/

  

    btnCancel.onClick = function () {

        w.close(0);

    };

  

    // SHOW THE WINDOW

  

    if (w.show() == 1) {

        try {

            process();

            alert(abort || "Done", title, false);

        } catch (e) {

            alert("An error has occurred.\nLine " + e.line + ": " + e.message, title, true);

        }

    }

  

    function process() {

        var masterFiles = new Folder(txtMasterFolderInput.text).getFiles(/\.(jpg|psd|psb|tif|gif|eps)$/i);

    

        if (!masterFiles.length) {

            abort = "No files found in the selected folder";

            return;

        }

    

        var totalFiles = masterFiles.length;

        var currentFile = 0;

    

        // Create progress dialog

        var progressDialog = new Window("palette", "Progress");

        var progressText = progressDialog.add("statictext", undefined, "Processing file 1 of " + totalFiles);

        var progressBar = progressDialog.add("progressbar", undefined, 0, totalFiles);

        progressDialog.show();

    

        // Loop through master files

        for (var i = 0; i < totalFiles; i++) {

            currentFile++;

            progressText.text = "Processing file " + currentFile + " of " + totalFiles;

            progressBar.value = currentFile;

    

            var file = masterFiles[i];

            processFile(file);

    

            if (abort) {

                break;

            }

        }

    

        progressDialog.close();

    

        if (!abort) {

            alert("Done", title, false);

        }

    }

    

    function processFile(file) {

        var csvFile = File(txtcsvFile.text);

        var masterDoc;

        var sourceDoc;

        var saveFolder = new Folder(txtSaveAsInput.text);

    

        // Creating a log file

        var logFile = new File(saveFolder + '/log.txt');

        if (logFile.exists) {

            logFile.remove();

        }

        logFile.open('w');

        log('Log file created.', logFile);

    

        masterDoc = app.open(file);

    

        try {

            csvFile = File(csvFile);

            var lines = readInCSV(csvFile);

    

            for (var i = 1; i < lines.length; i++) {

                var subfolder = lines[i][4];

                var fileName = lines[i][5];

                var filePath = decodeURI(txtSourceFolderInput.text + '/' + subfolder + '/' + fileName);

    

                try {

                    sourceDoc = app.open(File(filePath));

                    app.activeDocument = sourceDoc;

 

                    app.doAction(sourceAction, sourceActionSet);    

                    alert ("performing action on source file 1")

                    // Perform desired actions on the opened source document

                    // ...

 

                    app.activeDocument = masterDoc;

 

                    alert ("performing action on master file")

                    app.doAction(sourceAction, masterActionSet);

    

                    log('Processed file: ' + filePath, logFile);

                } catch (e) {

                    log('Error opening file: ' + filePath, logFile);

                }

    

                /*if (lines[i][6] && lines[i][7]) {

                    var additionalFilePath1 = decodeURI(txtSourceFolderInput.text + '/' + subfolder + '/' + lines[i][6]);

                    var additionalFilePath2 = decodeURI(txtSourceFolderInput.text + '/' + subfolder + '/' + lines[i][7]);

    

                    try {

                        sourceDoc = app.open(File(additionalFilePath1));

 

    

                        // Perform desired actions on the opened additional source document

                        // ...

    

                        log('Processed file: ' + additionalFilePath1, logFile);

                    } catch (e) {

                        log('Error opening file: ' + additionalFilePath1, logFile);

                    }

    

                    try {

                        sourceDoc = app.open(File(additionalFilePath2));

    

                        // Perform desired actions on the opened additional source document

                        // ...

    

                        log('Processed file: ' + additionalFilePath2, logFile);

                    } catch (e) {

                        log('Error opening file: ' + additionalFilePath2, logFile);

                    }

                }*/

            }

        } finally {

            // Close the master document without saving changes

           // masterDoc.close(SaveOptions.DONOTSAVECHANGES);

        }

    }

    

    // Rest of the code (readInCSV, log) remains the same

    

    

    

 

    function progress(message) {

        var b;

        var t;

        var w;

        w = new Window("palette", "Progress", undefined, {

            closeButton: false

        });

        t = w.add("statictext", undefined, message);

        t.preferredSize = [450, -1];

        b = w.add("progressbar");

        b.preferredSize = [450, -1];

        progress.close = function () {

            w.close();

        };

        progress.increment = function () {

            b.value++;

        };

        progress.message = function (message) {

            t.text = message;

            app.refresh();

        };

        progress.set = function (steps) {

            b.value = 0;

            b.minvalue = 0;

            b.maxvalue = steps;

        };

        w.show();

        app.refresh();

    }

    ///// CSV READ //////

function readInCSV(fileObj) {

    var fileArray = new Array();

    fileObj.open('r');

    fileObj.seek(0, 0);

    while(!fileObj.eof)

    {

         var thisLine = fileObj.readln();

         var csvArray = thisLine.split(',');

         fileArray.push(csvArray);

    }

    fileObj.close();

    return fileArray;

}

 

///// LOG FUNCTION /////

function log(message, file){

   var time = new Date();

   file.write(('0' + time.getHours()).slice(-2) + ':' + ('0' + time.getMinutes()).slice(-2) + ':' + ('0' + time.getSeconds()).slice(-2));

   file.write('  >>>>> ' + message);

   file.writeln('');

}

 

  

  })();

 

TOPICS
Actions and scripting , macOS

Views

295
Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe
People's Champ ,
Jul 11, 2023 Jul 11, 2023

Copy link to clipboard

Copied

What is the code in this line?

How did you know that the error is happening on this line?

What is this error?

 

Tip: Insert an error indication and the line number with the error inside the "catch(e)" block,

something like

 

alert("Line: " + e.line + "\n\n" + e);

 

 

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jul 11, 2023 Jul 11, 2023

Copy link to clipboard

Copied

This script is not giving any error. It just not playing the action command. 236 is the row number just for the location of the action command. This is the error msg recorded in the log file : '09:32:58 >>>>> Error opening file: ~/Desktop/test/Beds mockup 03_07_23/Source Folder/S1/Bedsheet 1.psd'
I have just one problem with this script that it is not playing action. Since i m not a programmer i have no idea where is the actual problem and how bigger is the problem.

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
People's Champ ,
Jul 12, 2023 Jul 12, 2023

Copy link to clipboard

Copied

LATEST

Post your code normally, press the symbol symbol </> to insert the code or attach a link to the jsx file. I can't figure out where you have line 236.

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines