/* Create HTML Code: Ilias Ismanalijev Arlo Rose */ var isCS2 = (app.version.split(".")[0] > 8) ? true : false; var nonNormalAlert = false; function makeDialog(htmlInfo) { var docRef = activeDocument; var dialogW = 390; // dialog width var dialogH = 175; // dialog height var buttonW = 80; // button width var buttonH = (isCS2) ? 22 : 20; // button height var textW = 270; // text width var textH = 22; // text height var margin = 10; // margin var bounds = {x:0, y:0, width:dialogW, height:dialogH}; var yLayout = margin; var myWindow = new Window("dialog", "Please Choose Your Options", bounds); bounds = {x:(dialogW - buttonW - margin), y:yLayout, width:buttonW, height:buttonH}; myWindow.buttonRun = myWindow.add("button", bounds, "Create"); myWindow.defaultElement = myWindow.buttonRun; myWindow.buttonRun.onClick = buttonRunOnClick; yLayout += buttonH + margin; bounds = {x:(dialogW - buttonW - margin), y:yLayout, width:buttonW, height:buttonH}; myWindow.buttonCancel = myWindow.add("button", bounds, "Cancel"); myWindow.buttonCancel.onClick = function() { this.parent.close(0); }; yLayout = margin; bounds = {x:margin, y:yLayout, width:180, height:textH}; myWindow.add("statictext", bounds, "Title:"); yLayout += textH; bounds = {x:margin, y:yLayout, width:textW, height:textH}; myWindow.htmlName = myWindow.add("edittext", bounds, htmlInfo.thehtmlName); yLayout += textH + margin; bounds = {x:margin, y:yLayout, width:180, height:textH}; myWindow.add("statictext", bounds, "Destination:"); yLayout += textH; bounds = {x:margin, y:yLayout, width:textW-buttonW-margin-5, height:textH}; // 5px smaller myWindow.fileDestination = myWindow.add("edittext", bounds, htmlInfo.theSavePath); bounds = {x:margin+textW-buttonW-5, y:yLayout, width:buttonW+5, height:buttonH}; // 5px wider myWindow.buttonBrowse = myWindow.add("button", bounds, "Browse..."); myWindow.buttonBrowse.onClick = buttonBrowseOnClick; yLayout += textH + margin; bounds = {x:margin, y:yLayout, width:textW, height:textH}; myWindow.checkboxHiddenLayers = myWindow.add( "checkbox", bounds, "Build Page With Hidden Layers"); myWindow.checkboxHiddenLayers.value = htmlInfo.addHiddenLayers; yLayout += textH + margin; bounds = {x:margin, y:yLayout, width:textW, height:textH}; myWindow.checkboxRast = myWindow.add( "checkbox", bounds, "Never rasterize text"); myWindow.checkboxRast.value = htmlInfo.rasterize; myWindow.center(); var result = myWindow.show(); if ( 0 == result ) return result; htmlInfo.thehtmlName = myWindow.htmlName.text; htmlInfo.theSavePath = myWindow.fileDestination.text; htmlInfo.rasterize = myWindow.checkboxRast.value htmlInfo.addHiddenLayers = myWindow.checkboxHiddenLayers.value var instructionData = (htmlInfo.addHiddenLayers) ? instructionData + "1" : instructionData + ""; app.activeDocument.info.instructions = instructionData; app.activeDocument.info.title = htmlInfo.thehtmlName; return result; } function buttonBrowseOnClick() { var defaultFolder = this.parent.fileDestination.text; var testFolder = new Folder(this.parent.fileDestination.text); if (!testFolder.exists) defaultFolder = ""; var selFolder = Folder.selectDialog("Please choose a destination folder:", defaultFolder); if ( selFolder != null ) { this.parent.fileDestination.text = selFolder.toString(); } return; } function buttonRunOnClick() { this.parent.close(1); } function writehtmlHeader(myFile, docRefName, htmlInfo) { var docRef = activeDocument; myFile.writeln( '' ); myFile.writeln( '' ); myFile.writeln( '' ); myFile.writeln( '' + htmlInfo.thehtmlName + '' ); myFile.writeln( '' ); } function fixValue(theValue) { theValue = String(theValue).replace(/ px| pt/, ''); return theValue; } function camelCase(theValue) { theValue = theValue.toLowerCase().replace(/[^\w ]/g, ""); var theNewValue = ""; var capTheChar = false; for (var i = 0; i < theValue.length; i++) { var c = theValue.charAt(i); if (c == " " && theNewValue.length > 0) capTheChar = true; else if (c != " ") { if (capTheChar) { theNewValue += c.toUpperCase(); capTheChar = false; } else theNewValue += c; } } return theNewValue; } function writeLayerDefinitions(myFile, resource_dir, htmlInfo) { var origVisibilities = []; var docRef = activeDocument; var originalDocumentState = docRef.activeHistoryState; numLayers = docRef.artLayers.length; do { numLayers = docRef.artLayers.length; mustRestart = false; for (var i = 0; i < numLayers; i++) { if ( docRef.layers[i].visible == false ) { docRef.layers[i].remove(); mustRestart = true; break; } } } while (mustRestart); do { numLayers = docRef.artLayers.length; mustRestart = false; for (var i = numLayers-1; i >= 0; i--) { if (docRef.artLayers[i].grouped) { docRef.artLayers[i].merge(); mustRestart = true; break; } } } while (mustRestart); numLayers = docRef.layers.length; for (var i = 0; i < numLayers; i++) { origVisibilities[i] = docRef.layers[i].visible; docRef.layers[i].visible = false; } for (var i = (numLayers-1); i >= 0; i--) { if (docRef.layers[i].blendMode != "BlendMode.NORMAL") { if (!nonNormalAlert) alert ( "This file contains layers that use a blend mode other than \"Normal\".\nBe aware that as a html, the layers that don't use \"Normal\" may not blend as you'd expect." ); nonNormalAlert = true; } if ( origVisibilities[i] == true || htmlInfo.addHiddenLayers == true) { var rasterizeText = !htmlInfo.rasterize; if (!htmlInfo.rasterize) { if (docRef.layers[i].kind == LayerKind.TEXT) { rasterizeText = confirm('Make text layer "'+ docRef.layers[i].name +'" into an image?'); } } if (!rasterizeText && (docRef.layers[i].kind == LayerKind.TEXT)) { var textStyleString = ' style = "'; var pos = docRef.layers[i].textItem.position; var newVOffset = Math.round(fixValue(pos[1])); if (docRef.layers[i].textItem.kind == TextType.PARAGRAPHTEXT) newVOffset -= 3; myFile.writeln( ' '); myFile.writeln(docRef.layers[i].textItem.contents); myFile.writeln('

'); } else { docRef.layers[i].visible = true; theLayerName = docRef.layers[i].name; beforeWidth = docRef.width; beforeHeight = docRef.height; savedState = docRef.activeHistoryState; if (theLayerName != "Background") { docRef.trim(TrimType.TRANSPARENT, true, true, false, false); } hOffset = beforeWidth - (docRef.width * 1); vOffset = beforeHeight - (docRef.height * 1); if (theLayerName != "Background") { docRef.trim(TrimType.TRANSPARENT, false, false, true, true); } var theNewLayerName = camelCase(theLayerName); var imgStyleString = ' style = "'; myFile.writeln( ' '); docRef.layers[i].opacity = 100; pngFile = new File(resource_dir +'/'+ theNewLayerName +'.png'); //pngFile = new File(htmlInfo.theSavePath +'/' + htmlInfo.thehtmlName + '.html/img/'+ theNewLayerName +'.png'); saveForWebPNG(pngFile); docRef.activeHistoryState = savedState; delete pngFile; delete pngSaveOptions; delete savedState; docRef.layers[i].visible = false; } myFile.writeln(''); } } docRef.activeHistoryState = originalDocumentState; for (i = 0; i < numLayers; i++) { docRef.layers[i].visible = origVisibilities[i]; } } function writeWindowDefinition(myFile, docRefName, htmlInfo) { var docRef = activeDocument; myFile.writeln(' '); myFile.writeln(''); } function writehtmlFooter(myFile) { myFile.writeln(' '); myFile.writeln(''); myFile.writeln(''); } function writehtmlLayout(myFile, resource_dir, docRefName, htmlInfo) { writeWindowDefinition(myFile, docRefName, htmlInfo); writeLayerDefinitions(myFile, resource_dir, htmlInfo); } function createhtml(htmlInfo) { var safeDocRef = activeDocument; var docRef = safeDocRef.duplicate(); var docRefName = safeDocRef.name; docRefName = docRefName.replace('.psd', ''); var origUnit = preferences.rulerUnits; preferences.rulerUnits = Units.PIXELS; var resource_dir = htmlInfo.theSavePath +'/img'; resDir = new Folder(resource_dir); resDir.create(); var origName = htmlInfo.theSavePath +'/'+ docRefName +'.html'; var backName = htmlInfo.theSavePath +'/'+ docRefName +' backup.txt'; origFile = new File(origName); if (origFile.open('r', 'TEXT','K0nF')) { backFile = new File(backName); backFile.open('w', 'TEXT','R*ch'); while (!origFile.eof) { line = origFile.readln(); backFile.writeln(line); } origFile.close(); backFile.close(); } var filename = htmlInfo.theSavePath +'/'+ docRefName +'.html'; myFile = new File(filename); myFile.open('w', 'TEXT','K0nF'); writehtmlHeader(myFile, docRefName, htmlInfo); writehtmlLayout(myFile, resource_dir, docRefName, htmlInfo); writehtmlFooter(myFile); myFile.close(); docRef.trim(TrimType.TRANSPARENT, true, true, true, true); preferences.rulerUnits = origUnit; docRef.close(SaveOptions.DONOTSAVECHANGES); } function setUpDialogInfo(htmlInfo) { if (app.activeDocument.info.instructions == "") { var instructionData = "1.0,4.0,,1,,"; app.activeDocument.info.instructions = instructionData; } else { var instructionData = app.activeDocument.info.instructions; } instructionData = instructionData.split(","); htmlInfo.addHiddenLayers = instructionData[0]; if (app.activeDocument.info.title) { htmlInfo.thehtmlName = app.activeDocument.info.title; } else { htmlInfo.thehtmlName = app.activeDocument.name.replace('.psd', ''); } try { htmlInfo.theSavePath = String(app.activeDocument.path); } catch(someError) { if (isMac) { htmlInfo.theSavePath = "~/Desktop"; } else { htmlInfo.theSavePath = ""; } } } function saveForWebPNG( filePath ) { var id48 = charIDToTypeID( "Expr" ); var desc7 = new ActionDescriptor(); var id49 = charIDToTypeID( "Usng" ); var desc8 = new ActionDescriptor(); var id50 = charIDToTypeID( "Op " ); var id51 = charIDToTypeID( "SWOp" ); var id52 = charIDToTypeID( "OpSa" ); desc8.putEnumerated( id50, id51, id52 ); var id53 = charIDToTypeID( "Fmt " ); var id54 = charIDToTypeID( "IRFm" ); var id55 = charIDToTypeID( "PN24" ); desc8.putEnumerated( id53, id54, id55 ); var id56 = charIDToTypeID( "Intr" ); desc8.putBoolean( id56, false ); var id57 = charIDToTypeID( "Trns" ); desc8.putBoolean( id57, true ); var id58 = charIDToTypeID( "Mtt " ); desc8.putBoolean( id58, true ); var id59 = charIDToTypeID( "MttR" ); desc8.putInteger( id59, 255 ); var id60 = charIDToTypeID( "MttG" ); desc8.putInteger( id60, 255 ); var id61 = charIDToTypeID( "MttB" ); desc8.putInteger( id61, 255 ); var id62 = charIDToTypeID( "SHTM" ); desc8.putBoolean( id62, false ); var id63 = charIDToTypeID( "SImg" ); desc8.putBoolean( id63, true ); var id64 = charIDToTypeID( "SSSO" ); desc8.putBoolean( id64, false ); var id65 = charIDToTypeID( "SSLt" ); var list1 = new ActionList(); desc8.putList( id65, list1 ); var id66 = charIDToTypeID( "DIDr" ); desc8.putBoolean( id66, false ); var id67 = charIDToTypeID( "In " ); desc8.putPath( id67, filePath ); var id68 = stringIDToTypeID( "SaveForWeb" ); desc7.putObject( id49, id68, desc8 ); executeAction( id48, desc7, DialogModes.NO ); } function main() { if ( app.documents.length <= 0 ) { alert( "No Open Documents\nYou must have a document open to create a HTML from." ); return; } var htmlInfo = new Object(); setUpDialogInfo(htmlInfo); if (0 == makeDialog(htmlInfo)) return; var htmlName = htmlInfo.thehtmlName; if (htmlName.match(/[\!|\?]/) && !isMac) { alert ( "Bad File Name\nThe file name you're using contains characters that will not work on browsers." ); main(); return; } var destination = htmlInfo.theSavePath; if (destination.length == 0) { alert("Please Specify Destination\nIn order to save your page, you need to specify a destination folder for it."); main(); return; } createhtml(htmlInfo); alert("Creation Is Complete\nA HTML page has been created from your document and is ready to go!"); } main();