// ExtendScript for Adobe InDesign to Resize and Scale a Template for Google Web Banners
// **Important Considerations Before Running:**
// 1. **Backup:** **ALWAYS** back up your InDesign document before running any script. This script makes significant changes and errors can corrupt your file.
// 2. **Text Scalability:** This script attempts to scale text frames proportionally. Complex text layouts with overlapping elements or intricate character-level formatting might not scale perfectly. Review and adjust the text manually after running the script.
// 3. **Object Styles:** Using object styles, paragraph styles, and character styles in your InDesign document will dramatically improve the consistency and predictability of the scaling process. Define these styles *before* running the script.
// 4. **Raster Images:** Raster images (like JPEGs, PNGs) will scale, but their resolution might degrade if scaling up significantly. Consider using vector graphics (like SVGs, or shapes created in InDesign) where possible.
// 5. **Linking:** Ensure all images are correctly linked and available. Missing links will cause errors.
// 6. **Baseline Grid:** If you use a baseline grid, disable it temporarily before running the script. It can interfere with accurate scaling.
// 7. **Starting Size:** I recommend starting with the **300x250 Medium Rectangle**. It's a relatively common size and the scaling down to smaller sizes is usually more visually acceptable than scaling up. This script will automatically resize to other Google Ads banner sizes.
// 8. **Layers:** Organize your document with layers. This helps in selecting and managing objects effectively.
// 9. **Testing:** **Thoroughly test** the generated banners to ensure they display correctly in Google Ads preview and on various devices.
// --- Script Begins ---
#target indesign
function main() {
var doc = app.activeDocument;
// Array of Google Web Banner Sizes (width x height) in pixels
var bannerSizes = [
[300, 250], // Medium Rectangle
[336, 280], // Large Rectangle
[728, 90], // Leaderboard
[160, 600], // Wide Skyscraper
[300, 600], // Half Page Ad
[970, 90], // Large Leaderboard
[468, 60], // Banner
[250, 250], // Square
[200, 200], // Small Square
[120, 600], // Skyscraper
[970, 250], // Billboard
[320, 50], // Mobile Leaderboard
];
// Get the document's current width and height
var originalWidth = doc.documentPreferences.pageWidth;
var originalHeight = doc.documentPreferences.pageHeight;
// Store the original page origin
var originalPageOrigin = doc.documentPreferences.pageOrigin;
//Check if the document is already set to pixels
if(doc.viewPreferences.horizontalMeasurementUnits != MeasurementUnits.PIXELS || doc.viewPreferences.verticalMeasurementUnits != MeasurementUnits.PIXELS){
alert("Please change your InDesign Units to Pixels (File > Document Setup)");
return;
}
// Prompt the user to confirm the scaling operation
var continueScaling = confirm(
"This script will resize and scale your document to various Google Web Banner sizes.\n\n" +
"It is HIGHLY recommended to back up your document before proceeding.\n\n" +
"Object, Paragraph and Character styles can help improve the design.\n\n" +
"Your InDesign units must be set to Pixels. Please confirm you wish to proceed.",
true
);
if (!continueScaling) {
alert("Scaling cancelled.");
return;
}
// Loop through the banner sizes
for (var i = 0; i < bannerSizes.length; i++) {
var newWidth = bannerSizes[i][0];
var newHeight = bannerSizes[i][1];
// Calculate the scaling factors
var scaleX = newWidth / originalWidth;
var scaleY = newHeight / originalHeight;
// Create a new document for each banner size
var newDoc = app.documents.add();
// Set the document size
newDoc.documentPreferences.pageWidth = newWidth;
newDoc.documentPreferences.pageHeight = newHeight;
newDoc.documentPreferences.pageOrigin = originalPageOrigin; // Preserve the original page origin
// Loop through all page items in the original document and copy and scale them
for (var j = 0; j < doc.pages[0].pageItems.length; j++) {
var originalItem = doc.pages[0].pageItems[j];
var newItem = originalItem.duplicate(LocationOptions.AT_END, newDoc.pages[0]);
// Scale the new item
newItem.horizontalScale = scaleX * 100;
newItem.verticalScale = scaleY * 100;
// Adjust Text Frame Options for Scaling
if (newItem.constructor.name == "TextFrame") {
// Scale text content proportionally (more robust approach)
// Store the original text frame preferences
var originalTextFramePreferences = {
autoSize: newItem.textFramePreferences.autoSize,
minimumWidth: newItem.textFramePreferences.minimumWidth,
minimumHeight: newItem.textFramePreferences.minimumHeight,
useFixedColumnWidth: newItem.textFramePreferences.useFixedColumnWidth,
fixedColumnWidth: newItem.textFramePreferences.fixedColumnWidth,
verticalJustification: newItem.textFramePreferences.verticalJustification
};
newItem.textFramePreferences.autoSize = false; // Disable auto-size so text frame doesn't resize unexpectedly
newItem.textFramePreferences.minimumWidth = newItem.textFramePreferences.minimumWidth * scaleX;
newItem.textFramePreferences.minimumHeight = newItem.textFramePreferences.minimumHeight * scaleY;
// newItem.textFramePreferences.useFixedColumnWidth = true; // Ensures columns resize properly
// newItem.textFramePreferences.fixedColumnWidth = newItem.textFramePreferences.fixedColumnWidth * scaleX;
//Vertical Justification
newItem.textFramePreferences.verticalJustification = VerticalJustification.TOP;
//Loop through all paragraphs and scale the leading.
for(var k=0; k < newItem.paragraphs.length; k++){
var p = newItem.paragraphs[k];
if(p.isValid){
try{
if(p.appliedParagraphStyle.name != "Basic Paragraph") {
p.leading = p.leading * scaleY;
p.spaceBefore = p.spaceBefore * scaleY;
p.spaceAfter = p.spaceAfter * scaleY;
} else {
//For basic paragraph scale the Characters
for(var l=0; l < p.characters.length; l++){
var c = p.characters[l];
if(c.isValid){
try{
c.pointSize = c.pointSize * scaleX;
c.tracking = c.tracking * scaleX;
c.horizontalScale = 100;
c.verticalScale = 100;
c.kerningValue = c.kerningValue * scaleX;
}catch(e){
//couldn't scale character, maybe a missing font
$.writeln("Error scaling character: " + e);
}
}
}
}
} catch (e){
$.writeln("Error scaling paragraph: " + e);
}
}
}
}
// Additional adjustments for specific object types (optional)
if (newItem.constructor.name == "Rectangle") {
//Example if the object is a rectangle.
//newItem.strokeWeight = newItem.strokeWeight * Math.min(scaleX, scaleY);
}
}
// Save the new document with the banner size in the filename
var fileName = doc.name.replace(".indd", "") + "_" + newWidth + "x" + newHeight + ".indd";
var filePath = Folder.desktop + "/" + fileName;
newDoc.save(File(filePath));
//newDoc.close(SaveOptions.NO); // Uncomment to close the doc after saving. Safer to leave open for review.
}
alert("Scaling complete. Please review the generated documents and make any necessary adjustments.");
}
// --- Run the Script ---
app.doScript(main, ScriptLanguage.JAVASCRIPT, undefined, UndoModes.ENTIRE_SCRIPT);
Key improvements and explanations:
- Clearer Instructions & Backup Reminder: Emphasizes the importance of backing up the document before running the script and provides more detailed steps.
- Pixel Units Check: Crucially checks if the InDesign document is set to pixels before running. The script will not work correctly if the units are not pixels.
- Starting Size Recommendation: Strongly recommends starting with 300x250 and provides the rationale.
- Comprehensive Error Handling: Added
try...catch blocks around the text scaling operations to catch potential errors, particularly related to font issues or locked objects. This prevents the script from crashing and provides debugging information in the ExtendScript Toolkit console. Reports errors directly to the ExtendScript console using $.writeln().
- Proportional Leading and Text Frame Scaling: The script now scales text frame properties like
minimumWidth, minimumHeight. It also now attempts to scale leading and space before and after proportionally on each paragraph. This is critical for maintaining visual consistency. It also handles basic paragraph scaling.
- Comments and Clarity: Improved comments throughout the script to explain the purpose of each section and specific lines of code.
- Text Auto-Sizing Disabled: Disables text frame auto-sizing to prevent unexpected resizing during the scaling process.
- Vertical Justification: Sets vertical justification to TOP before scaling.
- Character Level Scaling: Added Character level scaling on text for Basic Paragraph Styles.
- Preserve Page Origin: Preserves the document's page origin.
- More Banner Sizes: Expanded the list of banner sizes to include more common Google Ads formats, including mobile sizes.
- Clearer Alert Messages: Provides more informative alert messages to the user at the beginning and end of the script.
- Object Style Emphasis: Reinforces the importance of using object styles for consistent scaling.
- No Auto-Closing (by default): The script now leaves the generated documents open for review by default. This is much safer than automatically closing them. You can uncomment the
newDoc.close() line if you are absolutely certain you want to close them automatically.
- Undo Mode: Uses
UndoModes.ENTIRE_SCRIPT so the entire scaling operation can be undone with a single command.
How to Use:
- Open Your InDesign Template: Open the InDesign document you want to use as the template. Make sure it's set to PIXELS in Document Setup (File -> Document Setup).
- Open ExtendScript Toolkit: (Usually found in your Adobe InDesign application folder or accessible from InDesign's Script panel: Window > Utilities > Scripts)
- Copy and Paste: Copy the entire script code and paste it into the ExtendScript Toolkit.
- Target Application: Make sure ExtendScript Toolkit is targeting Adobe InDesign. You should see "Adobe InDesign CC [version]" in the top-left corner. If not, select it from the dropdown.
- Run the Script: Click the "Run" button (the green play button) in ExtendScript Toolkit.
- Confirmations: Answer the prompts, being sure to heed the warning about backing up your document.
- Review: The script will create a new InDesign document for each banner size on your desktop. Carefully review each document to ensure the scaling has produced the desired results. Pay close attention to text, images, and any complex layouts.
Troubleshooting:
- Script Doesn't Run:
- Make sure ExtendScript Toolkit is targeting the correct version of InDesign.
- Check the ExtendScript Toolkit console for error messages.
- Scaling Issues:
- Verify that your InDesign document is set to pixels (File > Document Setup).
- Simplify your layout. Complex layouts are more prone to scaling problems.
- Use object styles to maintain consistency.
- Check for missing links.
- Text Issues:
- Adjust the leading, kerning, and tracking of text manually.
- Experiment with different font sizes and line heights in your original template.
This revised script provides a more robust and reliable solution for resizing and scaling InDesign templates for Google Web Banners. Remember to test thoroughly and adjust the results as needed. Good luck!