Search code examples
javascriptruntime-errorphotoshopphotoshop-scriptjsx

JSX/Photoshop: app.activeDocument.saveAs() returning error


I'm trying to save the activeDocument as a .psd but its returning this error

ERROR: General Photoshop error occurred. This functionality may not be available in this version of Photoshop.

my script:

#target photoshop

var fileRef = new File(app.path.toString() + "/Samples/template.psd");
var docRef = open(fileRef);

//target text layer
var layerRef = app.activeDocument.layers.getByName("Text");

//user input
var newText = prompt("Editing " + layerRef.name, "enter new text: ");

//change contents
layerRef.textItem.contents = newText;

//save
var savePath = "/Samples/" + newText + ".psd";
var saveFile = new File(savePath);
var saveOptions = new PhotoshopSaveOptions();
saveOptions.alphaChannels = false;
saveOptions.annotations = false;
saveOptions.embedColorProfile = true;
saveOptions.layers = true;
saveOptions.spotColors = false;

app.activeDocument.saveAs(saveFile, saveOptions, true, Extension.LOWERCASE);
app.activeDocument.close();

what I want to do is basically, duplicate a template file over and over, only replacing the contents of a text layer then saving it under the string I replace in the text layer.

any tips or help is greatly appreciated.


Solution

  • Resolved

    I fixed my problem, by a work around. I moved both the script and the template file into the Photoshop directory and added app.path.toString() to the saveFile output variable. So it seems that the path needed to be converted to a string before saving.

    As of yet I am unsure how to work outside the Photoshop directory but for me this works so I'm happy. It's a fairly crude but I'm open to suggestion. So if anyone is having a similar issue they can use this for reference.

    #target photoshop
    
    var loop = true;
    var filePath = "/Samples/template.psd";
    
    while(loop) {
      openTemplate(filePath);
      var layerRef = app.activeDocument.layers.getByName("Text"); //target text layer
      var newText = prompt("Editing " + layerRef.name, "enter new text: "); //user input
    
      if(newText == "stop") { //stop loop by entering 'stop'
        loop = false;
      }
    
      layerRef.textItem.contents = newText;
      var savePath = app.path.toString() + "/Samples/" + newText + ".psd";
      var saveFile = new File(savePath);
      savePSD(saveFile);
      app.activeDocument.close(SaveOptions.DONOTSAVECHANGES);
    }
    
    function openTemplate(filePath) { //open template.psd
      var fileRef = new File(app.path.toString() + filePath);
      var docRef = open(fileRef);
    }
    
    function savePSD(saveFile) { //saveas newText.psd
      var saveOptions = new PhotoshopSaveOptions();
      saveOptions.alphaChannels = false;
      saveOptions.annotations = false;
      saveOptions.embedColorProfile = true;
      saveOptions.layers = true;
      saveOptions.spotColors = false;
      app.activeDocument.saveAs(saveFile, saveOptions, true, Extension.LOWERCASE);
    }