Copy link to clipboard
Copied
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
}
alert("You have " + h + " words in this document" )
--------------------------------------------------
any idea?
thanks in advance
1 Correct answer
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
...Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
Thanks for taking time to reply.
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
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…
Every time contents is added, counter is updated…
HTH,
Loic
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
That's exactly what I wanted, thanks for your help.

