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

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

Participant ,
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

874

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 3 Correct answers

LEGEND , 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)

 

Votes

Translate

Translate
Community Expert , Aug 26, 2021 Aug 26, 2021
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.r
...

Votes

Translate

Translate
Community Expert , Sep 08, 2022 Sep 08, 2022

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

Votes

Translate

Translate
Participant ,
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.

Votes

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
Participant ,
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)

Votes

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
LEGEND ,
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)

 

Votes

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
Community Expert ,
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! 

Votes

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
Participant ,
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?

Votes

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
Community Expert ,
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);
}

 

 

 

Votes

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
Participant ,
Sep 07, 2022 Sep 07, 2022

Copy link to clipboard

Copied

This Script wont run on new version of Photohop V. 23.5.0 .

Error 8800: Photoshop general error. The feature may not be available in this version of Photoshop

Any Help please?

Votes

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
Community Expert ,
Sep 07, 2022 Sep 07, 2022

Copy link to clipboard

Copied


@hamid5C9F wrote:

This Script wont run on new version of Photohop V. 23.5.0 .

Error 8800: Photoshop general error. The feature may not be available in this version of Photoshop

Any Help please?


 

@hamid5C9F – I just tested in 23.5.0 on Mac and both the JPG and PNG scripts which I posted don't produce an error.

 

Does the error mention a line or other info to help debug the error?

 

Votes

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
Participant ,
Sep 07, 2022 Sep 07, 2022

Copy link to clipboard

Copied

@Stephen_A_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);

Votes

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
Community Expert ,
Sep 08, 2022 Sep 08, 2022

Copy link to clipboard

Copied

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

Votes

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
Participant ,
Sep 08, 2022 Sep 08, 2022

Copy link to clipboard

Copied

LATEST

THANKS ALOT. It works again 🙂

Votes

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
Participant ,
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)

Votes

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
Participant ,
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.

Votes

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
LEGEND ,
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.

Votes

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
Participant ,
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

Votes

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
LEGEND ,
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?

Votes

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
Participant ,
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.

Votes

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
LEGEND ,
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

Votes

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
Participant ,
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.

Votes

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
LEGEND ,
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

Votes

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
Participant ,
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.

Votes

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
LEGEND ,
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

Votes

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

Copy link to clipboard

Copied

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

Votes

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