Skip to content

Instantly share code, notes, and snippets.

@stefan-vatov
Created April 11, 2013 00:32
Show Gist options
  • Save stefan-vatov/5359707 to your computer and use it in GitHub Desktop.
Save stefan-vatov/5359707 to your computer and use it in GitHub Desktop.
Get all the fonts used in text layers in all open documents and save them to a target txt file, one at a line.
/**
*
*
* Modified version of text export script (https://github.com/omgmog/photoshop-text-export) 1.3
* Used to export the used fonts in all open photoshop documents
*
**/
// Use save as dialog (true/false)? - This will be overriden to false when running TextExport on multiple files!
var useDialog = true;
// Open file when done (true/false)? - This will be overriden to true when running TextExport on multiple files!
var openFile = true;
// text separator
var separator = "*************************************";
var used_fonts = new Array();
/**
* NO NEED TO CHANGE ANYTHING BENEATH THIS LINE
* -------------------------------------------------------------
*/
/**
* TextExport Init function
* -------------------------------------------------------------
*/
function initTextExport() {
// Linefeed shizzle
if ($.os.search(/windows/i) != -1)
fileLineFeed = "windows";
else
fileLineFeed = "macintosh";
// Do we have a document open?
if (app.documents.length < 1) {
alert("Please open at least one file", "TextExport Error", true);
return;
}
// Oh, we have more than one document open!
if (app.documents.length > 1) {
var runMultiple = confirm("TextExport has detected Multiple Files.\nDo you wish to run TextExport on all opened files?", true, "TextExport");
if (runMultiple === true) {
docs = app.documents;
} else {
docs = [app.activeDocument];
}
} else {
runMultiple = false;
docs = [app.activeDocument];
}
// Pop up save dialog
var saveFile = File.saveDialog("Please select a file to export the text to:");
// User Cancelled
if(saveFile == null)
{
alert("User Cancelled");
return;
}
// set filePath and fileName to the one chosen in the dialog
filePath = saveFile.path + "/" + saveFile.name;
var fileOut = File(filePath);
// clear dummyFile
dummyFile = null;
// set linefeed
fileOut.linefeed = fileLineFeed;
// open for write
fileOut.open("w", "TEXT", "????");
// Loop all documents
for (var i = 0; i < docs.length; i++)
{
// create outfile
// Append title of document to file
// Set active document
app.activeDocument = docs[i];
// call to the core with the current document
goTextExport2(app.activeDocument, fileOut, '/');
// Hammertime!
// close the file
// Give notice that we're done or open the file (only when running 1 file!)
if (runMultiple === false)
{
if (openFile === true)
fileOut.execute();
else
alert("File was saved to:\n" + Folder.decode(filePath), "TextExport");
}
}
fileOut.close();
if (runMultiple === true)
{
alert("Parsed " + documents.length + " files;\nFiles were saved in your documents folder", "TextExport");
}
}
/**
* TextExport Core Function (V2)
* -------------------------------------------------------------
*/
function goTextExport2(el, fileOut, path)
{
// Get the layers
var layers = el.layers;
// Loop 'm
for (var layerIndex = layers.length; layerIndex > 0; layerIndex--)
{
// curentLayer ref
var currentLayer = layers[layerIndex-1];
// currentLayer is a LayerSet
if (currentLayer.typename == "LayerSet") {
goTextExport2(currentLayer, fileOut, path + currentLayer.name + '/');
// currentLayer is not a LayerSet
} else {
// Layer is visible and Text --> we can haz copy paste!
if ( (currentLayer.visible) && (currentLayer.kind == LayerKind.TEXT) )
{
if (ProcessFont(currentLayer.textItem.font)) {
fileOut.writeln(currentLayer.textItem.font);
};
}
}
}
}
function ProcessFont(font)
{
for (var i = used_fonts.length - 1; i >= 0; i--) {
if (used_fonts[i] == font)
{
return false;
}
};
used_fonts.push(font);
return true;
}
/**
* TextExport Boot her up
* -------------------------------------------------------------
*/
initTextExport();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment