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

Custom text variable from javascript

Community Beginner ,
Apr 08, 2016 Apr 08, 2016

I have this little script that gives the word count of a document and was wondering how I could insert it into a custom text variable (so I can use it in a footnote later, along with doc. name, date etc.)

Here's the script:

--------------------------------------------------

var myDoc = app.activeDocument

h = 0

for ( var j = 0; myDoc.stories.length > j; j++) {

h = myDoc.stories.words.length + h

}

alert("You have " + h + " words in this document" )

--------------------------------------------------

any idea?

thanks in advance

TOPICS
Scripting
3.4K
Translate
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

People's Champ , Apr 09, 2016 Apr 09, 2016

Hi,

I understand two things here. First of all our friend wants to know how many words are inside his document. Second of all, he seems to look for a dynamic solution (text variable) that would be automatically updated (otherwise no need for a variable).

So here is an approach. An idle task is continously updating a text variable. So our friend can have a dynamic count of the documents words. I chose to excluse etxt variables intances but it may require deeper adjustments (as some variables might

...
Translate
Enthusiast ,
Apr 09, 2016 Apr 09, 2016

A text variable is simply a string. You can write your script as text and create a text variable instance using the text. But you will need some way to run the text (the script).

This is normally done with do script (doScript in ExtendScript).

The variable can't run itself or interpret itself--it is just text. You could embed the script in the text variable instance's label. But again, you would need a script to read the label and run the script saved in the label.

Since all you are wanting is a modal dialog box to report the number of words in the document, just write the script and run it. Maybe assign the script to a keyboard shortcut. I see no advantage to using a text variable instance.

Translate
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 ,
Apr 11, 2016 Apr 11, 2016

Thanks for taking time to reply.

Translate
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 ,
Apr 09, 2016 Apr 09, 2016

Sylvain -- When you say 'wondering how I could insert it into a custom text variable', by 'it' you mean the word count, not the script, correct? I think that Sue thinks you want to store the text of the script in a variable.

Assuming that you want to stoe just the word count, and that you have a custom text variable named 'word count', then add this line after your script:

app.activeDocument.textVariables.item('word count').variableOptions.contents = String (h);

Peter

Translate
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 ,
Apr 09, 2016 Apr 09, 2016

Hi,

I understand two things here. First of all our friend wants to know how many words are inside his document. Second of all, he seems to look for a dynamic solution (text variable) that would be automatically updated (otherwise no need for a variable).

So here is an approach. An idle task is continously updating a text variable. So our friend can have a dynamic count of the documents words. I chose to excluse etxt variables intances but it may require deeper adjustments (as some variables might not be to exclude).

I have used a snippet from @indiscripts tfor counting words : http://www.indiscripts.com/post/2011/09/what-exactly-is-a-word

#targetengine "session"

main();

//Will add a custom idleTask listener to count current document words

function main()

{

    var myIdleTask = app.idleTasks.item("countWords");

   

    if ( !myIdleTask.isValid ) {

        myIdleTask = app.idleTasks.add({name:"countWords", sleep:100});

        var onIdleEventListener = myIdleTask.addEventListener(IdleEvent.ON_IDLE,  onIdleEventHandler, false);

    }

}

//Idke task listener

function onIdleEventHandler(myIdleEvent)

{

   

    var doc = app.properties.activeDocument,

    tv;

   

    if ( !doc ) return;

   

    tv = doc.textVariables.item("countText");

   

    !tv.isValid && tv = doc.textVariables.add({name:"countText", variableType:VariableTypes.CUSTOM_TEXT_TYPE});

   

    tv.variableOptions.contents =  String( countWords() );

   

}

//Snippet from indiscripts.com = http://www.indiscripts.com/post/2011/09/what-exactly-is-a-word

//with minor modification to exclude textVariable instances.

var countWords = function F(/*Story|Cell|Footnote*/every)

    {

    var ret, t;

    every = every || app.activeDocument.stories.everyItem();

    if( !every.isValid ) return 0;

    ret = every.words.length - every.textVariableInstances.length;

    t = every.texts &&

        every.texts.everyItem &&

        every.texts.everyItem();

    if( !t ) return ret;

    t.tables.length &&

        ret += F( t.tables.everyItem().cells.everyItem() ) - t.tables.everyItem().cells.everyItem().textVariableInstances.length;

    t.footnotes.length &&

        ret += F( t.footnotes.everyItem() ) - t.footnotes.everyItem().textVariableInstances.length;

    t = null;

    return ret;

    };

before anything is typed in…

Capture d’écran 2016-04-09 à 21.52.11.png

Every time contents is added, counter is updated…

Capture d’écran 2016-04-09 à 21.52.34.png

HTH,

Loic

Ozalto | Productivity Oriented - Loïc Aigon

Translate
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 ,
Apr 11, 2016 Apr 11, 2016
LATEST

Works perfectly! Amazing, thank you so much!

I actually didn't think to push it that far yet, but dynamic would definitely be an interesting solution(better).

I'll will play with your script a bit and see what it can do.

Thanks a lot.

Translate
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 ,
Apr 11, 2016 Apr 11, 2016

That's exactly what I wanted, thanks for your help.

Translate
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