Copy link to clipboard
Copied
Hi All,
How to find the continue 3 same words in firstLine?
I tried the code 3 same words lastword of lines is working, but firstLine is not working.Please suggest friends
Code is here:
var myDoc = app.activeDocument;
var myLines_first = app.activeDocument.stories.everyItem().lines.everyItem().words.firstItem().getElements();
for (var i=0; i<myLines_first.length; i++)
{
if(myLines_first.contents==myLines_first[i+1].contents && myLines_first.contents==myLines_first[i+2].contents)
{
var myResult = app.select(myLines_first)
alert("stack word there")
break;
}
else
{
alert("No stackwords found");
exit();
}
}
Thanks,
Prabu G
Copy link to clipboard
Copied
Ananth@desgin napisał(-a)
... but firstLine is not working ...
be more specific what "not working" does mean...
no result, wrong result, error?
Jarek
Copy link to clipboard
Copied
Hi Jarek,
Thanks for the reply. In above code show error message.
Thanks,
Prabu G
Copy link to clipboard
Copied
What error? Why don't you tell us?
Copy link to clipboard
Copied
Hi,
You could try this:
var myDoc = app.activeDocument,
myLines_first = myDoc.stories.everyItem().lines.everyItem().words.firstItem().getElements(),
myCounter = 0;
for (var i=0; i < myLines_first.length-2; i++) {
if (myLines_first.contents == myLines_first[i+1].contents && myLines_first.contents == myLines_first[i+2].contents) {
var myResult = app.select(myLines_first);
alert("Words Stack Here: \"" + myLines_first.contents + "\"\r(^/) ;-)")
myCounter += 1;
}
}
if (myCounter == 0) alert("No Words Stack Found!\r(^/) :(")
(^/)
Copy link to clipboard
Copied
Hi Obi-wan Kenobi,
This worked perfectly! It was exactly the script I was looking for. Thanks so much, you're awesome!!!.
I need one clarification: "myLines_first.length-2", why we use "-2"??
Thanks,
Prabu
Copy link to clipboard
Copied
"-2" because you don't need to loop on "n-1" and "n" (targeted by "n-2")!
If you actually try, you'll get an error because "n+1" and "n+2" don't exist!
(^/)
Copy link to clipboard
Copied
Hi Obi,
Thanks,
Prabu G
Copy link to clipboard
Copied
Hi Prabu,
there is much to say about the logic of your script.
1. Error message
"Undefined is not an object"
With a document that contains only one story with two lines of text that would happen.
Or a document with two stories each conatining one line of text.
Or more stories, all empty with exception of one story with two lines or two stories with one line each.
I hope you guess why. Your loop tries to reach out to elements in the array that do not exist:
i+1 and i+2 where i is length-1
At that moment the loop throws the error "Undefined is not an object".
2. Error message
"Invalid Object".
Your document conains only empty stories and the script is throwing an error in line 2 where the array should be built.
That would also be the case with Obi-wan's version.
That error could be prevented.
var wordsLength = myDoc.stories.everyItem().words.everyItem().length;
if(wordsLength == 0){alert("No elements in collection of words"); exit()};
3. Your script is alerting false positives as you can see below.
Why? Because you are doing an array of all stories with all lines and all first words.
See the screenshot below.
The script checks the first word of the next story ( i+2 ) and compares with words i+1 and i of the first story.
Obi-wan's script is doing the same:
3. Your script is only doing one single iteration of the loop with every script run!
Why?
A. If the first three lines of the first story contain a stack it will break the loop.
B. If the first three lines of the first story contain no stack:
you alert that nothing is found.
And exit the script!
So it cannot detect a stack that begins after the first line of the first story.
Regards,
Uwe
Copy link to clipboard
Copied
Hi Uwe!
/*
Script written by Michel Allio [2017/02/26]
version B [2017/02/27]
See: https://forums.adobe.com/message/9357481
Object: Find 3 consecutive first line words! …
*/
app.doScript("main()", ScriptLanguage.javascript, undefined, UndoModes.ENTIRE_SCRIPT, "3-Words Stack! …");
function main() {
var myDoc = app.activeDocument,
myStories = myDoc.stories.everyItem().getElements(),
S = myStories.length;
if (myDoc.conditions.item("Words Stack").isValid) myDoc.conditions.item("Words Stack").remove();
myCondition = app.activeDocument.conditions.add ({name: "Words Stack", indicatorMethod: ConditionIndicatorMethod.USE_HIGHLIGHT});
while (S--) {
if (myStories
.words.length == 0) continue;var myLines_first = myStories
.lines.everyItem().words.firstItem().getElements();for (var i = 0; i < myLines_first.length-2; i++) {
if (myLines_first.contents == myLines_first[i+1].contents && myLines_first.contents == myLines_first[i+2].contents) {
myLines_first.appliedConditions = [myCondition];
myLines_first[i+1].appliedConditions = [myCondition];
myLines_first[i+2].appliedConditions = [myCondition];
}
}
}
}
(^/)
Copy link to clipboard
Copied
Bonjour Obi-wan,
Est-il possible d'avoir une explication pour la ligne suivent :
app.doScript("main()", ScriptLanguage.javascript, undefined, UndoModes.ENTIRE_SCRIPT, "3-Words Stack! …");
Merci beaucoup
Philippe
Copy link to clipboard
Copied
Hi Philippe,
the method app.doScript() is available in InDesign's DOM since a long time.
It was enhanced by the developers in InDesign CS4 where undo modes were added.
In CS4, CS5 and CS5.5 ( I'm not sure what the version is where a special bug was fixed ) there were some problems with UndoModes.FAST_ENTIRE_SCRIPT. So for sake of compatibility best use: UndoModes.ENTIRE_SCRIPT
See some details here from Jongware's DOM documentation:
Adobe InDesign CS4 (6.0) Object Model JS: Application
Search the forum and read about how doScript() can be utilized.
You will find a lot of discussions and code samples.
Back to Obi-wan's code line:
1. The first argument of the method is the name of the function that is fed.
Could also be written without its string representation:
app.doScript ( main , …
The first argument could also be a script file somewhere on the harddisk.
Or a string that is written from a text file…
2. Argument two is the script language.
For example one could also execute AppleScript code if the script is executed on Mac OSX.
Or VB code when on Windows.
3. Argument three is for Arguments that could be fed as array. E.g. if you want to inject preprocessed values from ExtendScript to an AppleScript that comes with argument one. Just one example. Search the forum for details. Also see app.scriptArgs.
4. Argument four is the undo mode
5. Argument five is the string that is presented to the user and is visible with InDesign's undo UI.
Regards,
Uwe
Copy link to clipboard
Copied
Merci Laubender,
Merci pour cette explication.
Philippe