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

Scripting a Save As command

New Here ,
Mar 11, 2010 Mar 11, 2010

Copy link to clipboard

Copied

Is there a way to script a Save As dialog?  I need to resize an image multiple times and between each time have it save the file to the original location as a jpeg with a suffix.  I already have the script to resize the image, but I cannot find a Save As script.  Any help would be appreciated.

TOPICS
Actions and scripting

Views

60.7K

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 1 Correct answer

Guru , Mar 11, 2010 Mar 11, 2010
var doc = app.activeDocument;
var docName = doc.name;
docName = docName.match(/(.*)(\.[^\.]+)/) ? docName = docName.match(/(.*)(\.[^\.]+)/):docName = [docName, docName];
var suffix = '_300';
var saveName = new File(decodeURI(doc.path)+'/'+docName[1]+suffix+'.jpg');
saveJPEG( app.activeDocument, saveName, 10 );

Votes

Translate

Translate
Adobe
Guru ,
Mar 11, 2010 Mar 11, 2010

Copy link to clipboard

Copied

The scripting documenttion is shipped with Photoshop. You should have the guides for javascript, applescript, and vbs if you did a normal install.

The exact syntax will depend on which langauage you use and what format you want to save the file.

But if you just want to script opening the SaveAs dialog so the user can interact with it, here is how to do that in javascript

executeAction( charIDToTypeID( "save" ), undefined, DialogModes.ALL );

And here is a javascript function for saving a copy as jpeg

function saveJPEG( doc, saveFile, qty ) {
     var saveOptions = new JPEGSaveOptions( );
     saveOptions.embedColorProfile = true;
     saveOptions.formatOptions = FormatOptions.STANDARDBASELINE;
     saveOptions.matte = MatteType.NONE;
     saveOptions.quality = qty;
     doc.saveAs( saveFile, saveOptions, true );
}

saveJPEG( app.activeDocument, new File('~/Desktop/sample.jpg'), 10 );

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
New Here ,
Mar 11, 2010 Mar 11, 2010

Copy link to clipboard

Copied

Thank you for your help.  How would I adjust the last line to save the file into the folder it opened from?

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
Guru ,
Mar 11, 2010 Mar 11, 2010

Copy link to clipboard

Copied

saveJPEG( app.activeDocument, new File(decodeURI(app.activeDocument.path)+'/sample.jpg'), 10 );

Note that should throw an error if the file has never been saved. And it needs the saveJPEG function from the post above.

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
New Here ,
Mar 11, 2010 Mar 11, 2010

Copy link to clipboard

Copied

Thank you very much.


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
New Here ,
Mar 11, 2010 Mar 11, 2010

Copy link to clipboard

Copied

I have one more question.  How would I also save it with the current document name plus a suffix?

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
Guru ,
Mar 11, 2010 Mar 11, 2010

Copy link to clipboard

Copied

var doc = app.activeDocument;
var docName = doc.name;
docName = docName.match(/(.*)(\.[^\.]+)/) ? docName = docName.match(/(.*)(\.[^\.]+)/):docName = [docName, docName];
var suffix = '_300';
var saveName = new File(decodeURI(doc.path)+'/'+docName[1]+suffix+'.jpg');
saveJPEG( app.activeDocument, saveName, 10 );

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
New Here ,
Mar 12, 2010 Mar 12, 2010

Copy link to clipboard

Copied

Thank you all for the help.  A job that would have taken days will now take about 30 minutes.

You saved my sanity.

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
Explorer ,
Sep 14, 2011 Sep 14, 2011

Copy link to clipboard

Copied

This is a great script. Thank you.

I do have one question:

How would you take this same script and add a subfolder to save the files? I'll say the new folder name is /images just to be generic.

I am having issues trying to do this.

Thanks

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
Valorous Hero ,
Mar 11, 2010 Mar 11, 2010

Copy link to clipboard

Copied

This is one way..


var doc = app.activeDocument;
var Path = doc.path;
var Name = doc.name.replace(/\.[^\.]+$/, '');
var Suffix = "-Copy";
var saveFile = File(Path + "/" + Name + Suffix + ".jpg");
SaveJPEG(saveFile, 8);

function SaveJPEG(saveFile, jpegQuality){
jpgSaveOptions = new JPEGSaveOptions();
jpgSaveOptions.embedColorProfile = true;
jpgSaveOptions.formatOptions = FormatOptions.STANDARDBASELINE;
jpgSaveOptions.matte = MatteType.NONE;
jpgSaveOptions.quality = jpegQuality;
activeDocument.saveAs(saveFile, jpgSaveOptions, true,Extension.LOWERCASE);
}

