Skip to main content
Known Participant
August 25, 2021
Answered

save for web (lagacy) and change the file name in the same time (by script?)

  • August 25, 2021
  • 3 replies
  • 3168 views

I have a lot of psd files.

I need a script to save opened file for web (lagacy) and change the nam:

from (fileName_123456.psd) to (123456_fileName.psd)

The numbers in the fileName are not constant, they can be more or less numbers.

Can someone help me achieve my goal?

This topic has been closed for replies.
Correct answer Stephen Marsh

@Stephen Marsh  - JPG Script woks, but noth PNG

This is the Error:

- <no further information available>
Line: 27
-> doc.exportDocument(saveFilePNG, ExportType.SAVEFORWEB, exportOptions);

(Translated from German)

And this is the line Nr. 27 on my code:

doc.exportDocument(saveFilePNG, ExportType.SAVEFORWEB, exportOptions);

@hamid5C9F 

 

Try this revised version:

 

/* Save for web PNG - Transpose filename around underscore: 'fileName_123456.psd' as '123456_fileName.png' */

#target photoshop

// File naming
var doc = app.activeDocument;
var docName = doc.name.replace(/\.[^\.]+$/, '');
var docNameOutput = docName.replace(/(.+)(_)(\d+)/, '$3$2$1');

// Output directory
var outputFolder = new Folder(decodeURI('~/Desktop/PNG'));
if (outputFolder.exists === false) outputFolder.create();

// PNG S4W Options
var pngOptions = new ExportOptionsSaveForWeb();
pngOptions.PNG8 = false;
pngOptions.transparency = true;
pngOptions.interlaced = false;
pngOptions.quality = 100;
pngOptions.includeProfile = true;
pngOptions.format = SaveDocumentType.PNG;

// File path & naming
activeDocument.exportDocument(File(outputFolder + '/' + docNameOutput + '.png'), ExportType.SAVEFORWEB, pngOptions);

// Optional end of script alert, comment out or remove as required
app.beep();

3 replies

JJMack
Community Expert
Community Expert
August 25, 2021

You could most likely write a batch script to just rename the the files if  you do not need two file with identical content with different names.  The script should be much faster then opening the files in Photoshop then saving a new  PSD that has a different name that has the same content as the opened file that must be close out of Photoshop. Save for web does not support PSD.  Psd files are not used on the web. 

JJMack
hamid5C9FAuthor
Known Participant
August 26, 2021

I create my product in Photoshop and then need to save it in different formats right away, so for some formats, I need to change the name-order.

I created an action and added some scripts so now I achive some stips, but saveForWeb wont work with actions, so I need to add another script to my action.

JJMack
Community Expert
Community Expert
August 26, 2021

I Save for web work in actions but actions can not process a Document's Name and generate the output file name you want. Action step  settings are recorded when your record the step.  However you could make the step interactive by turning on the steps dialog so you can enter the file name you want save.  To Automated that process you would need to Script the Save for Web step using Action Manager code where the Scripts  process the Document's Name and generated the output file name you want used.

 

JJMack
Kukurykus
Legend
August 25, 2021

 

(o = new ExportOptionsSaveForWeb()).PNG8 = false
o.transparency = 1, o.format = SaveDocumentType.PNG
theFile = File('~/desktop/PNG/' + (aD = activeDocument)
.name.replace(/(.*)(_)(\d{3})(\.).*/, '$3$2$1$4png'))
aD.exportDocument(theFile, ExportType.SAVEFORWEB, o)

 

Stephen Marsh
Community Expert
Community Expert
August 25, 2021

My take:

 

/* Save for web JPG - Transpose filename around underscore: 'fileName_123456.psd' as '123456_fileName.jpg' */

#target photoshop

// File naming
var doc = app.activeDocument;
var docName = doc.name.replace(/\.[^\.]+$/, '');
var docNameOutput = docName.replace(/(.+)(_)(\d+)/, '$3$2$1');

// Save location - GUI
var docPath = Folder.selectDialog('Select a folder to save the JPEG image to...');

// File path & naming
var saveFileJPEG = new File(docPath + '/' + docNameOutput + '.jpg');

// Export
SaveForWeb(saveFileJPEG);

// Optional end of script message, comment out or remove as required
alert(docNameOutput + '.jpg' + '\r' + 'Saved to:' + '\r\r' + docPath.fsName);

// Optional end of script alert, comment out or remove as required
// app.beep();

// JPEG S4W Options
function SaveForWeb(saveFileJPEG) {
    var sfwOptions = new ExportOptionsSaveForWeb();
    sfwOptions.format = SaveDocumentType.JPEG;
    sfwOptions.includeProfile = true;
    sfwOptions.optimized = true;
    sfwOptions.quality = 70;
    doc.exportDocument(saveFileJPEG, ExportType.SAVEFORWEB, sfwOptions);
}

 

Rather than asking for a save location, the source PSD file location could be used instead. Conversion to sRGB could be added. Checking for overwriting an existing file of the same name could be added etc. Many things are possible! 

hamid5C9FAuthor
Known Participant
August 26, 2021

Could you edith the code, so it saves in a specific location (ex: '~/desktop/PNG/') and if exist, override the old one?

And can i use this script for save export as png by changing SaveDocumentType to PNG?

hamid5C9FAuthor
Known Participant
August 25, 2021

@Kukurykushelped me change the fileName during normal saving using this script:

(ad = activeDocument).saveAs(File("Desctop/jpg")/' + ad.name), new JPEGSaveOptions(JPEGSaveOptions.quality = 12), true).

its helpful, but I have to also save my files for web (lagacy) and it won't work in this case.

hamid5C9FAuthor
Known Participant
August 25, 2021

I meant this script:
(activeDocument).saveAs(File('~/desktop/JPG/' + activeDocument.name.replace(/(.*)(_)(\d{12})/, '$3$2$1')), new JPEGSaveOptions(JPEGSaveOptions.quality = 7), true)