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

Rewritting the script by using an array and Find/Change

Participant ,
Dec 12, 2023 Dec 12, 2023

Copy link to clipboard

Copied

Hi guys,

I'm enjoying writting scripts and try to slowly but surely improve the quality and efficiency of my code. A long way to go, I admit, besides being never ending…

Anyways,

I'd like to get some insight since I don't know how to make it right.

My first code is working. Not the problem here. It is just ugly and definitely amateur, and probably slow due to the bad writting. I want to make it better looking, possibly even making him faster in the process.

The second code is Not working though. That's the reason why I'm seeking help.

 

What is it about ?

- a script for changing the Cell Style of a table depending on its content. (it's for dealing with a 100+ pages long document so it's very useful for me, despite being a bit slow)

 

The code so far:

 

 

 

app.findTextPreferences.findWhat = "Text 1"
var myFound = myDoc.findText()
for(i=0; i<myFound.length; i++)
{    if(myFound[i].parent.constructor.name == "Cell")
     {myFound[i].parent.appliedCellStyle = "Cell Style 1"
     }
}

app.findTextPreferences.findWhat = "Text 2"
var myFound = myDoc.findText()
for(i=0; i<myFound.length; i++)
{
    if(myFound[i].parent.constructor.name == "Cell")
    {myFound[i].parent.appliedCellStyle = "Cell Style 1)"
    }
}

 

 

 

 

As you can see, 

- I'm using a simple F/C query

- I apply the same Cell Style 1 to both queries

 

I believe it would far better to write it in a way like below, for reading purposes and for adjustments that I will have to do along the way. I thought adding a loop before the script would be the way, but I'm missing something.

 

 

 

// Change to the new Cell Styles
var myQueries = [
    ["Text 1"],
    ["Text 2"],
    ["Text 3"],
]
    for(i = 0 ; i < myQueries.length; i++){
        try{
           app.findTextPreferences.findWhat = myQueries[i];
           var myFound = myDoc.findText()
           for(j=0; j<myFound.length; j++)
           {
           if(myFound[j].parent.constructor.name == "Cell")
           {
           myFound[j].parent.appliedCellStyle = "Cell Style 1"
           }
           } 
    }catch(e){
    }
    }

 

 

 

Something (or many things) is wrong.
I hope you can help me see what to do here

 

Thanks for the support ^^

TOPICS
Scripting

Views

135

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 , Dec 12, 2023 Dec 12, 2023

Hi @-Dain- ,

as far as I can see at one glance, you are very, very close.

Try this:

 

/ Change to the new Cell Styles
var myQueries =
[
    "Text 1" ,
    "Text 2" ,
    "Text 3" ,
];

With your approach you had 1 array containing 3 arrays that stored 1 string.

 

Regards,
Uwe Laubender
( Adobe Community Expert )

Votes

Translate

Translate
Community Expert ,
Dec 12, 2023 Dec 12, 2023

Copy link to clipboard

Copied

Hi @-Dain- ,

as far as I can see at one glance, you are very, very close.

Try this:

 

/ Change to the new Cell Styles
var myQueries =
[
    "Text 1" ,
    "Text 2" ,
    "Text 3" ,
];

With your approach you had 1 array containing 3 arrays that stored 1 string.

 

Regards,
Uwe Laubender
( Adobe Community Expert )

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
Participant ,
Dec 12, 2023 Dec 12, 2023

Copy link to clipboard

Copied

LATEST

Thanks a lot,

I was recycling that formatting of the array from another script I wrote without thinking enough about what I was doing. Thanks for pointing me out what I missed !

Script works as a charm now (and is faster^^)

 

Here is the sample, in case anyone needs something like this

 

var myDoc = app.activeDocument;
app.findTextPreferences = app.changeTextPreferences = null

var myQueries = 
[
    "Text 1",
    "Text 2",
    "Text 3",
];

for(j = 0 ; j < myQueries.length; j++){
        try{
app.findTextPreferences.findWhat = myQueries[j]
var myFound = myDoc.findText()
for(i=0; i<myFound.length; i++)

{
    if(myFound[i].parent.constructor.name == "Cell")
    {
    myFound[i].parent.appliedCellStyle = "Cell Style 1"
    }
   }
}catch(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