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

Check if Specific function executed

Enthusiast ,
Jul 27, 2020 Jul 27, 2020

Copy link to clipboard

Copied

Hi Pros.

is there away to test if Specific fucntion by Name is Exceuted, so i can alert the user that the function is executed then i can exit the script, So please can you show me an Example? , My Funtion Name is (doUnStyler();), Thanks in Advance.

 

Best Regards

Mohammad

Best
Mohammad Hasanin
TOPICS
How to , Scripting

Views

513

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 2 Correct answers

Community Expert , Jul 27, 2020 Jul 27, 2020

JS is by nature async so it could be possible that in a big document this happens. There is not much we can do here in my opinion, we can't force the sync behaviour. One idea is try putting the alert after doScript and then check.

 

-Manan

Votes

Translate

Translate
Guide , Jul 27, 2020 Jul 27, 2020

ExtendScript predates anything async in JavaScript by a decade.

 

A problem of the alert in the middle of the script execution may be that InDesign's text processing "composer" is async and not completed when the alert dialog shows and blocks all other InDesign processing (kind of the opposite of async). You may eventually get around that by adding this line before the alert(), it forces the composer to complete.

app.activeDocument.recompose()

Votes

Translate

Translate
Community Expert ,
Jul 27, 2020 Jul 27, 2020

Copy link to clipboard

Copied

I did not quite understand you requirement, how about placing an alert inside the method defintion saying it was called, like the following

function doUnStyler
{
 alert("You called doUnStyler")
//do your stuff here
}

-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 ,
Jul 27, 2020 Jul 27, 2020

Copy link to clipboard

Copied

Thank you for your reply but i mean when function is finshing executing and all the function code is done!, how to alert the user that the function is done befor exit the script?

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 ,
Jul 27, 2020 Jul 27, 2020

Copy link to clipboard

Copied

The answer remains the same as above, just the alert line becomes the last line of the function definition, unless it has some other routes of exit.

If you could show a simple example of your code decribing what is needed while the code flows across functions maybe i will be able to give a better idea

 

-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 ,
Jul 27, 2020 Jul 27, 2020

Copy link to clipboard

Copied

I tried it here but still alert the user before it real finishing the code, here is the code :

//Remove all Object Styles, Paragraph Styles, Character Styles
//Developed By Neal Derek
//Additional Developing : Mohammad Ibrahim

//Activate Undo Feature for this Script
if (parseFloat(app.version) < 6)
doUnStyler();
else
app.doScript(doUnStyler, ScriptLanguage.JAVASCRIPT, undefined, UndoModes.ENTIRE_SCRIPT, "UnStyle all Object,Paragraph,Character Styles");
//Start the Function
function doUnStyler() {
//Remove all Object Styles, Paragraph Styles, Character Styles
#target InDesign    
    myDoc = app.activeDocument;         
    var chs1 = myDoc.characterStyles.length;    
    for(var j=chs1-1; j>0; j--) //j++ replaced here by j--    
    {    
      var CharRes = myDoc.characterStyles[j].remove();    
    }    
  var pghs1 = myDoc.paragraphStyles.length;    
    for(var j=pghs1-1; j>1; j--)//j++ replaced here by j--    
    {    
      var PSRes = myDoc.paragraphStyles[j].remove();    
    }    
  var objs1 = myDoc.objectStyles.length;    
    for(var j=objs1-1; j>0; j--) //j++ replaced here by j--    
    {    
      var ObjRes = myDoc.objectStyles[j].remove();    
    }
//Check if the Program is Done
 alert("Finished")
}
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 ,
Jul 27, 2020 Jul 27, 2020

Copy link to clipboard

Copied

How did you come to the conclusion that the alert is shown before the code execution is finished. I see the script deletes the styles properly before the alert pops up. What am i missing here? Which version of InDesign are you using?

-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 ,
Jul 27, 2020 Jul 27, 2020

Copy link to clipboard

Copied

Im using InDesign 2020 (15.1.1) I have test it on 50 page document , and alert start very early!, if you test it in One Page only you will feel its normal

 

Mohammad

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
Advisor ,
Jul 27, 2020 Jul 27, 2020

Copy link to clipboard

Copied

Hello Mohammad

 

you could try waitForTask();

 

//Remove all Object Styles, Paragraph Styles, Character Styles
//Developed By Neal Derek
//Additional Developing : Mohammad Ibrahim

//Activate Undo Feature for this Script
if (parseFloat(app.version) < 6)
doUnStyler();
else
task = app.doScript(doUnStyler, ScriptLanguage.JAVASCRIPT, undefined, UndoModes.ENTIRE_SCRIPT, "UnStyle all Object,Paragraph,Character Styles");
//Start the Function
function doUnStyler() {
//Remove all Object Styles, Paragraph Styles, Character Styles
#target InDesign    
    myDoc = app.activeDocument;         
    var chs1 = myDoc.characterStyles.length;    
    for(var j=chs1-1; j>0; j--) //j++ replaced here by j--    
    {    
      var CharRes = myDoc.characterStyles[j].remove();    
    }    
  var pghs1 = myDoc.paragraphStyles.length;    
    for(var j=pghs1-1; j>1; j--)//j++ replaced here by j--    
    {    
      var PSRes = myDoc.paragraphStyles[j].remove();    
    }    
  var objs1 = myDoc.objectStyles.length;    
    for(var j=objs1-1; j>0; j--) //j++ replaced here by j--    
    {    
      var ObjRes = myDoc.objectStyles[j].remove();    
    }
//Check if the Program is Done
 task.waitForTask();
 alert("Finished")
}

 

 

you may need to move the "task = " to  function doUnStyler()

Regards,

Mike

 

Edit:   Please disregard my mistake, waitForTask(); is for background task.

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 ,
Jul 28, 2020 Jul 28, 2020

Copy link to clipboard

Copied

LATEST

Thansk alot manan, your ideas help me alot, thanks again for your generous help

Best

-Mohammad

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 ,
Jul 27, 2020 Jul 27, 2020

Copy link to clipboard

Copied

JS is by nature async so it could be possible that in a big document this happens. There is not much we can do here in my opinion, we can't force the sync behaviour. One idea is try putting the alert after doScript and then check.

 

-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 ,
Jul 27, 2020 Jul 27, 2020

Copy link to clipboard

Copied

Thank you very much

-Mohammad

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
Guide ,
Jul 27, 2020 Jul 27, 2020

Copy link to clipboard

Copied

ExtendScript predates anything async in JavaScript by a decade.

 

A problem of the alert in the middle of the script execution may be that InDesign's text processing "composer" is async and not completed when the alert dialog shows and blocks all other InDesign processing (kind of the opposite of async). You may eventually get around that by adding this line before the alert(), it forces the composer to complete.

app.activeDocument.recompose()

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 27, 2020 Jul 27, 2020

Copy link to clipboard

Copied

That's a fine idea Dirk. I meant the same where the api's return promptly and might lead to cases where the effect is not yet registered totally as you explained. i think your suggestion should work. Thanks for sharing your thoughts

 

-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 ,
Jul 28, 2020 Jul 28, 2020

Copy link to clipboard

Copied

Thank you a lot,

This is much Accurate Solution, Thank you all , everyday we learn new things

Best

-Mohammad

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