Copy link to clipboard
Copied
Hi, sorry I am new with the Scripting.
Inside of my document, are many CODES, and I need check with Indesign Script:
For example this is a example of my document with the 2 different cases:
All human beings are born free and equal in dignity and rights. (CODE 001) They are endowed with reason and conscience and should act towards one another in a spirit of brotherhood.
Everyone has the right to life, (CODE 002) liberty and security of person.
Maybe I need an Array, because my CODES are many, like:
“CODE 001”
“CODE 002”
“CODE 003”
“CODE 004”
...
Thanks so much for your help.
Copy link to clipboard
Copied
1. If your codes match a pattern, look for them using a GREP search such as CODE\s\d+
2. Iterate through the found items. If an item's style is Regular, make it Italic, otherwise (i.e. if it's Italic) make it Regular.
P.
Copy link to clipboard
Copied
Hi Peter,
sorry no the codes not match a pattern, I use for create a simple example. The code are like:
"STR 94583"
"TRe87 090"
"MR 840EOA"
Copy link to clipboard
Copied
Hi Manuel,
If you cannot create a GREP pattern that will find your codes you need a list of all possible codes and do a search with every item of the list. Or if you are lucky you'll find another unique formatting property for all used codes and you are searching with that…
Regards,
Uwe
Copy link to clipboard
Copied
In that case you'll have to list them in an array. Your script would be along these lines (untested):
codes = ["STR 94583", "TRe87 090", "MR 840EOA"];
app.findTextPreferences = null;
for (i = 0; i < codes.length; i++) {
app.findTextPreferences.findWhat = codes;
found = app.documents[0].findText();
for (j = 0; j < found.length; j++) {
if (found
.fontStyle == 'Italic') { found
.fontStyle == 'Regular'; } else {
found
.fontStyle == 'Italic'; }
}
}
P.
Copy link to clipboard
Copied
Looks works, but I added a alert with else if, for show when is a error. But now don't work. Please can you check, or let me know how need I see the errors.
Thanks!
codes = ["STR 94583", "TRe87 090", "MR 840EOA"];
app.findTextPreferences = null;
for (i = 0; i < codes.length; i++) {
app.findTextPreferences.findWhat = codes;
found = app.documents[0].findText();
for (j = 0; j < found.length; j++) {
if (found
.fontStyle == 'Italic') { found
.fontStyle == 'Regular'; } else if (found
.fontStyle == 'Italic'){ } else{alert("ERROR: some styles are wrong")}
}
}
Copy link to clipboard
Copied
Your code makes little sense. It says 'If style is italic, apply regular else if style is italic, do nothing else show an error. So when some found item is regular, the iteration always falls through to the error line.
I now see that I had some operators wrong. In my original script, in lines 8 and 10, replace == with =
Copy link to clipboard
Copied
Hi Peter,
I use again your code and I did the changes you indicate, but have some issues.
Looks for example if I run the script with this text:
ORIGINAL TEXT:
All human beings are born free and equal in dignity and rights STR 94583.
Everyone has the right to life. TRe87 090 liberty and security of person.
AFTER APPLY SCRIPT, THE RESULT IS CORRECT IN THIS CASE:
All human beings are born free and equal in dignity and rights STR 94583.
Everyone has the right to life. TRe87 090 liberty and security of person.
BUT IF FOR ANY REASON THE ORIGINAL TEXT, HAVE ALREADY APPLY SOME FONT STYLE LIKE THIS:
All human beings are born free and equal in dignity and rights STR 94583.
Everyone has the right to life. TRe87 090 liberty and security of person.
AFTER APPLY THE SCRIPT IS INCORRECT:
All human beings are born free and equal in dignity and rights STR 94583.
Everyone has the right to life. TRe87 090 liberty and security of person.
Thanks for help me.
Copy link to clipboard
Copied
Also, Is a bucle, each time you run the script do a different thing
Copy link to clipboard
Copied
Manuel -- The behaviour you see is consistent with the script: it changes regular to italic and italic to regular. The script doesn't check whether the change is necessary. Why not? Because that's not what you asked. You can change the script so that it doesn't make the change if it's not necessary. Maybe by checking the font style of the applied paragraph style. Good one for you to figure out!
Copy link to clipboard
Copied
Hi,
Just for help! …
var myDoc = app.activeDocument,
myCodes = ["STR 94583", "TRe87 090", "MR 840EOA"], C = myCodes.length, c;
app.findTextPreferences = null;
for ( c = 0; c < C; c++) {
app.findTextPreferences.findWhat = myCodes
; myFound = myDoc.findText();
var F = myFound.length, f;
for ( f = 0; f < F; f++) myFound
.appliedParagraphStyle.fontStyle == 'Italic' ? myFound .fontStyle = 'Regular' : myFound .fontStyle = 'Italic'; }
app.findTextPreferences = null;
Best,
Michel, from FRIdNGE
Copy link to clipboard
Copied
Hi Michel,
thanks but didn't work. I am using Indesign CC 2018.
Nothing changes with your script.
Thanks so much.
Copy link to clipboard
Copied
Manuel,
The second loop wasn't so cool I thought! …
This code is easier to read:
var myDoc = app.activeDocument,
myCodes = ["STR 94583", "TRe87 090", "MR 840EOA"], C = myCodes.length, c;
app.findTextPreferences = null;
for ( c = 0; c < C; c++) {
app.findTextPreferences.findWhat = myCodes
; myFound = myDoc.findText();
var F = myFound.length, f;
for ( f = 0; f < F; f++) {
if (myFound
.appliedParagraphStyle.fontStyle == 'Roman') myFound .appliedCharacterStyle = 'Italic'; else if (myFound
.appliedParagraphStyle.fontStyle == 'Italic') myFound .appliedCharacterStyle = 'Roman'; else if (myFound
.appliedParagraphStyle.fontStyle == 'Bold') myFound .appliedCharacterStyle = 'Bold Italic'; else if (myFound
.appliedParagraphStyle.fontStyle == 'Bold Italic') myFound .appliedCharacterStyle = 'Bold'; }
}
app.findTextPreferences = null;
The code checks the font style used by the para style applied to the para and applies the "contrary" char style as:
Roman => Italic,
Italic => Roman,
Bold => Bold Italic,
Bold Italic => Bold.
Best,
Michel
Copy link to clipboard
Copied
However, note that the first code given works for me and this variant too!
… If the game is limited to a "roman (or regular) vs. italic" fight!
var myDoc = app.activeDocument,
myCodes = ["STR 94583", "TRe87 090", "MR 840EOA"], C = myCodes.length, c;
app.findTextPreferences = null;
for ( c = 0; c < C; c++) {
app.findTextPreferences.findWhat = myCodes
; myFound = myDoc.findText();
var F = myFound.length, f;
for ( f = 0; f < F; f++) myFound
.appliedParagraphStyle.fontStyle == 'Roman' ? myFound .appliedCharacterStyle = 'Italic' : myFound .appliedCharacterStyle = 'Roman'; }
app.findTextPreferences = null;
Best,
Michel
Copy link to clipboard
Copied
Hi Michel!
thanks for help me. I let you know I get a Error, when I apply Both Scripts.
But, is very IMPORTANT, the script can know when the codes have already applied a styles. I let you know because is very common to receive text with a mistakes.
For example sometimes the translators give a document and some codes are in italic, and sometimes in Roman, because they do a mistakes, and apply manually the styles and other times they forget apply the right style. Like you can see here in this text the translators apply the right Italic in the first code STR 94583 but they forget apply in the second STR 94583 if the script is not smarty won't be work:
All human beings are born free and equal in dignity and rights. (STR 94583) They are endowed with reason and conscience and should act towards one another in a spirit of brotherhood STR 94583.
Everyone has the right to life, (TRe87 090) liberty and security of person.
Please see the images for more information. I use the first code because is more clear.
Thank you very much
Copy link to clipboard
Copied
Hi,
First, the scripts don't take in account how are styled the occurrences found.
they check their para style applied to the para where the occurrence is found. So the errors will be corrected.
Then, I've used a font in the 2 last scripts that has no "Regular" font style, but "Roman".
Change this word and, normally, they could work! …
Best,
Michel
Copy link to clipboard
Copied
Hi Michel!
sorry for the mistake, I just add the right character style name.
But If you try to run on the paragraph with Italic, like this, you will see the code didn't change, and need also change to Roman:
Everyone has the right to life, (STR 94583) liberty and security of person.
thanks so much!
Copy link to clipboard
Copied
Well! …
Javascript seems to have some problems with "Regular/Roman" font styles! …
This works better:
1/ including a reset,
2/ placing the treatment of the problem in an "else" at the end of the "if" chain.
var myDoc = app.activeDocument,
myCodes = ["STR 94583", "TRe87 090", "MR 840EOA"], C = myCodes.length, c;
app.findTextPreferences = null;
for ( c = 0; c < C; c++) {
app.findTextPreferences.findWhat = myCodes
; myFound = myDoc.findText();
var F = myFound.length, f;
for ( f = 0; f < F; f++) {
myFound
.appliedCharacterStyle = myDoc.characterStyles[0]; if ( myFound
.appliedParagraphStyle.appliedFont.fontFamily == 'Avenir' ) { if ( myFound
.appliedParagraphStyle.appliedFont.fontStyleName == 'Oblique' ) myFound .appliedCharacterStyle = 'Avenir Roman -- blue'; else if ( myFound
.appliedParagraphStyle.appliedFont.fontStyleName == 'Black' ) myFound .appliedCharacterStyle = 'Avenir Black Oblique -- blue'; else if ( myFound
.appliedParagraphStyle.appliedFont.fontStyleName == 'Black Oblique' ) myFound .appliedCharacterStyle = 'Avenir Black -- blue'; else myFound
.appliedCharacterStyle = 'Avenir Oblique -- blue'; }
if ( myFound
.appliedParagraphStyle.appliedFont.fontFamily == 'Arial' ) { if ( myFound
.appliedParagraphStyle.appliedFont.fontStyleName == 'Italic' ) myFound .appliedCharacterStyle = 'Arial Regular -- red'; else if ( myFound
.appliedParagraphStyle.appliedFont.fontStyleName == 'Bold' ) myFound .appliedCharacterStyle = 'Arial Bold Italic -- red'; else if ( myFound
.appliedParagraphStyle.appliedFont.fontStyleName == 'Bold Italic' ) myFound .appliedCharacterStyle = 'Arial Bold -- red'; else myFound
.appliedCharacterStyle = 'Arial Italic -- red'; }
}
}
Copy link to clipboard
Copied
Hi Michel,
wow! looks pretty nice. But I was testing deeply, and I find one issue.
For example, sometimes the beginners Editors when they need apply the italics to a paragraph P_Avenir_Roman they use directly the Character Style Avenir_Oblique -- blue instead of the Paragraph Style P_Avenir_Oblique. In this case when the Script run, we get a error, because the code will appear with Avenir_Oblique -- blue instead of the Avenir_Roman -- blue.
Look before and after run the script:
I expend hours to fix, but Is not working
if ( myFound
.appliedParagraphStyle.appliedFont.fontStyleName == 'Roman' && myFound .appliedCharacterStyle.item == 'Avenir Oblique -- blue'){
Big thank You!