Skip to content

Instantly share code, notes, and snippets.

@DareWreck
Last active July 18, 2017 18:53
Show Gist options
  • Select an option

  • Save DareWreck/8444593 to your computer and use it in GitHub Desktop.

Select an option

Save DareWreck/8444593 to your computer and use it in GitHub Desktop.
Photoshop save image as jpg in image origin folder with appended file name for use in conjunction with an action/droplet combo
var docRef = app.activeDocument;
function ExportJpg(path, filename, qual)
{
try
{
var options = new ExportOptionsSaveForWeb();
jpgFile = new File( path + "\\" + filename );
options.quality = qual; // Start with highest quality (biggest file).
options.format = SaveDocumentType.JPEG; // Or it'll default to GIF.
docRef.exportDocument(jpgFile, ExportType.SAVEFORWEB, options);
}
catch (e)
{ // display error
alert("Error encountered when saving the image! \r\r" + e);
return; // quit
}
};
function main()
{
try
{
var docRef = app.activeDocument;
// for (var key in docRef) {
// alert(key + ': ' + docRef[key]);
// }
// var fname = docRef.name.split("."); // splits at first sign of "."
// var fname = fname[0];
var fname = docRef.name.substring(0, docRef.name.lastIndexOf(".")); // will split before the LAST "."
ExportJpg(app.activeDocument.fullName.parent.fsName, fname + "-optimized.jpg", 70);
}
catch (e)
{ // display error
alert("Error encountered when saving the image! \r\r" + e);
return; // quit
}
};
// execute script
main();
@Cyril0001
Copy link

Could you please help me scripting a Save As animated GIF with "loop forever" and name, as a layer name?

I have the script blow that saves JPG using a layer name. It works great.

I am trying to update this script so, that it would save animated GIF with "loop forever" option.

It would be just awesome if there is a possibility to set in the script quantity of GIF's colors (64, 128, 256), Transparency, Dithering and other parameters.

Any help would be greatly appreciated!

#target photoshop
main();
function main(){
if(!documents.length) return;
try{
var Path= activeDocument.path;
}catch(e){var Path = "~/desktop";}
var Name = decodeURI(app.activeDocument.name).replace(/.[^\.]+$/, '');
var layerName = app.activeDocument.activeLayer.name.replace(/[:/\*?"<>|]/g, "_");
var saveFile= new File(Path + "/" + Name + "-" + layerName + ".jpg");
SaveForWeb(saveFile,80);
}
function SaveForWeb(saveFile,jpegQuality) {
var sfwOptions = new ExportOptionsSaveForWeb();
sfwOptions.format = SaveDocumentType.JPEG;
sfwOptions.includeProfile = false;
sfwOptions.interlaced = 0;
sfwOptions.optimized = true;
sfwOptions.quality = jpegQuality;
activeDocument.exportDocument(saveFile, ExportType.SAVEFORWEB, sfwOptions);
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment