SHARE
TWEET

Export for iOS PhotoShop Script

a guest May 18th, 2012 1,181 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**
  2.  * Export for iOS Photoshop Script
  3.  *
  4.  * Author: Daniel Wood (runloop.com)
  5.  * Twitter: @loadedwino
  6.  *
  7.  * This script is intended to be used on a photoshop document containing retina
  8.  * artwork for iOS. It will resize, trim and save the selected layer or group, into a
  9.  * directory you select using the layer name (normalised) for the file name. There are a  
  10.  * couple of resizing options you can select such as the
  11.  * resizing method and whether to scale styles or not. It does not alter your original
  12.  * document in anyway.
  13.  *
  14.  * Feel free to share/reuse/modify to your hearts content. Attribution would be nice but
  15.  * is not required.
  16.  */
  17.  
  18. // constants
  19. var ResizeMethod = {
  20.     NEARESTNEIGHBOUR: {name: 'Nearest Neighbour', value: 'Nrst'},
  21.     BILINEAR: {name: 'Bilinear', value: 'Blnr'},
  22.     BICUBIC: {name: 'Bicubic', value: 'Bcbc'},
  23.     BICUBICSMOOTHER: {name: 'Bicubic Smoother', value: 'bicubicSmoother'},
  24.     BICUBICSHARPER: {name: 'Bicubic Sharper', value: 'bicubicSharper'}
  25. };
  26. var resizeMethodLookup = {};
  27. resizeMethodLookup[ResizeMethod.NEARESTNEIGHBOUR.name] = ResizeMethod.NEARESTNEIGHBOUR.value;
  28. resizeMethodLookup[ResizeMethod.BILINEAR.name] = ResizeMethod.BILINEAR.value;
  29. resizeMethodLookup[ResizeMethod.BICUBIC.name] = ResizeMethod.BICUBIC.value;
  30. resizeMethodLookup[ResizeMethod.BICUBICSMOOTHER.name] = ResizeMethod.BICUBICSMOOTHER.value;
  31. resizeMethodLookup[ResizeMethod.BICUBICSHARPER.name] = ResizeMethod.BICUBICSHARPER.value;
  32.  
  33. var ResizeOptions = {
  34.     REGULAR: 1,
  35.     RETINA: 2
  36. };
  37.  
  38. var exportDialog;
  39.  
  40. // http://davidchambersdesign.com/photoshop-save-for-web-javascript/
  41. function saveForWebPNG(doc, outputFolderStr, filename)
  42. {
  43.     var opts, file;
  44.     opts = new ExportOptionsSaveForWeb();
  45.     opts.format = SaveDocumentType.PNG;
  46.     opts.PNG8 = false;
  47.     opts.quality = 100;
  48.     if (filename.length > 27) {
  49.         file = new File(outputFolderStr + "/temp.png");
  50.         doc.exportDocument(file, ExportType.SAVEFORWEB, opts);
  51.         file.rename(filename + ".png");
  52.     }
  53.     else {
  54.         file = new File(outputFolderStr + "/" + filename + ".png");
  55.         doc.exportDocument(file, ExportType.SAVEFORWEB, opts);
  56.     }
  57. }
  58.  
  59. function resizeImage(width, method, scaleStyles)
  60. {
  61.     var idImgS = charIDToTypeID( "ImgS" );
  62.     var desc4 = new ActionDescriptor();
  63.     var idWdth = charIDToTypeID( "Wdth" );
  64.     var idPxl = charIDToTypeID( "#Pxl" );
  65.     desc4.putUnitDouble( idWdth, idPxl, width );
  66.    
  67.     if(scaleStyles == true)
  68.     {
  69.         var idscaleStyles = stringIDToTypeID( "scaleStyles" );
  70.         desc4.putBoolean( idscaleStyles, true );
  71.     }
  72.    
  73.     var idCnsP = charIDToTypeID( "CnsP" );
  74.     desc4.putBoolean( idCnsP, true );
  75.     var idIntr = charIDToTypeID( "Intr" );
  76.     var idIntp = charIDToTypeID( "Intp" );
  77.     var idNrst = charIDToTypeID( method );
  78.     desc4.putEnumerated( idIntr, idIntp, idNrst );
  79.     executeAction( idImgS, desc4, DialogModes.NO );
  80. }
  81.  
  82. function exportImages(resizeOption, resizeMethod, scaleStyles)
  83. {
  84.     // select a folder to save to
  85.     var folder = Folder.selectDialog();
  86.     if(folder)
  87.     {
  88.         // get currect document
  89.         var doc = app.activeDocument;
  90.  
  91.         // create new document based on the current docs values except name which user
  92.         var dup = app.documents.add(doc.width, doc.height, doc.resolution, doc.activeLayer.name, NewDocumentMode.RGB, DocumentFill.TRANSPARENT);
  93.  
  94.         // switch back to origal doc to allow duplicate
  95.         app.activeDocument = doc;
  96.  
  97.         // duplicate the selected layer (only works for one) at place it in the new doc
  98.         doc.activeLayer.duplicate(dup);
  99.  
  100.         // switch back to the new document
  101.         app.activeDocument = dup;
  102.  
  103.         // trim the document so that all that appears is our element
  104.         dup.trim(TrimType.TRANSPARENT);
  105.  
  106.         // adjust canvas size so that it is an even number of pixels (so scaling down fits on whole pixel)
  107.         dup.resizeCanvas(Math.ceil(dup.width/2)*2, Math.ceil(dup.height/2)*2, AnchorPosition.TOPLEFT);
  108.        
  109.         // normalise name (basic normalisation lower case and hyphenated, modify or remove to taste)
  110.         var normalisedName = dup.name.toLowerCase().replace(' ', '-');
  111.  
  112.         // export retina image
  113.         if((resizeOption & ResizeOptions.RETINA) != 0)
  114.             saveForWebPNG(dup, folder.fullName, normalisedName + '@2x');
  115.  
  116.         if((resizeOption & ResizeOptions.REGULAR) != 0)
  117.         {
  118.             // resize image
  119.             resizeImage(dup.width/2, resizeMethod, scaleStyles);
  120.             //dup.resizeImage(dup.width/2, dup.height/2, dup.resolution, ResampleMethod.BILINEAR);
  121.    
  122.             // export regular image
  123.             saveForWebPNG(dup, folder.fullName, normalisedName);
  124.         }
  125.        
  126.         dup.close(SaveOptions.DONOTSAVECHANGES);
  127.     }
  128. }
  129.  
  130. function okClickedHandler()
  131. {
  132.     var resizeMethod = resizeMethodLookup[exportDialog.methodOptions.selection.text];
  133.     var scaleStyles = exportDialog.scaleStylesCheckBox.value;
  134.     var resizeOption;
  135.    
  136.     if(exportDialog.sizesPanel.retina.value == true)
  137.         resizeOption = ResizeOptions.RETINA;
  138.     else if(exportDialog.sizesPanel.regular.value == true)
  139.         resizeOption = ResizeOptions.REGULAR;
  140.     else if(exportDialog.sizesPanel.both.value == true)
  141.         resizeOption = (ResizeOptions.REGULAR | ResizeOptions.RETINA);
  142.    
  143.     exportImages(resizeOption, resizeMethod, scaleStyles);
  144. }
  145.  
  146. exportDialog = new Window('dialog', 'Export Selected Layer for iOS');
  147. exportDialog.alignChildren = 'left';
  148. exportDialog.sizesPanel = exportDialog.add('panel', undefined, 'Export Sizes');
  149. exportDialog.sizesPanel.alignChildren = 'left';
  150. exportDialog.sizesPanel.both = exportDialog.sizesPanel.add('radiobutton', undefined, 'Regular + Retina');
  151. exportDialog.sizesPanel.regular = exportDialog.sizesPanel.add('radiobutton', undefined, 'Regular Only');
  152. exportDialog.sizesPanel.retina = exportDialog.sizesPanel.add('radiobutton', undefined, 'Retina Only');
  153. exportDialog.sizesPanel.both.value = true;
  154.  
  155. exportDialog.methodOptions = exportDialog.add('dropdownlist', undefined, [ResizeMethod.NEARESTNEIGHBOUR.name, ResizeMethod.BILINEAR.name, ResizeMethod.BICUBIC.name, ResizeMethod.BICUBICSMOOTHER.name, ResizeMethod.BICUBICSHARPER.name]);
  156. exportDialog.methodOptions.children[1].selected = true;
  157. exportDialog.scaleStylesCheckBox = exportDialog.add('checkbox', undefined, 'Scale Styles');
  158. exportDialog.scaleStylesCheckBox.value = true;
  159.  
  160. exportDialog.buttonGroup = exportDialog.add('group');
  161. exportDialog.buttonGroup.cancelButton = exportDialog.buttonGroup.add('button', undefined, 'Cancel');
  162. exportDialog.buttonGroup.okButton = exportDialog.buttonGroup.add('button', undefined, 'OK');
  163. exportDialog.buttonGroup.okButton.addEventListener('click', okClickedHandler);
  164. exportDialog.show();
RAW Paste Data
Top