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

Script not working in CC2018

Community Beginner ,
Jun 13, 2018 Jun 13, 2018

Copy link to clipboard

Copied

I have the script below, but when I run it, nothing happens and I can't understand why.  It literally worked about 5 days ago and now after i updated yesterday its not working.  Please Help.  I am trying to apply a cell style based on cell content.

var myDoc = app.activeDocument

app.findTextPreferences = app.changeTextPreferences = null

app.findTextPreferences.findWhat = "1"

var myFound = myDoc.findText()

for(i=0; i<myFound.length; i++)

{

    if(myFound.parent.constructor.name == "Cell")

    {

  

   myFound.parent.appliedCellStyle = "Green"

    }

}

TOPICS
Scripting

Views

2.2K

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

Community Expert , Jun 15, 2018 Jun 15, 2018

Hi Liphou ,

after seeing into the sample document I'd prefer a GREP find to isolate cell contents like "D" vs "DP".

And you can use method clearCellStyleOverrides() on a cell.

Code below is using all this:

app.doScript

(

    main,

    ScriptLanguage.JAVASCRIPT,

    [],

    UndoModes.ENTIRE_SCRIPT,

    "Apply Cell Style to found Cell | Uwe | SCRIPT"

);

function main()

{

  

    var myDoc = app.activeDocument;

    app.findGrepPreferences = app.changeGrepPreferences = null;

  

    app.findGrepPreferences.findWhat

...

Votes

Translate

Translate
Community Expert ,
Jun 14, 2018 Jun 14, 2018

Copy link to clipboard

Copied

Hi,

best make it a habit to end every single statement with a: ;

Like that:

var myDoc = app.activeDocument;

app.findTextPreferences = app.changeTextPreferences = null;

app.findTextPreferences.findWhat = "1";

var myFound = myDoc.findText();

for(var i=0; i<myFound.length; i++)

{

    if(myFound.parent.constructor.name == "Cell")

    {

       myFound.parent.appliedCellStyle = "Green";

    };

};

I see no principle problem in the code.

What can go wrong then?

An error could be thrown if the cell style is hosted by a cell style group.

Regards,
Uwe

EDIT: To make that clear. Tested the code above with InDesign CC 2018.1 13.1.0.76 on Windows 10.

No problem found if the cell style named "Green" is in the root of the Cell Styles Panel.

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
Community Expert ,
Jun 14, 2018 Jun 14, 2018

Copy link to clipboard

Copied

Moving to InDesign Scripting forum

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 ,
Jun 14, 2018 Jun 14, 2018

Copy link to clipboard

Copied

I second Laubender​, the called cell style may be invalid as it.

Set a breakpoint and look at its status.

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
Community Beginner ,
Jun 14, 2018 Jun 14, 2018

Copy link to clipboard

Copied

I have it in the cell styles panel, Di i need it to be anywhere else?

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
Community Expert ,
Jun 14, 2018 Jun 14, 2018

Copy link to clipboard

Copied

Hi graphicsguy85 ,

call the cell style in a different way and test if it is valid:

var cellStyle = app.documents[0].cellStyle.itemByName("Green");

if( !cellStyle.isValid ){ /*do something here*/ };

What can be done if the cell style is not valid?
Look after it in a nested structure of the Cell Styles Panel or add a new one if it still cannot be found.

Do something here can be written this way:

var allCellStylesOfDoc = app.documents[0].allCellStyles; // Returns array of cell styles

for(var n=0;n<allCellStylesOfDoc.length;n++)

{

     if(allCellStylesOfDoc.name == "Green")

     { cellStyle = allCellStylesOfDoc ; break }

}

if( !cellStyle.isValid )

{

     app.documents[0].cellStyles.add

     (

          {

               name : "Green" ,

               /*

                    add other properties here. Look after read/write values you want to see.

                    I inserted only pseudo value VALUE below.

                    Look them up here: https://www.indesignjs.de/extendscriptAPI/indesign-latest/#CellStyle.html

                    appliedParagraphStyle : VALUE,

                    basedOn : VALUE ,

                    bottomEdgeStrokeColor : VALUE ,

                    …

                    verticalJustification : VALUE

               */

          }

     )

};

Then work on with the rest of your script.

At the point you apply the cell style use the variable name cellStyle instead of the name "Green".

Regards,
Uwe

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
Community Beginner ,
Jun 14, 2018 Jun 14, 2018

Copy link to clipboard

Copied

I apologize I am still very new at this.  The entire reason for the script is to be able to run it on the attached file, and it automatically fill in the color based on the cell input.  are you able to make the script according the comment above with your new script?Screen Shot 2018-06-14 at 7.37.12 PM.png

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
Community Expert ,
Jun 14, 2018 Jun 14, 2018

Copy link to clipboard

Copied

Hi,

you cannot attach a file to a forum post.

If you want to share it, just use a service like Dropbox and post the link.

Best,
Uwe

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 ,
Jun 14, 2018 Jun 14, 2018

Copy link to clipboard

Copied

Hi

Actually I just tested the script no problem, a file will be welcome to help you.

Effectivement je viens de tester le script pas de problème, un fichier serai le bien venu pour vous aidée .

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
Community Beginner ,
Jun 15, 2018 Jun 15, 2018

Copy link to clipboard

Copied

below is a link to file I am trying to use.  I am trying to turn lets just say the cells with "D" or 1* to a green background. It did work a few days ago but now won't work for some reason.  My computer is running High Sierra 10.13.4 , Indesign 13.1

Dropbox - Table Script test.indd

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 ,
Jun 15, 2018 Jun 15, 2018

Copy link to clipboard

Copied

it still works but the style is not forced "Green +".

//----

ça fonction toujours mais le style n'est pas forcé "Green+".

myFound.parent.fillColor = "C=75 M=5 Y=100 K=0";

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
Community Beginner ,
Jun 15, 2018 Jun 15, 2018

Copy link to clipboard

Copied

where should I put that line of code? Does that replace the cell style line?

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 ,
Jun 15, 2018 Jun 15, 2018

Copy link to clipboard

Copied

I do not force the style (I do not know if it's possible on a Cell Style) but place the color.
Attention, there is no control over the existence of the runner (it must be there)

/--------

Je ne force pas le style (je ne sais pas si c'est possible sur un Style de cellule) mais place la couleur.
Attention, il n'y a pas de contrôle sur l'existence de la coureur (elle doit être là)

for(i=0; i<myFound.length; i++) {

    if(myFound.parent.constructor.name == "Cell") {

                myFound.parent.appliedCellStyle = "Green";

                myFound.parent.fillColor = "C=75 M=5 Y=100 K=0";

    }

}//

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
Community Expert ,
Jun 15, 2018 Jun 15, 2018

Copy link to clipboard

Copied

Hi Liphou ,

after seeing into the sample document I'd prefer a GREP find to isolate cell contents like "D" vs "DP".

And you can use method clearCellStyleOverrides() on a cell.

Code below is using all this:

app.doScript

(

    main,

    ScriptLanguage.JAVASCRIPT,

    [],

    UndoModes.ENTIRE_SCRIPT,

    "Apply Cell Style to found Cell | Uwe | SCRIPT"

);

function main()

{

  

    var myDoc = app.activeDocument;

    app.findGrepPreferences = app.changeGrepPreferences = null;

  

    app.findGrepPreferences.findWhat = "^D$";

    

    var myFound = myDoc.findGrep();

  

    app.findGrepPreferences = app.changeGrepPreferences = null;

    

    for(var i=0; i<myFound.length; i++)

    {

        if(myFound.parent.constructor.name == "Cell")

        {

            var cell = myFound.parent;

            cell.clearCellStyleOverrides( true );

            cell.appliedCellStyle = "Green";

        };

    };

  

};

FWIW: The script's action can be undone in one go.

Regards,
Uwe

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 ,
Jun 15, 2018 Jun 15, 2018

Copy link to clipboard

Copied

Hi Laubender​, and thank you

Actually I noticed that it was more prudent to search in GREP for not locating another "D".
Thank you for the command ".clearCellStyleOverrides (true)" (it will serve me again),
I did not search further, because forced cell style, here, removed the double lines of cell cards.
Very good example of code thank you (I keep)

Effectivement j'avais remarqué qu'il étais plus prudent de faire une recherche en GREP pour ne pas localisé d'autre "D".

Merci pour la commande ".clearCellStyleOverrides( true )" (elle me servira encore),

je n'avais pas recherche plus loin, car forcé le style de cellule, ici, va supprimé les double lignes des carde de cellule.

Très bonne exemple de code merci ( je garde)

Philippe

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
Community Beginner ,
Jun 15, 2018 Jun 15, 2018

Copy link to clipboard

Copied

I will try this as soon as I can get back to my computer, thank you and everyone else who has been so extremely helpful. This will save me hours of work if this works.

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
Community Expert ,
Jun 15, 2018 Jun 15, 2018

Copy link to clipboard

Copied

The way that Mac and Windows deal with addressing styles differs slightly. To avoid problems with applying styles, reference the object, don't use a string. So instead of this:

myFound.parent.appliedCellStyle = "Green";

use this:

myFound.parent.appliedCellStyle = app.documents[0].cellStyles.item ("Green");

P.

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
Community Beginner ,
Jun 15, 2018 Jun 15, 2018

Copy link to clipboard

Copied

I tried this in Indesign 12.1.0.56 mac 10.10.5 and it works, I will try on CC 2018 when I get home.  Thank you again!  Wherever you are in the world. I appreciate your help.

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
Community Beginner ,
Jun 15, 2018 Jun 15, 2018

Copy link to clipboard

Copied

Am I able to search for numbers with special characters like 1* or 2*

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
Community Expert ,
Jun 15, 2018 Jun 15, 2018

Copy link to clipboard

Copied

You can look for whole numbers using this:

app.findGrepPreferences.findWhat = '\\d+';

To match numbers with decinals and thousands separators, use this:

app.findGrepPreferences.findWhat = '\\d[\\d.,]+';

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
Community Expert ,
Jun 18, 2018 Jun 18, 2018

Copy link to clipboard

Copied

graphicsguy85  wrote

Am I able to search for numbers with special characters like 1* or 2*

Do you mean you are looking for 1* as contents of a cell ?

Then you could do this:

app.findGrepPreferences.findWhat = "^1\\*$";

Regards,
Uwe

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
Community Beginner ,
Jul 19, 2018 Jul 19, 2018

Copy link to clipboard

Copied

Tank you again for the help, would you be so kind to assist me a little further?

How would I search for the following:         (1)

                                                                     (G*)

I am having trouble for it searching for the Parentheses

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
Community Expert ,
Jul 19, 2018 Jul 19, 2018

Copy link to clipboard

Copied

LATEST

Look for '\\(1\\)'

Parentheses, brackets, braces, asterisks, and a clutch of other symbols have a special meaning in Grep, so you need to escape them, as it's called. And in strings you need to escape the backslash because it too is a special character in Grep.

Go on the net and read up on Grep. It's worth it.

P.

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
Community Beginner ,
Jun 14, 2018 Jun 14, 2018

Copy link to clipboard

Copied

when it runs it applies the text style to what I want it to but it doesn’t apply the cell style, it basically ignores 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