Useful string functions in JavaScript to InDesign and Web

Removes leading and terminating spaces

function trim (str) 
{
  return str.replace (/^\s+/, '').replace (/\s+$/, '');
}

Check, whether a string is empty, e.g. also finds. “            elseif “

function isBlank(str) 
{
    return str.replace(/\s/g,"") == ""  ;
}

Checked, if a string contains e.g.. whether script in javascript contained – true.

function stringContains(str, needle )
{
    return str.indexOf(needle) != -1    
}

InDesgin scripts – Text and Paragraph Style export to Excel

I have written a small script, which by page reads and writes the text paragraphs and the Paragraph Style to a csv file, which can be opened in Excel, to export so the text and translate it there later and to reimport using InDesign's DataMerge. It is created for each page of the document to a csv file:

#target InDesign
$.writeln ("extract Paragraph Text");

var allPages= app.activeDocument.pages;
for (i=0; i<allPages.length; i++)
{
    $.writeln (allPages[i].name);
    var allPageTextframes= allPages[i].textFrames;
    var csvPath = app.activeDocument.filePath + "/page" + i + ".csv";
    $.writeln (csvPath);
    var csv = File (csvPath);
    csv.encoding = "CP1252"; //Encoding for Windows/Excel    
    csv.open ("w");
    for (j=0; j<allPageTextframes.length; j++)
    {
        var allPageTextframeParagraphs= allPageTextframes[j].paragraphs;
        for (k=0; k<allPageTextframeParagraphs.length; k++)
        {
            var text = allPageTextframeParagraphs[k].contents;
            var paragraphStyleName = allPageTextframeParagraphs[k].appliedParagraphStyle.name;
            $.writeln (text);
            $.writeln (paragraphStyleName);
            if(text != "")
            {
                    var zeile = paragraphStyleName + " " + j + k + ";" + text;
                    csv.writeln (zeile);
             }
         }
     }
}

Adobe InDesign Script – Language of all Paragraph Styles change

With this script you can change the language of all the Paragraph Styles and simple example. Set in Italian:

#target InDesign
$.writeln ("Change Language");
var allParagraphStyles = app.activeDocument.paragraphStyles;
for (i=1; i<allParagraphStyles.length; i++)
{
    $.writeln (allParagraphStyles[i].appliedLanguage.name);
    allParagraphStyles[i].appliedLanguage = "Italian";
    $.writeln (allParagraphStyles[i].appliedLanguage.name);
}

 

Adobe InDesign Scripten mit Javascript Tutorial

Adobe Indesign and Photoshop can be cross-platform scripting with javascript on MAC and PC with the javascript extension of Adobe ExtendScript.

Using the menu Window>Tools->Scripts are already supplied with scripts displayed. Custom scripts are in the Users folder and have the ending .jsx. To the file browser to access the directory, where the scripts are found, one must simply right-click on the script and then “Open in Finder” click, we arrive at the simplest to the most deeply hidden folder. There you can create new custom scripts.

To test the file “changeLanguage.jsx” in the same folder (with me/…/Library/Preferences/Adobe InDesign/Version 7.0/en_GB/Scripts/Scripts Panel) be created. This appears autotmatisch in InDesign.

To edit the Dati, you just have to select InDesign Right click on the file and edit script and it will open the editor / IDE from Adobe Light: ExtendScript Toolkit.

It is important, set as the first, for the program to run the script, in our case: Adobe InDesign CS5 select the drop top right.

In addition, the auto-complete enabled: ExtendScript Toolkit->Preferences>Help>By car. Completion Report.

Example 1: Hello World

The Hello World is written quickly and before that includes a directive to InDesign, it is clear, for the program to run the script:

#target InDesign
alert("Hallo Welt");

This procedure can be executed by pressing F5 or Apple R.

Debugging can be very good with the function writln for strings, in the window Javascript-Konsole output is:

#target InDesign
//alert("Hallo Welt");
$.writeln ("Konsole log");

I had the problem, Comments that was hidden, that's because, that the font was not installed for comments. Simple font change: ExtendScript Toolkit->Preferences>Fonts and colors:

  • all indicators entkreuzen
  • Comment /**/ ->Font: Verdana Select
  • Comment // ->Font: Verdana Select

A reference / overview of the object model of InDesign finds here.