Last active
December 22, 2020 16:51
-
-
Save lstellway/fab922546699a227b6a8dcb8b754525e to your computer and use it in GitHub Desktop.
Photoshop Save for Web Script
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Add array indexOf() method | |
* | |
* @param {object} Object | |
* @param {int} max | |
* @param {int} min) | |
* @return {mixed} | |
*/ | |
if (!Array.prototype.indexOf) Array.prototype.indexOf = (function(Object, max, min){ | |
"use strict"; | |
return function indexOf(member, fromIndex) { | |
if(this===null||this===undefined)throw TypeError("Array.prototype.indexOf called on null or undefined"); | |
var that = Object(this), Len = that.length >>> 0, i = min(fromIndex | 0, Len); | |
if (i < 0) i = max(0, Len+i); else if (i >= Len) return -1; | |
if(member===void 0){ for(; i !== Len; ++i) if(that[i]===void 0 && i in that) return i; // undefined | |
}else if(member !== member){ for(; i !== Len; ++i) if(that[i] !== that[i]) return i; // NaN | |
}else for(; i !== Len; ++i) if(that[i] === member) return i; // all else | |
return -1; // if the value was not found, then return -1 | |
}; | |
})(Object, Math.max, Math.min); | |
/** | |
* Save for web | |
*/ | |
var s = { | |
defaults: { | |
width: '1000px', | |
quality: 60, | |
warnSize: UnitValue('3000px') | |
}, | |
imgTypes: ['jpg','jpeg','gif','png','pdf','svg'], | |
files: null, | |
destination: null, | |
width: null, | |
quality: null, | |
/** | |
* Set files | |
*/ | |
setFiles: function() { | |
this.files = openDialog(); | |
}, | |
/** | |
* Set image width | |
*/ | |
setWidth: function() { | |
var w = prompt('What pixel width would you like to resize your images to?', '1000 px'); | |
this.width = UnitValue(w ? w : this.defaults.width); | |
}, | |
/** | |
* Set quality | |
*/ | |
setQuality: function(q) { | |
q = parseInt(prompt('What quality would you like the images saved at? (0-100)', this.defaults.quality)); | |
if (!q || q < 0 || q > 100) { | |
this.setQuality(); | |
} | |
this.quality = q; | |
}, | |
/** | |
* Set save for web options | |
*/ | |
setSaveForWebOptions: function() { | |
this.saveForWebOptions = new ExportOptionsSaveForWeb(); | |
this.saveForWebOptions.quality = this.quality; | |
this.saveForWebOptions.format = SaveDocumentType.JPEG; | |
this.saveForWebOptions.saveForWebOptions = true; | |
}, | |
/** | |
* Check width | |
*/ | |
checkWidth: function(c) { | |
if (this.width.as('px') > this.defaults.warnSize.as('px')) { | |
c = confirm('The provided size is very large to save for web. Are you sure you would like to continue?'); | |
if (c) return true; | |
if (confirm('Would you like to provide a different width?')) this.init(); | |
return false; | |
} | |
return true; | |
}, | |
/** | |
* Set save destination | |
*/ | |
setDestination: function() { | |
this.destination = Folder.selectDialog("Choose Save Destination"); | |
}, | |
/** | |
* Save for web | |
* | |
* @param File | |
*/ | |
saveForWeb: function(file) { | |
// Ensure valid file type | |
if (file && file.constructor && file.constructor == File) { | |
var doc = open(file), | |
w = doc.width, | |
h = doc.height, | |
name = doc.name.split('.'), | |
ext = name.pop(); | |
if (this.imgTypes.indexOf(ext.toLowerCase()) < 0) { | |
name.push(ext); | |
} | |
name = name.join('.'); | |
// Resize | |
if (w.as('px') > this.width.as('px')) { | |
doc.resizeImage(this.width, null, 72); | |
} | |
// Save for web | |
doc.exportDocument( | |
new File(this.destination + '/' + name + '.jpg'), | |
ExportType.SAVEFORWEB, | |
this.saveForWebOptions | |
); | |
// Close | |
doc.close(SaveOptions.DONOTSAVECHANGES); | |
} | |
}, | |
/** | |
* Initialize script | |
*/ | |
init: function() { | |
if (!this.files) this.setFiles(); | |
if (!this.destination) this.setDestination(); | |
this.setWidth(); | |
this.setQuality(); | |
this.setSaveForWebOptions(); | |
// Check width | |
if (this.checkWidth()) { | |
// Loop through files | |
for (var i in this.files) { | |
this.saveForWeb(this.files[i]); | |
} | |
} | |
} | |
}; | |
s.init(); |
Very useful!
thanks for sharing buddy 👍
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Photoshop script to export images as
JPG
for web.Script workflow: