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

Delete unused text variables

New Here ,
Jun 03, 2019 Jun 03, 2019

Copy link to clipboard

Copied

Hello everybody,

is there any way to delete unused text variables, maybe by a script or even better with onboard functionality?

Often there are documents which include hundreds of text variables but only a few of them are used.

We already changed the workflow to manage variables externally and import them with a script but that does not fix the clutter of text variables still kept with the document during the document lifetime.

Currently the only way would be to manually go through the list and delete all variables manually which is not really feasible.

*Edit: Additionally it would be great to be able to export the variables already used in the document, in a simple form like text based. But that'd be a bonus 😉 if there is any solution for the problem mentioned above that would be great already...

Greetings

Rolf

Views

883

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 03, 2019 Jun 03, 2019

Hi Rolf,

a script could detect if there are any associated instances of a text variable.

Means it can see if the text variable is used in the document.

The code below ( ExtendScript / JavaScript ) will remove all unused text variables in the active document.

You can undo this in one go.

( function()

{

   

    app.scriptPreferences.userInteractionLevel = UserInteractionLevels.INTERACT_WITH_ALL;

    app.doScript

        (

       

        removeUnusedTextVars,

        ScriptLanguage.JAVASCRIPT,

        [],

  

...

Votes

Translate

Translate
Community Expert ,
Jun 03, 2019 Jun 03, 2019

Copy link to clipboard

Copied

Hi Rolf,

a script could detect if there are any associated instances of a text variable.

Means it can see if the text variable is used in the document.

The code below ( ExtendScript / JavaScript ) will remove all unused text variables in the active document.

You can undo this in one go.

( function()

{

   

    app.scriptPreferences.userInteractionLevel = UserInteractionLevels.INTERACT_WITH_ALL;

    app.doScript

        (

       

        removeUnusedTextVars,

        ScriptLanguage.JAVASCRIPT,

        [],

        UndoModes.ENTIRE_SCRIPT,

        "Remove Unused Text Variables | SCRIPT"

       

        );

       

    function removeUnusedTextVars()

    {

        var textVarsOfDoc = app.documents[0].textVariables.everyItem().getElements();

        var unusedVars = [];

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

        {

            if( textVarsOfDoc.associatedInstances.length == 0 )

            {

                unusedVars[unusedVars.length++] = textVarsOfDoc ;

            };

        };

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

        {

            unusedVars.remove();

        };

    };

}() )

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 Expert ,
Jun 03, 2019 Jun 03, 2019

Copy link to clipboard

Copied

That's a lot more concise than the loops I was creating in the mean time

Is there anything against moving line 31 "xxx.remove()" into the if .. test at line 25? You have to reverse the for.. loop (because -- as you know -- deleting an item from this array will mess up the for-loop counter), but I don't see any other problems with this.

As to listing current variables, this should do. If the type is "Custom", this little script adds the text contents as well; for other types only the name is listed.

var textVarsOfDoc = app.documents[0].textVariables.everyItem().getElements();

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

{

if (textVarsOfDoc.variableType == VariableTypes.CUSTOM_TEXT_TYPE)

  textVarsOfDoc = textVarsOfDoc.name+': "'+textVarsOfDoc.variableOptions.contents+'"';

else

  textVarsOfDoc = textVarsOfDoc.name;

}

alert (textVarsOfDoc.join('\r'));

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 03, 2019 Jun 03, 2019

Copy link to clipboard

Copied

https://forums.adobe.com/people/%5BJongware%5D  wrote

…Is there anything against moving line 31 "xxx.remove()" into the if .. test at line 25? …

Hi Jongware,

not really 🙂

Hm.

If one likes to fine-tune the result, leave the default ones, maybe it's better to do this in another loop and test for this.

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
New Here ,
Jun 03, 2019 Jun 03, 2019

Copy link to clipboard

Copied

If one likes to fine-tune the result, leave the default ones, maybe it's better to do this in another loop and test for this.

That sounds good to me. Is there a way to copy the text out of the alert windows or present it in a "copy-able" way?

Regards,

Rolf

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
New Here ,
Jun 03, 2019 Jun 03, 2019

Copy link to clipboard

Copied

Is there a way to copy the text out of the alert windows or present it in a "copy-able" way?

Done! The content of the alert can be copied with CTRL+C.

Thanks again!

Regards,

Rolf

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
New Here ,
Jun 03, 2019 Jun 03, 2019

Copy link to clipboard

Copied

Thank you Uwe,

I only used line 04 to 34 and the script works perfectly fine!

Thanks again for the lightning fast solution!

Greetings Rolf

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 03, 2019 Jun 03, 2019

Copy link to clipboard

Copied

Hi Rolf,

why not all the code?

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
New Here ,
Jun 03, 2019 Jun 03, 2019

Copy link to clipboard

Copied

Hi Uwe,

why not all the code?

I got a bracket exception with the round brackets at start and end of the script but now it seems to work fine (both variants).

Probably user error on my side at the first try...

Regards,

Rolf

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 03, 2019 Jun 03, 2019

Copy link to clipboard

Copied

LATEST

Hi Rolf,

edit: The two text variables I mentioned are visible in the UI.

Also in the German versions of InDesign.

"$ID/ TV XRefChapterNumber"

"$ID/ TV XRefPageNumber"

So I edited all I said in that previous version of this reply.
Sorry for confusing anyone here.

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