Copy link to clipboard
Copied
Hi All,
Here's the translation in English:
"The AI has failed, so I'm turning to the HI for a small script: in an open document, I want to copy four digits that already have a character style called 'red' located anywhere within a paragraph (except at the beginning), and paste them at the beginning of each paragraph."
Thanks for your help
A tiny change does it.
/**
* Repeat 4 digit codes at start of paragraphs.
* Codes are identified by their character style.
* @author m1b
* @discussion https://community.adobe.com/t5/indesign-discussions/copy-paste-4-digits-at-the-beginning-of-a-paragraph/m-p/13773037
*/
function main() {
var settings = {
characterStyleName: 'red',
afterCode: ' ',
ignoreParagraph: /^\d{4}/,
};
var doc = app.activeDocument;
app.findGrepPreferences = NothingEnum.NOTHI
...
Copy link to clipboard
Copied
Can you share some screenshots to explain the problem statement
-Manan
Copy link to clipboard
Copied
Sure
Copy link to clipboard
Copied
Hi @tournierlaurent, this is what I had in mind. Does this do what you need? You can adjust the settings if you like. It's quite straightforward. The only little fiddle is telling script to ignore a paragraph that already starts with 4 digits (see the settings.ignoreParagraph RegExp). Hope it works for you.
- Mark
/**
* Repeat 4 digit codes at start of paragraphs.
* Codes are identified by their character style.
* @author m1b
* @discussion https://community.adobe.com/t5/indesign-discussions/copy-paste-4-digits-at-the-beginning-of-a-paragraph/m-p/13773037
*/
function main() {
var settings = {
characterStyleName: 'red',
afterCode: ' ',
ignoreParagraph: /^\d{4}/,
};
var doc = app.activeDocument;
if (
doc.selection.length == 0
|| app.selection[0].findGrep == undefined
) {
alert('Please make a selection and try again.')
return;
}
app.findGrepPreferences = NothingEnum.NOTHING;
app.changeGrepPreferences = NothingEnum.NOTHING;
app.findGrepPreferences.appliedCharacterStyle = settings.characterStyleName;
var found = doc.selection[0].findGrep();
for (var i = found.length - 1; i >= 0; i--) {
if (settings.ignoreParagraph.test(found[i].paragraphs[0].contents))
continue;
found[i].paragraphs[0].insertionPoints[0].contents = found[i].texts[0].contents + settings.afterCode;
}
}
app.doScript(main, ScriptLanguage.JAVASCRIPT, undefined, UndoModes.ENTIRE_SCRIPT, 'Add Codes To Start');
Copy link to clipboard
Copied
Hi m1b
Sorry, I hadn't seen your solution. It works perfectly.
Copy link to clipboard
Copied
I just have one last request: how can the script be adapted to work on the entire open document, without needing to select a story?
Copy link to clipboard
Copied
A tiny change does it.
/**
* Repeat 4 digit codes at start of paragraphs.
* Codes are identified by their character style.
* @author m1b
* @discussion https://community.adobe.com/t5/indesign-discussions/copy-paste-4-digits-at-the-beginning-of-a-paragraph/m-p/13773037
*/
function main() {
var settings = {
characterStyleName: 'red',
afterCode: ' ',
ignoreParagraph: /^\d{4}/,
};
var doc = app.activeDocument;
app.findGrepPreferences = NothingEnum.NOTHING;
app.changeGrepPreferences = NothingEnum.NOTHING;
app.findGrepPreferences.appliedCharacterStyle = settings.characterStyleName;
var found = doc.findGrep();
for (var i = found.length - 1; i >= 0; i--) {
if (settings.ignoreParagraph.test(found[i].paragraphs[0].contents))
continue;
found[i].paragraphs[0].insertionPoints[0].contents = found[i].texts[0].contents + settings.afterCode;
}
}
app.doScript(main, ScriptLanguage.JAVASCRIPT, undefined, UndoModes.ENTIRE_SCRIPT, 'Add Codes To Start');
Copy link to clipboard
Copied
Thanks a lot