Edit Mike was much faster than me

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
New Here ,
Dec 16, 2015 Dec 16, 2015

Copy link to clipboard

Copied

Sorry to bother you but do you know if its possible to use a default location or ask for a location if there is no doc.path (Image pasted into a new file)?

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 ,
Dec 16, 2015 Dec 16, 2015

Copy link to clipboard

Copied

Yes

If you look at the code you can see the script sets the Path and the rest of the saveFile. Path does not have to be set to the path of the origibal backing file.  If the document is new it has no backing file the code would actually fail.

  1. var doc = app.activeDocument; 
  2. var Path = doc.path;  <=============== if the document is new there is no doc.path
  3. var Name = doc.name.replace(/\.[^\.]+$/, '');  
  4. var Suffix = "-Copy"; 
  5. var saveFile = File(Path + "/" + Name + Suffix + ".jpg");

,

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
New Here ,
Dec 17, 2015 Dec 17, 2015

Copy link to clipboard

Copied

Really sorry to bother you again but is there code where I can say (abridged for lack of knowledge on my part) if the variable Path is empty, use folder.myDocuments otherwise use doc.path? This is what I'm using:

var doc = app.activeDocument;
var Path = Folder.myDocuments;
var w = app.activeDocument.width.toString().replace(' px', '');
var h = app.activeDocument.height.toString().replace(' px', '');
var Name = app.activeDocument.activeLayer.name.replace(/[:\/\\*\?\"\<\>\|\ ]/g, "_");  // '/\:*?"<>| ' -> '_'
var Name = app.activeDocument.activeLayer.name.replace(/[.jpg\ ]/g, "_"); 
var Suffix = "_Device-Desktop_" + w + 'x' + h ;
var saveFile = File(Path + "/" + Name + Suffix + ".jpg"); 
SaveJPEG(saveFile, 8); 
function SaveJPEG(saveFile, jpegQuality){  
jpgSaveOptions = new JPEGSaveOptions(); 
jpgSaveOptions.embedColorProfile = true; 
jpgSaveOptions.formatOptions = FormatOptions.STANDARDBASELINE; 
jpgSaveOptions.matte = MatteType.NONE; 
jpgSaveOptions.quality = jpegQuality; 
activeDocument.saveAs(saveFile, jpgSaveOptions, true,Extension.LOWERCASE); 

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 ,
Dec 17, 2015 Dec 17, 2015

Copy link to clipboard

Copied

You could make the script interactive and Prompt for a path. If the user does not supply one if the document is not new there will be a doc.path available for you to use.  Scripts can use logic like this.

path = prompt for a path.

If path is not defined or is blank use doc path if one exists else message user you need to save the document or supply a path.

The thing is you should learn a little scripting if you want to do things like this.  I have a programming background but do not actually know javascript.  However I know how to search forums like this to find code the does something like I want to do and can hack it to do what I want it to do.

https://forums.adobe.com/search.jspa?place=%2Fplaces%2F1383833&sort=updatedDesc&q=prompt

For some unknown reason the abve search is not pass promt as the search item please type prompt into the search filed

you may fine code the looks like this:

if (to=="") {to=prompt("Enter email address",to);}    // Prompt for email address if no default


You may be able to change it to be something like


var path = '';

if (path=="") {path=prompt("Enter folder's path",path);}  // Prompt for path address if no default

if (path=="") {

   try {var path = activeDocument.path +"/";}

   catch (e) {path=prompt("New Document  enter folder path",path);}    // New doc Path

}

if (path=="") {alert("No Path Supplied not saved"); return; } //exit

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
Community Beginner ,
Oct 23, 2017 Oct 23, 2017

Copy link to clipboard

Copied

hello jjmack, sorry for bumping old thread,

i want to use this script,

  1. var doc = app.activeDocument; 
  2. var Path = doc.path; 
  3. var Name = doc.name.replace(/\.[^\.]+$/, '');  
  4. var Suffix = "-Copy"; 
  5. var saveFile = File(Path + "/" + Name + Suffix + ".jpg"); 
  6. SaveJPEG(saveFile, 8); 
  7.  
  8. function SaveJPEG(saveFile, jpegQuality){ 
  9. jpgSaveOptions = new JPEGSaveOptions(); 
  10. jpgSaveOptions.embedColorProfile = true; 
  11. jpgSaveOptions.formatOptions = FormatOptions.STANDARDBASELINE; 
  12. jpgSaveOptions.matte = MatteType.NONE; 
  13. jpgSaveOptions.quality = jpegQuality; 
  14. activeDocument.saveAs(saveFile, jpgSaveOptions, true,Extension.LOWERCASE); 

this script always save with "copy" in the end, but when i do with the same file name it will replace with the new one but same filename

how to make the append of "copy" file name,

what i want is how to make this script append the end of "copy" filename.

for example "copy_1" , "copy_2"  , "copy_3", etc

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 ,
Oct 23, 2017 Oct 23, 2017

Copy link to clipboard

Copied

You would need to test the existing filenames that exist with the serial suffix and add one tow the serial number for the next filename. This script will save a

PSD file versions and a matching jpeg file.

#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!\nTo establish a save path...");

return

    }

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 saveName = File(savePath + "/" + Name + "_" + Suffix)

SavePSD(saveName);

SaveJPEG(saveName,8);

alert("Versions " + Name + "_" + Suffix + " saved.")

}

//////////////////////////////////////////////////////////////////////////////////////////////////

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; 

};

function SaveJPEG(saveFile, jpegQuality){  

jpgSaveOptions = new JPEGSaveOptions();  

jpgSaveOptions.embedColorProfile = true;  

jpgSaveOptions.formatOptions = FormatOptions.STANDARDBASELINE;  

jpgSaveOptions.matte = MatteType.NONE;  

jpgSaveOptions.quality = jpegQuality;   

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

};  

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
Community Beginner ,
Oct 31, 2017 Oct 31, 2017

Copy link to clipboard

Copied

You seem to be rocking this, Ive been looking everywhere for a script to save a PSD with layers to a Targa (.tga) + a suffix..for example,

I need to change the copy 15 times on the same PSD and export them quick as TGA.

Could I use the above script but tweak it for tga files?

Would save me a lot of time if you could help me!

THANKS!

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 ,
Oct 31, 2017 Oct 31, 2017

Copy link to clipboard

Copied

Modify it any way you want you do not need my permission.

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
Community Beginner ,
Nov 02, 2017 Nov 02, 2017

Copy link to clipboard

Copied

Haha thanks JJMack, I don't have your skills thou, so my request would be if you could help me modify this script? to save .tga files in a folder (or same folder) but with a new suffix (ex: image_01.tga , image_02.tga etc..)

Mental karma and mentalbeer from Sweden.

THANKS!

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 ,
Oct 11, 2018 Oct 11, 2018

Copy link to clipboard

Copied

An old thread and I don’t expect that dominiquehs is still around or has a burning need for this anymore… however it is a perfect opportunity for me to practice/learn scripting by making modifications to scripts that I could not currently write from scratch.

//https://forums.adobe.com/message/9934728#9934728

//https://forums.adobe.com/message/10673464#10673464

//Save PSD & TGA Numbered Versions.jsx

#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!\nTo establish a save path..."); 

return 

    }  

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 saveName = File(savePath + "/" + Name + "_" + Suffix) 

