A Photoshop script to automate save for web as JPEG. Save the file as using jsx extenstion. Then in Photoshop go to "File-Scripts-Browse..." to load the file. This script will overwrite the existing output file if it already exists.
// Utils | |
function trace() { | |
$.writeln.apply($, arguments); | |
} | |
// Construct the dialog layout | |
var layoutInfo = "dialog{\ | |
text: 'Save for Web in JPEG',\ | |
info: Panel {\ | |
orientation: 'column',\ | |
alignChildren: 'left',\ | |
text: 'Source and Output Settings',\ | |
input: Group{\ | |
orientation: 'row',\ | |
label: StaticText { text: 'Input:', size: [ 60, 24 ] },\ | |
el: EditText { characters: 30 },\ | |
btn: Button { text: 'Choose...' }\ | |
},\ | |
output: Group{\ | |
orientation: 'row',\ | |
label: StaticText { text: 'Output:', size: [ 60, 24 ] },\ | |
el: EditText { characters: 30 },\ | |
btn: Button { text: 'Choose...' }\ | |
},\ | |
quality: Group{\ | |
orientation: 'row',\ | |
label: StaticText { text: 'Quality:', size: [ 60, 24 ] },\ | |
el: EditText { characters: 3 },\ | |
label: StaticText { text: '(1-100)' }\ | |
}\ | |
},\ | |
buttons: Group {\ | |
orientation: 'row',\ | |
cancelBtn: Button { text: 'Cancel', properties: { name: 'cancel' } },\ | |
okBtn: Button { text: 'OK', properties: { name: 'ok' } }\ | |
}\ | |
}"; | |
var dialog = new Window(layoutInfo); | |
// Get references of all the buttons and inputs | |
// Need to update the properties if layout changes | |
var inputGroup = dialog.children[0].children[0]; | |
var outputGroup = dialog.children[0].children[1]; | |
var qualityGroup = dialog.children[0].children[2]; | |
var chooseInputBtn = inputGroup.children[2]; | |
var chooseOutputBtn = outputGroup.children[2]; | |
var okBtn = dialog.children[1].children[1]; | |
var inputBox = inputGroup.children[1]; | |
var outputBox = outputGroup.children[1]; | |
var qualityBox = qualityGroup.children[1]; | |
// Event binding | |
chooseInputBtn.onClick = chooseInput; | |
chooseOutputBtn.onClick = chooseOutput; | |
okBtn.onClick = runAction; | |
// Handle events | |
function chooseInput(e) { | |
var directory = Folder.selectDialog('Choose the folder of source images'); | |
if (directory) inputBox.text = directory; | |
} | |
function chooseOutput(e) { | |
var directory = Folder.selectDialog('Choose the output folder'); | |
if (directory) outputBox.text = directory; | |
} | |
function runAction(e) { | |
if ( | |
inputBox.text.length === 0 || | |
outputBox.text.length === 0 | |
) { | |
return alert('Please check your input or output directory'); | |
} | |
var quality = parseInt(qualityBox.text, 10); | |
if (quality < 1) quality = 1; | |
else if (quality > 100) quality = 100; | |
else if (!quality) quality = 75; | |
var inputFiles = readInputFolder(inputBox.text); | |
if (!inputFiles) return; | |
dialog.close(); | |
for (var i = 0; i < inputFiles.length; i++) { | |
if (!inputFiles[i] instanceof File) continue; | |
saveForWebAsJpeg(inputFiles[i], quality); | |
} | |
alert('Done'); | |
} | |
/** | |
* Get image files from a folder. | |
* This method doesn't go into subfolders. | |
* @param {string} path - The directory | |
* @return {array} An array of File instances or undefined if the folder doesn't exist. | |
*/ | |
function readInputFolder(path) { | |
var folder = new Folder(path); | |
if (!folder) return alert("Can't find the input folder"); | |
return folder.getFiles(/\.(jpe?g|tif|psd|bmp|gif|png)$/i); | |
} | |
/** | |
* Saves a file using save for web command as JPEG, | |
* using a user defined quality setting. | |
* The quality and output path is set in the initial dialog box. | |
* @param {object} file - A photoshop script File instance | |
* @param {quality} number - The quality of the produced image as a percentage | |
*/ | |
function saveForWebAsJpeg(file, quality) { | |
var saveOptions = new ExportOptionsSaveForWeb(); | |
saveOptions.format = SaveDocumentType.JPEG; | |
saveOptions.quality = quality; | |
var saveFile = new File(outputBox.text + '/' + file.name.replace(/\.[^\.]+$/, '') + '.jpg'); | |
if (saveFile.exists) saveFile.remove(); | |
app.open(file); | |
app.activeDocument.exportDocument(saveFile, ExportType.SAVEFORWEB, saveOptions); | |
app.activeDocument.close(SaveOptions.DONOTSAVECHANGES); | |
} | |
// Show the modal dialog view | |
dialog.center(); | |
dialog.show(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This comment has been minimized.
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);
}