// Before you run this, change the dropdown on the upper left from: // ExtendScript Toolkit // to // Adobe Photoshop CS2 // (or whichever version of PS you have) function Main() { // Get a link to the active document (whatever photoshop file is open) var template = app.activeDocument; // Store the ruler units and set them to pixels var defaultRulerUnits = app.preferences.rulerUnits; app.preferences.rulerUnits = Units.PIXELS; // Put a "savepoint" in the history so we can restore it after mucking with the file in this script var startingHistoryLocation = template.activeHistoryState; // Here's a bunch of stuff to get information about the photoshop file that is currently open and active var layerCount = template.layers.length; var layerSetsCount = template.layerSets.length; var width = app.activeDocument.width; var height = app.activeDocument.height; // Set a bunch of paths we will use later var stuffPath = "/C/Users/Mark/Documents/Projects/Bastion/BastionPlaytest8/"; var dbText = new File(stuffPath + "bastion_spoiler.txt"); var outputFolderPath = "/output/large/"; // Open the text file (which is a list of cards/etc, tab delimited) dbText.open ('r'); var layerCount = template.layers.length; var layerSetsCount = template.layerSets.length; // Fill out the array of lines read from the file var lineArray = new Array(); var line = 0; var tempLine = ""; while( tempLine = dbText.readln() ) { lineArray[line] = tempLine; line++; } var numLines = lineArray.length; for(card = 0; card < numLines; ++card) { // The text file is tab-delimited, so split the line into columns var lineParts = lineArray[card].split('\t'); // Throw out any lines with less than 3 items in them, since that means they are comments or broken or something if( lineParts.length < 3 ) { continue; } // Put the columns into named variables for convenience var cardName = lineParts[0]; var cardType = lineParts[1]; var cardSubType = lineParts[2]; var cost = lineParts[3]; var health = lineParts[4]; var outputName = lineParts[6]; // .jpg // Bastion-specific stuff var textBox = lineParts[5].split('@'); var rulesText = textBox[0]; var hasUnitText = textBox.length > 1; var unitText = ""; if( hasUnitText ) { unitText = textBox[1]; } var isStructure = cardType == "Building"; var isInfantry = cardSubType == "Infantry"; var isOfficer = cardSubType == "Officer"; var isArtillery = cardSubType == "Artillery"; var isSupport = cardSubType == "Support"; var isCavalry = cardSubType == "Cavalry"; // Title and Type var maxWidth = 418; var titleLayerRef = template.layerSets[1].layers[0]; // This is the layer the title is on var titleRef = titleLayerRef.textItem; // This is the text item on that layer titleRef.horizontalScale = 100; // Reset scale to 100% titleRef.contents = cardName; // Fill out the title var actualWidth = titleLayerRef.bounds[2] - titleLayerRef.bounds[0]; // Check the actual width if( actualWidth > maxWidth ) { // Rescale the title since the text is too wide var newScale = 100 * maxWidth / actualWidth; titleRef.horizontalScale = newScale; } // Print subtype on side of card template.layerSets[1].layers[1].textItem.contents = cardSubType; // Resource and number icons template.layerSets[2].layerSets[0].layers[0].textItem.contents = cost; template.layerSets[2].layerSets[1].layers[0].textItem.contents = health; // Card Type template.layerSets[3].layerSets[0].visible = isInfantry; template.layerSets[3].layerSets[1].visible = isStructure; template.layerSets[3].layerSets[2].visible = isArtillery; template.layerSets[3].layerSets[3].visible = isOfficer; template.layerSets[3].layerSets[4].visible = isSupport; template.layerSets[3].layerSets[5].visible = isCavalry; // Turn |'s into endlines. rulesText = rulesText.replace("|", "\u000D"); rulesText = rulesText.replace("|", "\u000D"); rulesText = rulesText.replace("|", "\u000D"); // Text Box template.layerSets[1].layers[2].textItem.contents = rulesText; template.layerSets[1].layers[3].textItem.contents = unitText; template.layerSets[1].layers[3].visible = hasUnitText; // Unit Text layer template.layerSets[1].layers[4].visible = hasUnitText; // Horizontal Rule layer // Card Art if( template.layerSets[4].layers.length > card ) { template.layerSets[4].layers[card].visible = true; } // Save it out var uniqueFileName = stuffPath + outputFolderPath + outputName + ".jpg"; SaveAsJPEG( uniqueFileName ); // Rewind to the history "savepoint" that was taken earlier template.activeHistoryState = startingHistoryLocation; } // Set the ruler units back so your document isn't messed up app.preferences.rulerUnits = defaultRulerUnits; alert("Done!"); } // Save the current thing into the file name that was passed in function SaveAsJPEG( inFileName ) { var saveFile = new File(inFileName); var saveOptions = new JPEGSaveOptions(); saveOptions.embedColorProfile = true; saveOptions.formatOptions = FormatOptions.STANDARDBASELINE; saveOptions.matte = MatteType.NONE; saveOptions.quality = 12; // This is the max quality app.activeDocument.saveAs(saveFile, saveOptions, true, Extension.LOWERCASE); // Do the save } Main();