SavePSD(saveName); 

SaveTARGA(saveName); 

alert("Versions " + Name + "_" + Suffix + " saved.") 

}  

////////////////////////////////////////////////////////////////////////////////////////////////// 

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;   

};  

function SaveTARGA(saveFile){    

SaveOptions = new TargaSaveOptions();    

TargaSaveOptions.alphaChannels = true; //include alpha channels, change to false for none   

TargaSaveOptions.resolution = TargaBitsPerPixels.TWENTYFOUR; //options of SIXTEEN or THIRTYTWO

TargaSaveOptions.rleCompression = true; //false for no compression

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

};

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 Beginner ,
Aug 17, 2021 Aug 17, 2021

Copy link to clipboard

Copied

Can u give a script for Save PSD & JPEG Numbered Versions.jsx

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 17, 2021 Aug 17, 2021

Copy link to clipboard

Copied

It's in the post you reply to.

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 Beginner ,
Aug 17, 2021 Aug 17, 2021

Copy link to clipboard

Copied

I need some javascript for photoshop

1. Save PSD & jpeg Numbered Versions.jsx

2. PSD and PNG templates Viewer and selector from a destination folder (which i stored in my D Drive) dialog box

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 18, 2021 Aug 18, 2021

Copy link to clipboard

Copied

What happened to your D drive? No backup?

 

As @Kukurykus mentioned, it appears to be the script modified by JJMack above on the 24th October 2017:

 

Scripting a Save As command

 

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 Beginner ,
Aug 18, 2021 Aug 18, 2021

Copy link to clipboard

Copied

I tried JJMack script but its saved only same file with rename.  I want to save difrent file with serial number in a same folder

 

 

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