Last active
September 20, 2022 09:38
-
-
Save patmandenver/da8d077724acdb5b0103fd2806066fbf to your computer and use it in GitHub Desktop.
Photoshop script to save a file in multiple different sizes as a png file
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
| // Create Multiple icon files | |
| // Author: Patrick Bailey | |
| // License: MIT | |
| // | |
| //Copyright 2017 T. Patrick Bailey | |
| //Permission is hereby granted, free of charge, to any person obtaining a copy | |
| //of this software and associated documentation files (the "Software"), to | |
| //deal in the Software without restriction, including without limitation the | |
| //rights to use, copy, modify, merge, publish, distribute, sublicense, and/or | |
| //sell copies of the Software, and to permit persons to whom the Software is | |
| //furnished to do so, subject to the following conditions: | |
| // | |
| //The above copyright notice and this permission notice shall be included in | |
| //all copies or substantial portions of the Software. | |
| // | |
| //THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, | |
| //INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A | |
| //PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT | |
| //HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION | |
| //OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE | |
| //OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | |
| // | |
| // bring Photoshop into focus | |
| #target photoshop | |
| main(); | |
| function main() { | |
| app.preferences.rulerUnits = Units.PIXELS; // use pixels | |
| var dir = app.activeDocument.path | |
| //Make a copy | |
| var docCopy = app.activeDocument.duplicate(); | |
| var origWidth = docCopy.width.value; | |
| var origHeight = docCopy.height.value; | |
| var initialPrefs = app.preferences.rulerUnits; // will restore at end | |
| var sfw = new ExportOptionsSaveForWeb(); | |
| sfw.format = SaveDocumentType.PNG; | |
| sfw.PNG8 = false; // use PNG-24 | |
| sfw.transparency = true; | |
| if(origWidth == origHeight){ | |
| // Folder selection dialog | |
| var destFolder = Folder(dir).selectDlg("Select a Folder to save Icons in"); | |
| // var destFolder = Folder(dir).selectDialog( "Choose an output folder"); | |
| var iconName =prompt("Enter name of icon (all icons will start with this name","","Icon Name"); | |
| var sizeArr = [512, 256, 128, 96, 64, 48, 32, 24, 20, 16 ] | |
| var goForIt = confirm("This will make the following icon folder sizes [" +sizeArr + "] and the icon prefix name is: " + iconName ); | |
| try { | |
| if (goForIt) { | |
| for(i = 0; i < sizeArr.length; i++) { | |
| docCopy.resizeImage(UnitValue(sizeArr[i], "px"), UnitValue(sizeArr[i], "px"), null, ResampleMethod.BICUBIC) | |
| docCopy.exportDocument(new File(destFolder + "/" + iconName + "_" + sizeArr[i] + ".png"), ExportType.SAVEFORWEB, sfw); | |
| //Bring back this for demo purposes | |
| // confirm("Size is :" + sizeArr[i] + " name would be: " + iconName + "_" + sizeArr[i] + ".png") | |
| } | |
| } | |
| } | |
| catch(exception) { | |
| confirm("ERROR: " + exception) | |
| } | |
| finally{ | |
| app.preferences.rulerUnits = initialPrefs; // restore prefs | |
| if(docCopy != null){ | |
| docCopy.close(SaveOptions.DONOTSAVECHANGES); | |
| } | |
| } | |
| } | |
| else{ | |
| confirm("Height and width must be identical"); | |
| } | |
| } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment