Copy link to clipboard
Copied
Hi Scripters,
I'm interested by a space and if the char style applied to it!
So, I've written this line:
if ( myLast.contents == " " && myLast.appliedCharacterStyle = "Red-Underlined" ) ………………
I'm wrong!! Apparently, the 2nd part is not correct: I don't want to apply the char style! I only want to exclude the bad spaces and don't treat them when the char style is not applied to them!!! So, I would like: "space" + "char style applied"!
Thanks in advance!
(^/)
Copy link to clipboard
Copied
if ( myLast.contents == " " && myLast.appliedCharacterStyle.name != "Red-Underlined" )
Copy link to clipboard
Copied
Thanks Peter!
It seems to be better but something doesn't work correctly!!!
More explanations:
The deal is to add another answer to this topic: Avoid underline at the end of a line
Jongware gives us an old and great solution that works well but I find it too brutal without taking care of future modifications.
I've a different approach:
1/ I treat the "underlinings" with a grep style! … and the "no-underlined" too!! [screenshot 1]
To apply the underlining on text part, I insert 2 non-joiners before and 2 after the text.
As you see in the screenshot 2, bad underlined spaces at the line ends.
2/ I've inserted 3 direct char styles for sample. [screenshot 2]
I launch the Script for the first time. Result: screenshot 3.
3/ In a second time, I've inserted text! So the layout needs to be updated!
I launch the script another time! [screenshot 4]
The script seems to correctly update! … But not! If I launch the script several times, it inserts a bad result at the line n-1 adding non-joiners at the line end!!!!
Boring and no idea to correct this!
The code:
app.doScript(main, ScriptLanguage.JAVASCRIPT, [], UndoModes.ENTIRE_SCRIPT, "EndLineUnderlining! …");
function main ()
{
var myDoc = app.activeDocument;
app.findGrepPreferences = app.changeGrepPreferences = null;
app.findGrepPreferences.appliedCharacterStyle = "Red-Underlined";
app.findGrepPreferences.findWhat = "~j(?!~j)|(?<!~j)~j";
app.changeGrepPreferences.changeTo = "";
myDoc.changeGrep();
app.findGrepPreferences = app.changeGrepPreferences = null;
app.findGrepPreferences.appliedCharacterStyle = "Red-Underlined";
myFound = myDoc.findGrep();
for (F = 0; F < myFound.length; F++)
{
myF = myFound
;
for (L = 0; L < myF.lines.length; L++)
{
var myLast = myF.lines
.characters.item(-1); if ( myLast.contents == " " && myLast.appliedCharacterStyle.name != "Red-Underlined" ) myLast.insertionPoints[0].contents = SpecialCharacters.ZERO_WIDTH_NONJOINER;
}
}
}
(^/)
Copy link to clipboard
Copied
Gotcha!
This code works:
app.doScript(main, ScriptLanguage.JAVASCRIPT, [], UndoModes.ENTIRE_SCRIPT, "EndLineUnderlining! …");
function main ()
{
var
myDoc = app.activeDocument,
myCstyle = "Red-Underlined", // CHARACTER STYLE TO BE DEFINED!
myFindWhat = "~j(?!~j)|(?<!~j)~j",
myChangeTo = "";
app.findGrepPreferences = app.changeGrepPreferences = null;
app.findGrepPreferences.appliedCharacterStyle = myCstyle;
app.findGrepPreferences.findWhat = myFindWhat;
app.changeGrepPreferences.changeTo = myChangeTo;
myDoc.changeGrep();
app.findGrepPreferences = app.changeGrepPreferences = null;
app.findGrepPreferences.appliedCharacterStyle = myCstyle;
myFound = myDoc.findGrep();
for ( F = 0; F < myFound.length; F++ )
{
var myF = myFound
; for ( L = 0; L < myF.lines.length-1; L++ )
{
var myLast = myF.lines
.characters.item(-1); if ( myLast.contents == " " ) myLast.insertionPoints[0].contents = SpecialCharacters.ZERO_WIDTH_NONJOINER;
}
}
}
No need to have a double if statement and include the last line in the loop!
Cool! Thanks Peter to make me "think different"!
(^/)
Copy link to clipboard
Copied
Well, Michel, I must say that I much prefer Jongware's approach. You insert characters in your text, which triggers recomposition and could cause reflow, and you apply GREP styles, which aren't the quickest in longer texts. Jongware's script is much simpler and is probably much quicker too.
Peter
Copy link to clipboard
Copied
Peter,
Jongware's way has 2 breaks:
1/ it doesn't take care of future modifications of the layout;
2/ it doesn't take care of char styles already applied.
In a basic situation [without the 2 points above], I'll use it [already saved in my Scripts panel, in Jongware's folder, as "EndLineUnderlining_Jongware.jsx"].
… But, in my sample, It couldn't work!!
(^/)
Copy link to clipboard
Copied
> 2/ it doesn't take care of char styles already applied.
It does indeed look as if existing instances aren't removed, but that's easy to fix:
1. If the character style exists in the document, delete it.
2. Create the character style
3. Apply it.
That takes care of "it doesn't take care of future modifications of the layout" as well.
P.
Copy link to clipboard
Copied
Peter,
I've totally written the script. I think my first way can be keeped in some situations.
Note that the fourth para won't be taken in account because it's "right-justified"!
I launch the script:
Note that "Conditions", a char styles group and char styles in it have been created by the script.
Conditions underlining can be masked:
Important Point: If the layout moves …
… I can launch the script again:
Masking the Conditions underlining:
Script Version:
/*
Script written by Michel Allio [2016/12/04]
See: https://forums.adobe.com/thread/2245203
*/
app.doScript(main, ScriptLanguage.JAVASCRIPT, [], UndoModes.ENTIRE_SCRIPT, "EndLineUnderlining! …");
function main ()
{
var myDoc = app.activeDocument,
myParas = myDoc.stories.everyItem().paragraphs.everyItem().getElements(),
// CHARACTER STYLE TO BE DEFINED: U = Underlined | UNo = No-Underlined
myCstyleU = ["Red-Underlined_U", "Blue-Underlined_U", "Italic Green_U", "Bold Yellow_U", "Bold Italic Blue_U"];
// Group and Derived Char Styles Automatic Creation:
if( myDoc.characterStyleGroups.item( "UNo" ) == null) myDoc.characterStyleGroups.add({ name: "UNo" });
for ( U = 0; U < myCstyleU.length; U++ ) if( myDoc.characterStyles.item( myCstyleU + "No" ) == null) myDoc.characterStyles.add({ name: myCstyleU + "No" });
// All The Paras:
for ( P = 0; P < myParas.length; P++ )
{
// Target: Only Left-Center Alignments Paras:
if (myParas
.justification == Justification.LEFT_ALIGN || myParas
.justification == Justification.CENTER_ALIGN )
{
// Each Para:
var myPara = myParas
;
// Double Char Styles: with and without underlining:
for ( C = 0; C < myCstyleU.length; C++ )
{
var myU = myCstyleU
; var myNU = myU + "No";
// WARNING -- RESET
app.findGrepPreferences = app.changeGrepPreferences = null;
app.findGrepPreferences.appliedCharacterStyle = myNU;
app.changeGrepPreferences.appliedCharacterStyle = myU;
myPara.changeGrep();
// New targets = New Underlinings:
app.findGrepPreferences = app.changeGrepPreferences = null;
app.findGrepPreferences.appliedCharacterStyle = myU;
myFound = myPara.findGrep();
// End of line:
for ( F = 0; F < myFound.length; F++ )
{
var myF = myFound
; // Each Line End, except the last line:
for ( L = 0; L < myF.lines.length-1; L++ )
{ // Last char of each line:
var myLast = myF.lines
.characters.item(-1); // Deal! 😉
// -----------------------------------------------------------------------------------------------------------------------------------------
if ( myLast.contents == " " && myLast.appliedCharacterStyle = myU ) myLast.appliedCharacterStyle = myNU;
// -----------------------------------------------------------------------------------------------------------------------------------------
}
}
}
}
}
// OPTION: Double Char styles Colorization (by Conditions created here! -- Their "underlining" can be masked in the Conditions Panel!)
for ( D = 0; D < myCstyleU.length; D++ )
{
myCondition = app.activeDocument.conditions.item(myCstyleU
); if (!myCondition.isValid) myCondition = app.activeDocument.conditions.add ({name: myCstyleU
, indicatorMethod: ConditionIndicatorMethod.USE_HIGHLIGHT});
app.findGrepPreferences = app.changeGrepPreferences = null;
app.findGrepPreferences.appliedCharacterStyle = myCstyleU
; app.changeGrepPreferences.appliedConditions = [myCstyleU
]; myDoc.changeGrep();
app.findGrepPreferences = app.changeGrepPreferences = null;
app.findGrepPreferences.appliedCharacterStyle = myCstyleU
+ "No"; app.changeGrepPreferences.appliedConditions = [myCstyleU
]; myDoc.changeGrep();
}
app.findGrepPreferences = app.changeGrepPreferences = null;
alert("Done! (^/)")
}
// ___ (^/) ___
Thanks for all comment! [ … So good for my learning! ]
(^/)
Copy link to clipboard
Copied
Hi,
I've detected 2 errors/problems in my script last version and I don't find ways to fix it!
1/ I need to automatically create the char styles group I call "Uno" and the styles into it AND these char styles need to be "based on" their "brother" (same options except the underlining = "none")
So I try this:
// CHARACTER STYLE TO BE DEFINED: U = Underlined | UNo = No-Underlined
myCstyleU = ["Red-Underlined_U", "Blue-Underlined_U", "Italic Green_U", "Bold Yellow_U", "Bold Italic Blue_U"];
// Group and Derived Char Styles Automatic Creation:
if( myDoc.characterStyleGroups.item( "UNo" ) == null) myDoc.characterStyleGroups.add({ name: "UNo" });
var UNoGroup = myDoc.characterStyleGroups.item( "UNo" );
for ( U = 0; U < myCstyleU.length; U++ )
{
if( UNoGroup.characterStyles.item( myCstyleU + "No" ) == null )
{
UNoGroup.characterStyles.add({ name: myCstyleU + "No" }); //----------Problem 1: basedOn: myCstyleU
}
}
"UNo" group and "…Uno" char styles have been correctly created but the styles are not "based on" their brother!!
2/ When I launch the script now [the modification of the last version is about what I talk above: the creation of the char styles "into" "Uno" group folder],
I get an error: apparently, the new char styles are not detected!
Thanks for your help!
(^/)
Copy link to clipboard
Copied
Fixed! … but too tired to go on and search other issues!
/*
Script written by Michel Allio [2016/12/04]
See: https://forums.adobe.com/thread/2245203
*/
app.doScript(main, ScriptLanguage.JAVASCRIPT, [], UndoModes.ENTIRE_SCRIPT, "EndLineUnderlining! …");
function main ()
{
var myDoc = app.activeDocument,
myParas = myDoc.stories.everyItem().paragraphs.everyItem().getElements(),
// CHARACTER STYLE TO BE DEFINED: U = Underlined | UNo = No-Underlined
myCstyleU = ["Red-Underlined_U", "Blue-Underlined_U", "Italic Green_U", "Bold Yellow_U", "Bold Italic Blue_U"];
// Group and Derived Char Styles Automatic Creation:
if ( myDoc.characterStyleGroups.item( "UNo" ) == null ) myDoc.characterStyleGroups.add({ name: "UNo" });
var UNoGroup = myDoc.characterStyleGroups.item( "UNo" );
for ( U = 0; U < myCstyleU.length; U++ )
{
var myCsU = myDoc.characterStyles.item(myCstyleU),
myUNo = myCstyleU + "No";
if ( UNoGroup.characterStyles.item( myUNo ) == null )
{
var myCsUNo = UNoGroup.characterStyles.add({ name: myUNo, basedOn: myCsU, underline: false });
}
}
// All The Paras:
for ( P = 0; P < myParas.length; P++ )
{
// Target: Only Left-Center Alignments Paras:
if ( myParas
.justification == Justification.LEFT_ALIGN || myParas
.justification == Justification.CENTER_ALIGN )
{
// Each Para:
var myPara = myParas
;
// Double Char Styles: with and without underlining:
for ( C = 0; C < myCstyleU.length; C++ )
{
var myCsU = myDoc.characterStyles.item(myCstyleU
), myUNo = myCsU.name + "No",
myCsUNo = UNoGroup.characterStyles.item( myUNo );
// WARNING -- RESET
app.findGrepPreferences = app.changeGrepPreferences = null;
app.findGrepPreferences.appliedCharacterStyle = myCsUNo;
app.changeGrepPreferences.appliedCharacterStyle = myCsU;
myPara.changeGrep();
// New targets = New Underlinings:
app.findGrepPreferences = app.changeGrepPreferences = null;
app.findGrepPreferences.appliedCharacterStyle = myCsU;
myFound = myPara.findGrep();
// End of line:
for ( F = 0; F < myFound.length; F++ )
{
var myF = myFound
; // Each Line End, except the last line:
for ( L = 0; L < myF.lines.length-1; L++ )
{ // Last char of each line:
var myLast = myF.lines
.characters.item(-1); // Deal! 😉
// -----------------------------------------------------------------------------------------------------------------------------------------------
if ( myLast.contents == " " && myLast.appliedCharacterStyle = myCsU ) myLast.appliedCharacterStyle = myCsUNo;
// -----------------------------------------------------------------------------------------------------------------------------------------------
}
}
}
}
}
// OPTION: Double Char styles Colorization (by Conditions created here! -- Their "underlining" can be masked in the Conditions Panel!)
for ( D = 0; D < myCstyleU.length; D++ )
{
var myCsU = myDoc.characterStyles.item(myCstyleU
), myUNo = myCsU.name + "No",
myCsUNo = UNoGroup.characterStyles.item( myUNo ),
myCondition = app.activeDocument.conditions.item(myCstyleU
);
if (!myCondition.isValid) myCondition = app.activeDocument.conditions.add ({name: myCstyleU
, indicatorMethod: ConditionIndicatorMethod.USE_HIGHLIGHT});
app.findGrepPreferences = app.changeGrepPreferences = null;
app.findGrepPreferences.appliedCharacterStyle = myCsU;
app.changeGrepPreferences.appliedConditions = [myCondition];
myDoc.changeGrep();
app.findGrepPreferences = app.changeGrepPreferences = null;
app.findGrepPreferences.appliedCharacterStyle = myCsUNo;
app.changeGrepPreferences.appliedConditions = [myCondition];
myDoc.changeGrep();
}
app.findGrepPreferences = app.changeGrepPreferences = null;
alert("Done! (^/)")
}
// ___ (^/) ___
(^/)