Skip to main content
August 16, 2017
Answered

Script to save file with random or sequential filename to be placed in action - VARIATION

  • August 16, 2017
  • 2 replies
  • 4508 views

This has been asked before, but the answer the poster accepted didn't work for me. I basically need a script that just assigns a unique filename to my .PSD files, so I can just click on an action and not worry about naming the file in the "Save As" menu. It should be sequential as I'm using it to save out storyboards for animation. Thank you.

This topic has been closed for replies.
Correct answer SuperMerlin

Please try this..

#target photoshop

main();

function main(){

if(!documents.length) return;

var Name = app.activeDocument.name.replace(/\.[^\.]+$/, '');

Name = Name.replace(/\d+$/,'');

try{

var savePath = activeDocument.path;

}catch(e){

    alert("You must save this document first!");

    }

var fileList= savePath.getFiles(Name +"*.psd").sort().reverse();

var Suffix = 0;

if(fileList.length){

    Suffix = Number(fileList[0].name.replace(/\.[^\.]+$/, '').match(/\d+$/));

}

Suffix= zeroPad(Suffix + 1, 4);

var saveFile = File(savePath + "/" + Name + "_" + Suffix + ".psd");

SavePSD(saveFile);

}

function SavePSD(saveFile){

psdSaveOptions = new PhotoshopSaveOptions();

psdSaveOptions.embedColorProfile = true;

psdSaveOptions.alphaChannels = true; 

psdSaveOptions.layers = true; 

activeDocument.saveAs(saveFile, psdSaveOptions, true, Extension.LOWERCASE);

};

function zeroPad(n, s) {

   n = n.toString();

   while (n.length < s)  n = '0' + n;

   return n;

};

2 replies

SuperMerlin
SuperMerlinCorrect answer
Inspiring
August 16, 2017

Please try this..

#target photoshop

main();

function main(){

if(!documents.length) return;

var Name = app.activeDocument.name.replace(/\.[^\.]+$/, '');

Name = Name.replace(/\d+$/,'');

try{

var savePath = activeDocument.path;

}catch(e){

    alert("You must save this document first!");

    }

var fileList= savePath.getFiles(Name +"*.psd").sort().reverse();

var Suffix = 0;

if(fileList.length){

    Suffix = Number(fileList[0].name.replace(/\.[^\.]+$/, '').match(/\d+$/));

}

Suffix= zeroPad(Suffix + 1, 4);

var saveFile = File(savePath + "/" + Name + "_" + Suffix + ".psd");

SavePSD(saveFile);

}

function SavePSD(saveFile){

psdSaveOptions = new PhotoshopSaveOptions();

psdSaveOptions.embedColorProfile = true;

psdSaveOptions.alphaChannels = true; 

psdSaveOptions.layers = true; 

activeDocument.saveAs(saveFile, psdSaveOptions, true, Extension.LOWERCASE);

};

function zeroPad(n, s) {

   n = n.toString();

   while (n.length < s)  n = '0' + n;

   return n;

};

August 16, 2017

Thanks! I'll give this a try.

JJMack
Community Expert
Community Expert
August 16, 2017

Please explain how file name can be random and sequential.  Which do you actually want? Then waht path should thls file be saved to. If you look at the image processor  script you should be able to use the code you find in there.

JJMack
August 16, 2017

Thanks, I'll give this a try.

I guess I'm more focused on creating a unique name for a file without me having to manually type it in the "Save As" window. But for simplicity's sake, I'm going to say want sequential.