Created
January 5, 2017 12:22
-
-
Save silviu-bucsa/b5af4dd23fff61a416cfcac0695c9fa6 to your computer and use it in GitHub Desktop.
This file contains 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
/* | |
* | |
* Export All Text Layers | |
* 1.1.4 | |
* Pavel Ivanov | |
* https://github.com/pivanov/photoshop-scripts | |
* ========================================================== | |
* Modified by Silviu Bucsa | |
* | |
*/ | |
var doc = app.activeDocument; | |
var fileLineFeed = ($.os.search(/windows/i) != -1 ? "windows" : "macintosh"); | |
var openFile = true; // Open file when done (true/false) | |
var longDivider = "=========================================================="; | |
var shortDivider = "----------------------------"; | |
var semiColon = ";"; | |
var importantNote = "*** Important: "; | |
var data = []; | |
var count; | |
// Check if String Exist in | |
// Source: http://hayageek.com/javascript-string-contains/ | |
if (!String.prototype.contains) { | |
String.prototype.contains = function (str) { | |
return (this.indexOf(str) !== -1); | |
}; | |
} | |
// Check if String does not Exist | |
// Source: http://hayageek.com/javascript-string-contains/ | |
if (!String.prototype.doesNotContain) { | |
String.prototype.doesNotContain = function (str) { | |
return (this.indexOf(str) == -1); | |
}; | |
} | |
function init() { | |
// Do we have open document? | |
if (app.documents.length === 0) { | |
alert("Please open a file", "TextExport Error", true); | |
return; | |
} | |
ExportText(doc); | |
saveResults(); | |
} | |
function ExportText(el) { | |
// Get the layers | |
var layers = el.layers; | |
// Get number of layers | |
var count = layers.length; | |
for (var i = 0; i < count; i++) { | |
// curentLayer | |
var currentLayer = layers[i]; | |
if (currentLayer.typename == "LayerSet") { | |
ExportText(currentLayer); | |
} else { | |
// is Text layer visible | |
if (currentLayer.visible && currentLayer.kind === LayerKind.TEXT && currentLayer.textItem.contents && currentLayer.name.doesNotContain('#DNE')) { | |
doc.activeLayer = currentLayer; | |
// font-size | |
try { | |
var fontSize = parseInt(currentLayer.textItem.size); | |
} catch (e) {; | |
} | |
// color | |
try { | |
var color = currentLayer.textItem.color.rgb.hexValue; | |
} catch (e) { | |
var color = '000000'; | |
} | |
// font-family | |
try { | |
var fontFamily = currentLayer.textItem.font; | |
} catch (e) {; | |
} | |
// font-style | |
try { | |
var fontStyle = currentLayer.textItem.fauxItalic; | |
} catch (e) { | |
var fontStyle = false; | |
} | |
// font-weight | |
try { | |
var fontWeight = currentLayer.textItem.fauxBold; | |
} catch (e) { | |
var fontWeight = false; | |
} | |
// check for fontFamily.style | |
try { | |
var fontFamilyStyle = app.fonts.getByName(fontFamily).style; | |
} catch (e) {; | |
} | |
// text-transform | |
try { | |
var textTransform = currentLayer.textItem.capitalization; | |
if (textTransform && textTransform != 'TextCase.NORMAL') { | |
var textTransform = true; | |
} | |
} catch (e) { | |
var textTransform = false; | |
} | |
// letter-spacing | |
try { | |
var letterSpacing = currentLayer.textItem.tracking; | |
} catch (e) { | |
var letterSpacing = 0; | |
} | |
if (letterSpacing != 0) { | |
var letterSpacing = ((letterSpacing * fontSize) / 1000); | |
} | |
// line-height | |
try { | |
var lineHeight = parseInt(currentLayer.textItem.leading); | |
} catch (e) { | |
var lineHeight = 0; | |
}; | |
// check for fontExist | |
try { | |
var fontExist = app.fonts.getByName(fontFamily).name; | |
} catch (e) { | |
var fontExist = false; | |
} | |
if (fontExist) { | |
var fontFamily = fontExist; | |
} | |
var opacity = currentLayer.opacity; | |
var fillOpacity = currentLayer.fillOpacity; | |
var effects = activeLayerHasEffects(); | |
var obj = { | |
content: currentLayer.textItem.contents, | |
// fontSize: fontSize / 10 + 'rem', // from px/pt to rem | |
fontSize: fontSize + 'px', | |
color: '#' + color.toLowerCase(), | |
fontFamily: fontFamily, | |
fontExist: fontExist, | |
fontStyle: fontStyle, | |
fontWeight: fontWeight, | |
fontFamilyStyle: fontFamilyStyle, | |
textTransform: textTransform, | |
letterSpacing: letterSpacing, | |
lineHeight: lineHeight, | |
opacity: opacity, | |
fillOpacity: fillOpacity, | |
effects: effects | |
}; | |
data.push(obj); | |
} | |
} | |
} | |
} | |
// get Descriptor | |
function getDescriptor(psClass, psKey) { // integer:Class, integer:key | |
var ref = new ActionReference(); | |
if (psKey != undefined) ref.putProperty(charIDToTypeID("Prpr"), psKey); | |
ref.putEnumerated(psClass, charIDToTypeID("Ordn"), charIDToTypeID("Trgt")); | |
return executeActionGet(ref); | |
}; | |
// Check if layer has Layer Effects applied | |
function activeLayerHasEffects() { | |
var hasEffect = getDescriptor(charIDToTypeID('Lyr ')).hasKey(stringIDToTypeID('layerEffects')); | |
return hasEffect; | |
}; | |
// Save the Results | |
function saveResults() { | |
// Select where to save the txt file | Save dialog | |
// var file = File.saveDialog("Please select a file to save the results"); | |
// if file is not selected | |
// if (file == null) { | |
// return; | |
// } | |
// Get the filePath | |
// var filePath = file.path + "/" + file.name; | |
// Save a txt file on the Desktop | |
var filePath = Folder.desktop + '/Text Layer CSS for - ' + doc.name + '.txt'; | |
// Create the outputFile | |
var outputFile = new File(filePath); | |
// Set linefeed | |
outputFile.linefeed = fileLineFeed; | |
// Open for write | |
outputFile.open("w", "TEXT", "????"); | |
// Append title of the PSD to top of the file | |
outputFile.writeln(longDivider); | |
outputFile.writeln('All visible Text Layers from "' + doc.name + '"'); | |
outputFile.writeln(longDivider); | |
// Write layer properties to file | |
for (var i = 0; i < data.length; i++) { | |
var object = data[i]; | |
outputFile.writeln(shortDivider); | |
outputFile.writeln('Layer Content:'); | |
outputFile.writeln(shortDivider); | |
outputFile.writeln(object.content + '\n'); | |
outputFile.writeln('CSS properties:'); | |
outputFile.writeln(shortDivider); | |
outputFile.writeln('font-size: ' + object.fontSize + semiColon); | |
outputFile.writeln('color: ' + object.color + semiColon); | |
outputFile.writeln('font-family: ' + object.fontFamily + semiColon); | |
if (object.fontStyle) { | |
outputFile.writeln('font-style: italic' + semiColon + ' /* fauxItalic */'); | |
} | |
if (object.fontWeight) { | |
outputFile.writeln('font-weight: 700' + semiColon + ' /* fauxBold */'); | |
} | |
if (object.textTransform) { | |
outputFile.writeln('text-transform: uppercase' + semiColon); | |
} | |
if (object.letterSpacing != 0) { | |
outputFile.writeln('letter-spacing: ' + object.letterSpacing + 'px' + semiColon); | |
} | |
if (object.lineHeight != 0) { | |
outputFile.writeln('line-height: ' + object.lineHeight + 'px' + semiColon); | |
} | |
outputFile.writeln(''); | |
outputFile.writeln('Notes:'); | |
outputFile.writeln(shortDivider); | |
if (object.fontExist) { | |
outputFile.writeln(importantNote + 'Font Family Style is "' + object.fontFamilyStyle + '"'); | |
} else { | |
outputFile.writeln(importantNote + 'The following font is missing: ' + object.fontFamily); | |
} | |
if (object.effects) { | |
outputFile.writeln(importantNote + 'This layer has Layer Effect applied.\nCheck for: Drop Shadow, Outer Glow, Color or Gradient Overlay, etc.'); | |
} | |
if (object.opacity != '100' || object.fillOpacity != '100') { | |
outputFile.writeln(importantNote + 'This layer has ' + '"Opacity: ' + Math.round(object.opacity) + '%" and "' + 'Fill: ' + object.fillOpacity + '%"'); | |
} | |
outputFile.writeln(''); | |
outputFile.writeln(longDivider); | |
} | |
// Close the file | |
outputFile.close(); | |
// Open the file from or Notify that we're done | |
if (openFile === true) { | |
outputFile.execute(); | |
} else { | |
alert("File was saved to:\n" + Folder.decode(filePath), "TextExport"); | |
} | |
} | |
init(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment