-
Notifications
You must be signed in to change notification settings - Fork 0
/
GenerateLaunchImages.jsx
97 lines (77 loc) · 4.29 KB
/
GenerateLaunchImages.jsx
1
// Author: Pankaj Phartiyal// Email: [email protected]// License Info: Free BSD// Copyright (c) 2014, Pankaj Phartiyal// All rights reserved.// Redistribution and use in source and binary forms, with or without// modification, are permitted provided that the following conditions are met:// 1. Redistributions of source code must retain the above copyright notice, this// list of conditions and the following disclaimer. // 2. Redistributions in binary form must reproduce the above copyright notice,// this list of conditions and the following disclaimer in the documentation// and/or other materials provided with the distribution.// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR// ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND// ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.// The views and conclusions contained in the software and documentation are those// of the authors and should not be interpreted as representing official policies, // either expressed or implied, of the FreeBSD Project.// Guide// http://wwwimages.adobe.com/content/dam/Adobe/en/devnet/photoshop/pdfs/photoshop_scripting_guide.pdf// JS SDK Reference// http://wwwimages.adobe.com/content/dam/Adobe/en/devnet/photoshop/pdfs/photoshop_scriptref_js.pdf// configurationvar GLIPNGCompression = 5 // 0-9, 0 being highest qualityvar GLILaunchImagesPrefix = "launch-image" // 0-9, 0 being highest qualityvar thisFile = new File($.fileName);open(File(thisFile.path + "/PSDLaunchImages.psd"))var docRef = app.activeDocumentvar layerRefvar flattenedDocRef = docRef.duplicate("Flattened Doc", true)// debug code// var layerRef = docRef.layerSets[0].layerSets[0].layerSets[0].layers[1]// flattenedDocRef.crop(layerRef.bounds)// flattenedDocRef.saveAs(new File(docRef.path+"/launch-image-iPad-Retina-Portrait.png"), new PNGSaveOptions())// flattenedDocRef.activeHistoryState = flattenedDocRef.historyStates[flattenedDocRef.historyStates.length - 2]function saveLayer(layerRef, name) { // alert(name) var propername = name.replace(/[^a-zA-Z0-9]+/g, "-").toLowerCase() flattenedDocRef.crop(layerRef.bounds) var PNGSaveOptionsVar = new PNGSaveOptions() PNGSaveOptionsVar.compression = GLIPNGCompression flattenedDocRef.saveAs(new File(docRef.path+"/"+propername+".png"), PNGSaveOptionsVar) flattenedDocRef.activeHistoryState = flattenedDocRef.historyStates[flattenedDocRef.historyStates.length - 2]}for (var i = 0; i < docRef.layerSets.length; i++) { var basename = GLILaunchImagesPrefix + "-" + docRef.layerSets[i].name // alert(thename) for (var j = 0; j < docRef.layerSets[i].layerSets.length; j++) { var level1name = basename + "-" + docRef.layerSets[i].layerSets[j].name if (docRef.layerSets[i].layerSets[j].layerSets.length == 0) { layerRef = docRef.layerSets[i].layerSets[j].layers[1] saveLayer(layerRef, level1name) } for (var k = 0; k < docRef.layerSets[i].layerSets[j].layerSets.length; k++) { if (docRef.layerSets[i].layerSets[j].layerSets[k].layerSets.length == 0) { var finalname = level1name + "-" + docRef.layerSets[i].layerSets[j].layerSets[k].name layerRef = docRef.layerSets[i].layerSets[j].layerSets[k].layers[1] saveLayer(layerRef, finalname) } else { var level2name = level1name + "-" + docRef.layerSets[i].layerSets[j].layerSets[k].name for (var l = 0; l < docRef.layerSets[i].layerSets[j].layerSets[k].layerSets.length; l++) { var level3name = level2name + '-' + docRef.layerSets[i].layerSets[j].layerSets[k].layerSets[l].name layerRef = docRef.layerSets[i].layerSets[j].layerSets[k].layerSets[l].layers[1] saveLayer(layerRef, level3name) } } } }}flattenedDocRef.close(SaveOptions.DONOTSAVECHANGES)