• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Controlling table column alignment using variable

Enthusiast ,
Jan 27, 2013 Jan 27, 2013

Copy link to clipboard

Copied

Hi I'm new to Javascript, having only used Applescript before.

I'm using Indesign 5.5 on Mac and I'm learning Javascript by adapting old Applescripts that I have been using for years.

So, with an existing table, one of the things I want to do is to set the alignments in the columns using a varible. Here's what I've tried so far:

function setColumnWidthsAndAlignments()

{

    var myWidths=[29, 23, 13, 13, 13, 13];

    var myAlignments=["leftAlign","leftAlign","centerAlign","centerAlign","rightAlign","rightAlign"];

    var numberOfColumns=myTable.columns.count();

    for (c=0;c<numberOfColumns;c++)

    {

        myTable.columns.width=myWidths;

        myTable.columns.cells.everyItem().texts.everyItem().justification=Justification.myAlignments;

    }

}

but I get an error

-- Object does not support the property or method 'myAlignments' --

If I discard the variable myAlignments and just put leftAlign for example, it works, so maybe I'm not declaring the variable properly?

I know I could add a few extra paragraph styles and cell styles and style the table that way, but I though this would be a good learning exercise (besides which I want to limit the number of paragraph styles in the document - there are enough already!)

If anybody is really interested, I'd like to place the whole script here and would really welcome any feedback on how the structure could be improved or optimised or just tell me if I'm heading in the right direction.

Thanks and regards,

Malcolm

//Simple table script 1

app.scriptPreferences.userInteractionLevel = UserInteractionLevels.interactWithAll; //make sure that user interactivity is switched on ????  why????

if(checkForOpenDocs()) //checks to see if there are any open Indesign documents

    {

        if(targetMyTable()) //checks to see if the selection is inside a table - if not, gives an error dialog - if it is, returns variable myTable which can be used to target different parts of the table

            {

               // selectLastRow();

                //resetTable();

                //applyTableTextParaStyle();

                //removeRules();

                //applyAlternatingFills();

                setColumnWidthsAndAlignments();

                //setRowHeightsAndInsets();

               

               

    }

}

function checkForOpenDocs()

{

    if (app.documents.length!=0)   //there is at least one document open

    {

        myDocument = app.documents.item(0);    //get the id of the frontmost document - might be useful?

        return true;

    }  

    else

    {

        alert("There are no documents open");

        return false;

     }

}

function targetMyTable() //no matter what is selected in the table, change selection to be whole table

{

    if(app.selection[0]==null) //if there is nothing selected

    {

        alert("There is nothing selected.");

        return false

    }

    var mySelection = app.selection[0];

   

    switch(mySelection.constructor.name)

    {

                                        //When a row, a column, or a range of cells is

                                        //selected, the type returned is always "Cell"

        case "Cell":

        myTable=mySelection.parent;

        return true

        break;

        case "Table":

        myTable=mySelection;

        return true

        break;

        case "InsertionPoint":

        case "Character":

        case "Word":

        case "TextStyleRange":

        case "Line":

        case "Paragraph":

        case "TextColumn":

        case "Text":

        if(app.selection[0].parent.constructor.name == "Cell")

        {

            myTable=mySelection.parent.parent;

            return true

        }

        else

        {

            alert("The selection is not inside a table.");

            return false

        }

        break;

        case "Rectangle":

        case "Oval":

        case "Polygon":

        case "GraphicLine":

        case "TextFrame":

        if(app.selection[0].parent.parent.constructor.name == "Cell")

        {

            myTable=mySelection.parent.parent.parent;

            return true

        }

        else

        {

            alert("The selection is not inside a table.");

            return false

        }

        break;

        case "Image":

        case "PDF":

        case "EPS":

        if(app.selection[0].parent.parent.parent.constructor.name == "Cell")

        {

            myTable=mySelection.parent.parent.parent.parent;

            return true

        }

        else

        {

            alert("The selection is not inside a table.");

            return false

        }

        break;

       

        default:

        break;

    }

}

function resetTable()

{

    myTable.cells.everyItem().clearCellStyleOverrides (true);  

}

function selectLastRow()

    {

    //myTable.cells.itemByRange(0,-1).select();

    myTable.rows.itemByRange(-1,-1).select();

    }

function applyTableTextParaStyle()

{

    myParagraphStyle=myDocument.paragraphStyles.item("•table text");

    myTable.cells.everyItem().texts.everyItem().appliedParagraphStyle = myParagraphStyle;

}

