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?
(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)
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
...
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
...
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.
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)
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)
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!
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?
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?
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);
}
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?
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?
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:
Copy link to clipboard
Copied
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();
Copy link to clipboard
Copied
THANKS ALOT. It works again 🙂
Copy link to clipboard
Copied
Thank you for the efforts, but this code won't run. Error 8800. (the last line)
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.
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.
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
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?
Copy link to clipboard
Copied
Hey @Kukurykus ,
your right, the problem was the file name and now this script works as well.
Thank you.
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.
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.
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.
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.
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.
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 🙂