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

is it Possible to Apply Object Style to Overset Text Frame from GREP Result?

Enthusiast ,
Aug 27, 2021 Aug 27, 2021

Copy link to clipboard

Copied

Hi Experts

When Trying to Applying Object Style noramally via Script it applies easy like this example :

//Applying ObjectStyle to TextFrame
var myTextFrame = app.selection[0]
var myObjectStyle = app.documents[0].objectStyles.itemByName("Red Frame")
if(myObjectStyle.isValid)
	myTextFrame.applyObjectStyle(myObjectStyle)

But when I tried  to apply Object Style to GREP Result! it didn't apply to any Text Frame Will become overset text after apply, it seems it will apply then undo automatically!!, and also if the text frame is already overset also the script will not apply the Object Style!!, this drive me Insane! 🙂

Here is a Snippet form My Code :

var myObjectStyleIndx = myDropdown3.selection.index
var myRange = app.activeDocument; 
myFoundText = myRange.findGrep();
for(i=0;i<myFoundText.length;i++){
myFoundText[i].parentTextFrames[0].appliedObjectStyle=myDoc.allObjectStyles[myObjectStyleIndx];

please help if that possible, and Thanks in Advance

Best
Mohammad Hasanin
TOPICS
Scripting

Views

553

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 ,
Aug 27, 2021 Aug 27, 2021

Copy link to clipboard

Copied

Works fine for me. Maybr, I am not able to understand properly your precondtions regarding the overset. Do you mean that the found text is in the overset part of the textframe. If that is the case then your code that applies the style will crash as parentTextFrame property of an overset text would be undefined, if your code has a try catch block comment it out and you shall get the error I say. If I did not understand the issue correct then you need to send a sample InDesign document and a small snippet of code the reproduces the issue, remove the UI portion of the code and just include the grep search and style apply code.

-Manan

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 ,
Aug 28, 2021 Aug 28, 2021

Copy link to clipboard

Copied

Hi @Manan Joshi , Thank you for your reply, i cut the code out of UI and put it in Seperate JSX Script to simplify things but now it give me strange error never showned before!, here the code :

//Clear the find/change text preferences.
app.findGrepPreferences = app.changeGrepPreferences = null;
//grep options
app.findChangeGrepOptions.includeMasterPages = true;
app.findChangeGrepOptions.includeFootnotes = true;
app.findChangeGrepOptions.includeHiddenLayers = true;
app.findChangeGrepOptions.includeLockedLayersForFind = true;
app.findChangeGrepOptions.includeLockedStoriesForFind = true;

//var myObjectStyleIndx = 3;
//var myObjectStyle = myDropdown3.selection.toString();
var myDoc = app.activeDocument;
var myRange = app.activeDocument;
var myFind = "\\(.+?\\)";
var myChange = "$0";
var myGrepFoundTextOS = [];

app.findGrepPreferences.findWhat = myFind; 
app.changeGrepPreferences.changeTo = myChange;

myFoundTextOS = myRange.findGrep();
 for (var i = 0; i < myFoundTextOS.length; i++) {
         for (var j = 0; j < myFoundTextOS[i].length; j++) {     
     // add the actual found text item to the new array
    myGrepFoundTextOS.push(myFoundTextOS[i][j]);
    }
}
    //Apply Results Found as User Wish
for (var n = 0; n < myGrepFoundTextOS.length; n++) {
    myGrepFoundTextOS[n] = myRange.changeGrep(); 
    myGrepFoundTextOS[n].parentTextFrames[0].appliedObjectStyle=app.activeDocument.objectStyles.itemByName("Red Frame");
    //myGrepFoundTextOS[n].parentTextFrames[0].appliedObjectStyle=myDoc.allObjectStyles[myObjectStyleIndx];
    app.changeGrepPreferences.appliedObjectStyle = myDoc.allObjectStyles[myObjectStyleIndx];
             //Results
             var ri = myGrepFoundTextOS.length
             if(ri == 0){ 
             alert("Cannot Find Match!");
             }else{
             alert("Change Completed "+ri+" GREP Replacement(s) made");
             var ri = 0;
                           }
                        }

The Error is , bur no error shown if i comment the Array Push Line! also nothing changed the document! :

Doesnt Support method zero.jpg

Best
Mohammad Hasanin

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 ,
Aug 28, 2021 Aug 28, 2021

Copy link to clipboard

Copied

Looking at your code I don't quite understand what you are trying to do exactly. However, I do see lots of issues in the code(things that can cause actual crashes and logical errors as well). See the points explained below

 

 for (var i = 0; i < myFoundTextOS.length; i++) {
    for (var j = 0; j < myFoundTextOS[i].length; j++) {     
        // add the actual found text item to the new array
        myGrepFoundTextOS.push(myFoundTextOS[i][j]);
    }
}

 

In the above code snippet you have the result of findGrep in myFoundTextOS. Now the type of myFoundTextOS would be either a text or word or some similiar object. In the push line you are trying to use [] operator on these objects which according to my test results in an error which is fine as per the DOM. What are you trying to save in this array? If this code worked then lets say you had 3 search results with 5 characters in each result then you would have 15 entries in your myGrepFoundTextOS array, do you need that? Looking at your code following it I don't think you need those 1 character per slot in myFoundTextOS array. So the error is right and if you need to make if still work the way it is then maybe use something like the following

 

myGrepFoundTextOS.push(myFoundTextOS[i].contents[j]);

 

In the loop that follows you use the following statement in every iteration

myGrepFoundTextOS[n] = myRange.changeGrep();

It does not make any sense to me, in the first iteration all the objects will be changed(myRange points to document maybe you watned to assign it with something else) and whatever operation you want to do on the changed text is done then all the following iterations would practically do nothing since no change would be done(they were already done on the 1st iteration)

Secondly you are assigning changGrep return value to myGrepFoundTextOS[n], this overwrites that value you saved in it originally. If you had to override the value then why did you save it in the first place, this means the loop in which you get the error is non required.

You used appliedObjectStyle property of changeGrepPreferences object but I don't see that this propery even exists. See the following

https://www.indesignjs.de/extendscriptAPI/indesign-latest/#ChangeGrepPreference.html

So in the end I would say plan your code, think what you want to do, study the return type of all the api's you plan to use, understand the return type and then add code to your logic. Also keep testing with each small bit of code that you write and debug the reason for crash or unwanted results, don't write a massive peice of code rightaway and then debug it.

I am hopeful that if you try to rectify my points, follow my generic suggestions you will fix your problem. Good luck

-Manan

 

 

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 ,
Aug 28, 2021 Aug 28, 2021

Copy link to clipboard

Copied

Also regarding the overset thing that you mentioned, as I explained that could be a result of the next find result being in the overset(created due to object style apply operation) which subsequently causes crash and the code might be undoing the stack(do you use doScript method anywhere?). All this is specualtion at the moment as I can't see your document neither does the code snippet that we have curently is in a shape that should be eligible to work in the first place. So I suggest, fix the code to work for all the other conditions except overset, then if the overset causes a crash or strange behaviour then we can have a discussion on that.

-Manan

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 ,
Aug 28, 2021 Aug 28, 2021

Copy link to clipboard

Copied

Hi @Manan Joshi 

Update!, for Strange Reason it Worked!, i dont know why not worked before!

Button1.onClick = function()
{
    doRadioButtonOpt();
   //app.doScript(doRadioButtonOpt, ScriptLanguage.JAVASCRIPT, undefined, UndoModes.ENTIRE_SCRIPT, "Apply GREP to Styles");   
}

now it worked and apply object style inside overset text frames!, i will continue testing and tell you

 

Best
Mohammad Hasanin

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 ,
Aug 28, 2021 Aug 28, 2021

Copy link to clipboard

Copied

In your doscript you mention the undo mode so in case of an exception that is not handled the whole script would be undone and that could result in conditions where you would think that no operation was performed when in actaulity the operation was performed, the code crashed and whole script was undone.

It's good that you have got the code working atleast for some conditions, work from there and compare the code/conditions that work and that don't work and then draw some conclusions, test those conclusions with some test cases and keep refining your code and understanding. It's high time you move to the next step of succesfully debugging the code and come up with a final solution that you can explain fully. @Laubender is here to guide us as well so you have people looking over your shoulder to guide you, so give it a shot and bring some ideas to the table.

-Manan

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 ,
Aug 28, 2021 Aug 28, 2021

Copy link to clipboard

Copied

LATEST

Thanks a lot @Manan Joshi @Laubender , I Will try to fix all the issues in the script as i can , you are great friends looking over my shoulder to guide me, im so thankful for you and all the great people who helped me and still helping all the community.

Best
Mohammad Hasanin

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 ,
Aug 28, 2021 Aug 28, 2021

Copy link to clipboard

Copied

First Thanks a lot @Manan Joshi for detailed reply, that wil help me a lot in seeing the main problem, actually the main target of the script is finding pages that contain the founded GREP and Apply Object Style to their Text Frames (after result found of GREP) so as the example if the user search for words inside brackets the script shouid apply Object Style to these founded text frames, also i used the Push Array Command to Record the result and alert the user later with the amount of Applied (object Styles) on Text Frames contains GREP Results,  and thanks for correcting me the code , i will test it and tell , and for the other question about (doScript) so the answer is yes!, i used palette in my script (actually 3 palettes - one main and other two called from the main) and i read an old article in the adobe community about that i can run the code without problem if i used (doScript) inside the (OK) Button so my Execute Button is Looks like this :

 

Button1.onClick = function() 
{
   app.doScript(doRadioButtonOpt, ScriptLanguage.JAVASCRIPT, undefined, UndoModes.ENTIRE_SCRIPT, "Apply GREP to Styles");   
}

 

 i found this is the only way that i can run the script, other ways caused me (Crash) or (Freeze) the Script Buttons!, I dont know why, but you are the man!, you descovered the problem!, now what you reocmmend in replacing this code? because the main functions not called directly if I Call it from Button1.onClick and Casued a Freeze!, its something like that :

 

//User Selection for Radio Buttons
function doRadioButtonOpt() {
    myDoc = app.activeDocument;         
    //Character Style
  if (radio1.value == true) {
    CharStyleGREP();
    }
    //Paragraph Style
    if (radio2.value == true) {
    ParaStyleGREP();
    }
    //Object Style
    if (radio3.value == true) {
    ObjStyleGREP();
   }
}   

 

so if i called ( doRadioButtonOpt();) Directly from Button 1 it will not work and after searching the community i found old article for similar condition , i cant remember but unlucky no much information about how to deal with modeless palette rather than modal (dialog) because i want the user to interact with indesign, and i didnt know why the execution button not working! and cause script to freeze!, so i search the community and found this solution!, maybe its not suitalbe my case and cause a lot of troubles!, but thanks again for mention that, also i have to find another solution because you seems to see its the reason of the sudden UNDO after Apply the Object Style , and i remembered that also i couidnt add UNDO Feature to the Palette script without this way so i need your suggestion? are that possible(Adding UNDO Feature) without using execute button? beauce in the past it also caused errors, maybe because it was my first time to test modless script! , anyway thank a lot, I'm so greatful for your advices and recommendations and i will do my best to fix the script and let you know.

Best
Mohammad Hasanin

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 ,
Aug 28, 2021 Aug 28, 2021

Copy link to clipboard

Copied

yes in the past code if it found text frame and can be added object style without become overset text inside it will apply but if it will be overseted after apply it will not apply and also if any textframe is already overset the script will not apply object style, so i updated the code and got new errors!

Best
Mohammad Hasanin

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 ,
Aug 28, 2021 Aug 28, 2021

Copy link to clipboard

Copied

Hi M. Hasanain,

in the end by applying an object style to the text frames you create a "moving target".

The found text will perhaps move to a different text frame or to overset.

This is a situation where its hard to judge what to do. We need at least screenshots of the thing you like to do.

 

That visually answer one or more important questions:

[1] What is the reason that a found text's text frame gets a different object style than all other frames in the story.

[2] Could you add text frames ( also pages ) to the story if text is squeezed to overset.

[3] Is there another object style for frames that do not contain the found text?

 

Regards,
Uwe Laubender

( ACP )

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 ,
Aug 28, 2021 Aug 28, 2021

Copy link to clipboard

Copied

Hi @Laubender , Thanks a lot for your reply, i miss you, hope you are very fine, you are right i will make some screen shots and let you see, in some test i discovered i can fix the problem by adding some tricks in (Selected Story) option and it works! (Selected Story only) still trying to fix document mode :

 

//-----------------------------------------------------------//      
//Scope is myRange (User Selected Story)
//-----------------------------------------------------------// 
var myObjectStyle = myDropdown3.selection.index
var myRange = app.activeDocument.selection[0].parentStory; //User Selected Story
//------------------------------------------------//
myFoundTextStoryOS = myRange.findGrep();
//------------------------------------------------//
//Now Test Result Showing Values
for(i=0;i<myFoundTextStoryOS.length;i++){
    //Show Results Found as User Wish
    myFoundTextStoryOS[i].parentTextFrames[0].appliedObjectStyle=myDoc.allObjectStyles[myObjectStyle];
    //to Force Story Text Frame to Fit First
    var txts = myRange.texts.everyItem();
    while (myRange.overflows) {
    txts.pointSize -= .1;
    //End Shrink Stories
           //Start Counting
             var ri = myFoundTextStoryOS.length
             if(ri == 0){ 
             CheckResults.text = "Cannot Find Match!";
             }else{
             CheckResults.text = "Change Completed "+ri+" GREP Replacement(s) made";
             var ri = 0;
                    }
                }
             }

 

this will insure story will shrink to let the script apply the Object Style, i hope to find some solution to whole document.

 

Best
Mohammad Hasanin

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 ,
Aug 28, 2021 Aug 28, 2021

Copy link to clipboard

Copied

@Laubender (Answers to your questions)

 

[1] What is the reason that a found text's text frame gets a different object style than all other frames in the story.

A1: Beacause the Text inside TextFrame Some of it is Result of GREP Founded so Object Style Shouid be Applied to that text frame.

[2] Could you add text frames ( also pages ) to the story if text is squeezed to overset.

A2: Yes you Can

[3] Is there another object style for frames that do not contain the found text?

A3 : Yes there are 

Best
Mohammad Hasanin

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