function removeRules()

{

    myTable.cells.everyItem().topEdgeStrokeWeight=0;

    myTable.cells.everyItem().bottomEdgeStrokeWeight=0;

    myTable.cells.everyItem().leftEdgeStrokeWeight=0;

    myTable.cells.everyItem().rightEdgeStrokeWeight=0;

}

function applyAlternatingFills()

{

    myColor=myDocument.swatches.item("Black");

    myTable.alternatingFills=AlternatingFillsTypes.alternatingRows;

    myTable.startRowFillColor=myColor;

    myTable.startRowFillTint=20;

    myTable.endRowFillColor=myColor;

    myTable.endRowFillTint = 0;

}

function setColumnWidthsAndAlignments()

{

    var myWidths=[29, 23, 13, 13, 13, 13];

    var myAlignments=["leftAlign","leftAlign","centerAlign","centerAlign","rightAlign","rightAlign"];

    var numberOfColumns=myTable.columns.count();

    for (c=0;c<numberOfColumns;c++)

    {

        myTable.columns.width=myWidths;

        myTable.columns.cells.everyItem().texts.everyItem().justification=Justification.myAlignments;

    }

}

function setRowHeightsAndInsets()

{

    myTable.rows.everyItem().minimumHeight=1.058;

    myInset=0.5;

    myTable.rows.everyItem().topInset=myInset;

    myTable.rows.everyItem().bottomInset=myInset;

}

TOPICS
Scripting

Views

2.3K

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Enthusiast , Jan 27, 2013 Jan 27, 2013

Hi,

welcome

myAlignments should be properties, not strings. didn't read the second part of your post ..

myTable = app.activeDocument.stories[0].tables[0];

setColumnWidthsAndAlignments(myTable)

function setColumnWidthsAndAlignments(tableObj)

{

    var myWidths=[29, 23, 13, 13, 13, 13];

    var myAlignments=[Justification.LEFT_ALIGN,Justification.LEFT_ALIGN,Justification.CENTER_ALIGN,Justification.CENTER_ALIGN,Justification.RIGHT_ALIGN,Justification.RIGHT_ALIGN];

    var numberOfColumns=tableObj.columns.c

...

Votes

Translate

Translate
Enthusiast ,
Jan 27, 2013 Jan 27, 2013

Copy link to clipboard

Copied

Hi,

welcome

myAlignments should be properties, not strings. didn't read the second part of your post ..

myTable = app.activeDocument.stories[0].tables[0];

setColumnWidthsAndAlignments(myTable)

function setColumnWidthsAndAlignments(tableObj)

{

    var myWidths=[29, 23, 13, 13, 13, 13];

    var myAlignments=[Justification.LEFT_ALIGN,Justification.LEFT_ALIGN,Justification.CENTER_ALIGN,Justification.CENTER_ALIGN,Justification.RIGHT_ALIGN,Justification.RIGHT_ALIGN];

    var numberOfColumns=tableObj.columns.count();

    for (c=0;c<numberOfColumns;c++)

    {

        myTable.columns.width=myWidths;

        myTable.columns.cells.everyItem().texts.everyItem().justification=myAlignments;

    }

}

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
People's Champ ,
Jan 27, 2013 Jan 27, 2013

Copy link to clipboard

Copied

(Hans beat me to it!)

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Jan 27, 2013 Jan 27, 2013

Copy link to clipboard

Copied

LATEST

Perfect! - that's great. Thank you very much Hans.

Arīel - I didn't understand the eval statement when I tried to use the variable not as string (if that makes sense).

Now you've explained it, it all makes sense - thank you!

Regards,

Malcolm

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
People's Champ ,
Jan 27, 2013 Jan 27, 2013

Copy link to clipboard

Copied

You'd probably have more luck trying something like:

var myAlignments = [Justification.LEFT_ALIGN, Justification.LEFT_ALIGN,

Justification.CENTER_ALIGN etc.]

and then in the other line: ...texts.everyItem().justification =

myAlignments;

Alternatively, you could try keeping it as you have it, but perhaps

changing it to this:

texts.everyItem().justification = eval("Justification."+myAlignments);...

which just creates a string and then runs it with eval.

At any rate, the problem as I see it with what you have is that you

myAlignments is an array of strings, and you cannot access a property of

an object (in this case Justification) with a dot+string. When you put

"Justification.leftAlign" and it works, I think that's because leftAlign

isn't acting as a string -- it's simply a property of the Justification

object.

Ariel

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines