Welcome Dialog

Welcome to the Community!

We have a brand new look! Take a tour with us and explore the latest updates on Adobe Support Community.


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

Explorer ,
Aug 25, 2021 Aug 25, 2021

Copy link to clipboard

Copied

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?

TOPICS
Actions and scripting, macOS

Views

199

Likes

Translate

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

correct answers 2 Correct answers

Adobe Community Professional , Aug 25, 2021 Aug 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)

Likes

Translate

Translate
Adobe Community Professional , Aug 26, 2021 Aug 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? By @hamid5C9F
@hamid5C9F – Take a look at this revised code, with PNG options specified. /* 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....

Likes

Translate

Translate
Explorer ,
Aug 25, 2021 Aug 25, 2021

Copy link to clipboard

Copied

@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.

Likes

Translate

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
Explorer ,
Aug 25, 2021 Aug 25, 2021

Copy link to clipboard

Copied

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

Likes

Translate

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 Community Professional ,
Aug 25, 2021 Aug 25, 2021

Copy link to clipboard

Copied

 

(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)

 

Likes

Translate

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 Community Professional ,
Aug 25, 2021 Aug 25, 2021

Copy link to clipboard

Copied

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! 

Likes

Translate

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
Explorer ,
Aug 26, 2021 Aug 26, 2021

Copy link to clipboard

Copied

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?

Likes

Translate

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 Community Professional ,
Aug 26, 2021 Aug 26, 2021

Copy link to clipboard

Copied

quote

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?


By @hamid5C9F

 

@hamid5C9F – Take a look at this revised code, with PNG options specified.

 

/* 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();

// File path & naming
var saveFilePNG = new File(outputFolder + '/' + docNameOutput + '.png');

// Export
SaveForWeb(saveFilePNG);

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

// PNG S4W Options
function SaveForWeb(saveFilePNG) {
    exportOptions = new ExportOptionsSaveForWeb();
    exportOptions.format = SaveDocumentType.PNG;
    exportOptions.PNG8 = false; // false = PNG-24
    exportOptions.transparency = true; // true = transparent
    exportOptions.interlaced = false; // true = interlacing on
    exportOptions.includeProfile = true; // false = don't embedd ICC profile
    doc.exportDocument(saveFilePNG, ExportType.SAVEFORWEB, exportOptions);
}

 

 

 

Likes

Translate

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
Explorer ,
Aug 26, 2021 Aug 26, 2021

Copy link to clipboard

Copied

Thank you for the efforts, but this code won't run. Error 8800. (the last line)

Likes

Translate

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
Explorer ,
Aug 26, 2021 Aug 26, 2021

Copy link to clipboard

Copied

I meant this code:

(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)

I donnt know why the ansewer came down here.

Likes

Translate

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 Community Professional ,
Aug 26, 2021 Aug 26, 2021

Copy link to clipboard

Copied

For me it works with no error. If you have PNG folder on your destkop, then create some document and save it on desktop as nme_123.psd. Close and open it and run my script. If error will occur again I guess it's because of same reason it happened earlier (Action or Script to automate saving PNGs without appending "copy" to the filename.). If that's true that's very weird since there is no reason the code could not work with aD, but ad variable you found can be used. Change that and tell me if that really was the culprit.

Likes

Translate

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
Explorer ,
Aug 26, 2021 Aug 26, 2021

Copy link to clipboard

Copied

Hey @Kukurykus,

it works now, but somehow its saves as psd file format.

I appreciate your efforts but I got my answer now 🙂

Thank you

Likes

Translate

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 Community Professional ,
Aug 26, 2021 Aug 26, 2021

Copy link to clipboard

Copied

Did you try for a test with exactly same name as I suggested? The 3 digits was used due to your previous request where you needed such length. Anyway I'm curious if that is the reason it saves your .psd as .psd. It should when you don't use a name with that structure. That's easy to change if you'd like to know how. To fit it to other construction change {3} to *? and let know if that saves the file as .png?

Likes

Translate

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
Explorer ,
Aug 26, 2021 Aug 26, 2021

Copy link to clipboard

Copied

Hey @Kukurykus ,

your right, the problem was the file name and now this script works as well.

Thank you.

Likes

Translate

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 Community Professional ,
Aug 25, 2021 Aug 25, 2021

Copy link to clipboard

Copied

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. 

image.png

JJMack

Likes

Translate

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
Explorer ,
Aug 26, 2021 Aug 26, 2021

Copy link to clipboard

Copied

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.

Likes

Translate

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 Community Professional ,
Aug 26, 2021 Aug 26, 2021

Copy link to clipboard

Copied

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.

image.png

 

JJMack

Likes

Translate

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
Explorer ,
Aug 26, 2021 Aug 26, 2021

Copy link to clipboard

Copied

This is what I've done now.

I use one action for saving my document in different formats.

During the procces, this action triggers the scripts to change the name of some and saves it for a specific format/location.

Likes

Translate

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 Community Professional ,
Aug 27, 2021 Aug 27, 2021

Copy link to clipboard

Copied

That is the best way to handle your renaming.  Batch and image processors can name output files well but they do not support parsing the document current name and reconstruct the name like you want to do. So you need the script the reconstructtion of the document name and do the save as file type in your scripts. 

JJMack

Likes

Translate

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
Explorer ,
Aug 27, 2021 Aug 27, 2021

Copy link to clipboard

Copied

LATEST

I also use script for saving files as JPEG, otherwise there is copy at the end of my File-Name 🙂

Likes

Translate

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