Copy link to clipboard
Copied
Hello,
I have different text fields and I would like to set a different font size depending on the length of the content / line.
Example
Chocolate cake - 14 characters -> format X
Cake - 4 characters -> format Y
So far I have only managed to do this for individual words with:
\b[\u\l]{4,4}\b
The query should contain all characters in a line. For example:
Chocolate cake 132
Copy link to clipboard
Copied
Can you confirm - Line or Paragraph?
Even if you mean Lines - you could use my tool - but it's PC only...
Copy link to clipboard
Copied
Ok, my first try was ^[\u\l\d ]{5,5}$ , but it dint accept -. Now I am trying ^.{5,5}$
Copy link to clipboard
Copied
Ok, my first try was ^[\u\l\d ]{5,5}$ , but it dint accept -. Now I am trying ^.{5,5}$
By @Henri36655125at8n
But you still have to do it manually for each length...
Here is my take - using my tool:
First step - loading info about text(s).
Then there are two Tasks:
1st - run in BatchMode - executed on every row of the bottom list - will prepare names for ParaStyles, based on the length of each Paragraph - you don't even have to create those styles - they will be created by 2nd Task "on demand",
2nd - also run in BatchMode - will apply ParaStyle based on the contents of the "EXTRA 1" column - ParaSyles will be created if they don't exist - top-right corner of the screenshot - all those styles has been created automatically.
Of course you can filter out longer or shorter results, from specific pages/layers/X&Y location/with already applied particular ParaStyle/etc.
You can also sort any way you want - but in this case it doesn't matter.
Copy link to clipboard
Copied
GREP styles in the paragraph that looks for paragraphs with only 4 characters and applys one character style, Paragraphs longer than 13 characters apply another character style. Just sequence the grep styles from shortest to longest for this example.
Copy link to clipboard
Copied
Correcting myself.
Second grep statement could be simplified to: ^.{14,} (for 14 or more chaaracters)
Copy link to clipboard
Copied
GREP styles in the paragraph that looks for paragraphs with only 4 characters and applys one character style, Paragraphs longer than 13 characters apply another character style. Just sequence the grep styles from shortest to longest for this example.
By @Tim Sheasby
But OP doesn't want ranges.
And creating by hand 30 or 50 styles would be a bit of an overkill... Yes, you could use INDT, but 50 GREP styles in the ParaStyle definition...
And if you plan on using script to create all those styles - you can create a dedicated script that will go through Paragraphs collection and style accordingly.
Copy link to clipboard
Copied
Hi @Henri36655125at8n, because you tagged "scripting" here is a scripting approach. This is a first draft, because we don't know exactly what you want, eg. do you want *only* paragraphs with specific numbers of characters changed, like 4 and 20, or do you want every paragraph with 4 or more, or 20 or less, or ... ? My example script assumes the latter, which is probably wrong, but it's a starting point.
You configure the script in two ways: (1) by specifying a "base style name", eg. "Size Me" in my demo doc, and (2) by creating style variants for that style with the number at the end delimited by an underscore, eg. "Size Me_14" in my demo doc.
To see it working, you can open the attached demo.indd, which has the styles created.
- Mark
/**
* Set paragraph styles based on number of characters in paragraph.
* @author m1b
* @discussion https://community.adobe.com/t5/indesign-discussions/help-with-grep-apply-format-for-line-with-x-characters/m-p/14548751
*/
function main() {
var doc = app.activeDocument;
setStylesByParagraphLength(doc, 'Size Me')
};
app.doScript(main, ScriptLanguage.JAVASCRIPT, undefined, UndoModes.ENTIRE_SCRIPT, 'Set Styles By Contents Length');
/**
* Finds all paragraphs in `doc` that have a paragraph style
* applied that has "size variants", and applies the
* variant style based on the number of characters
* in the paragraph.
*
* Example variants:
* Size Me - the base style
* Size Me_4 - apply when paragraph length >= 4
* Size Me_10 - apply when paragraph length >= 10
* Size Me_15 - apply when paragraph length >= 15
* ...
*
* The nearest lower style is applied, so a paragraph
* with 20 characters will get the Size Me_15 style,
* and a paragraph with 14 characters will get the
* Size Me_10 style.
* @author m1b
* @version 2024-04-11
* @param {Document} doc - an Indesign Document.
* @param {String} baseStyleName - the base style name.
*/
function setStylesByParagraphLength(doc, baseStyleName) {
var styles = doc.allParagraphStyles;
// the base style is the one with no "variant number"
var baseStyle = getThing(styles, 'name', baseStyleName);
if (undefined == baseStyle)
return alert('Could not find paragraph style "' + baseStyleName + '".');
// store variant styles in array indexed by their variant number
var re = new RegExp('^' + baseStyleName + '_(\\d+)$'),
numberedStyleVariants = [baseStyle];
for (var i = 0, match, n = styles.length; i < n; i++) {
match = styles[i].name.match(re);
if (
null === match
|| match.length < 2
)
continue;
numberedStyleVariants[Number(match[1])] = styles[i];
}
if (0 === numberedStyleVariants.length)
return alert('No numbered variants of "' + baseStyleName + '" were found, eg. "' + baseStyleName + '_14".');
// find paragraphs set in the base style
app.findGrepPreferences = NothingEnum.NOTHING;
app.findGrepPreferences.appliedParagraphStyle = baseStyle;
var found = doc.stories.everyItem().paragraphs.everyItem().findGrep();
foundLoop:
for (var i = found.length - 1, n, para; i >= 0; i--) {
paragraphsLoop:
for (var j = found[i].length - 1; j >= 0; j--) {
para = found[i][j];
// don't include the carriage-return
n = para.length + ('\r' === para.characters[-1].contents ? 0 : 1);
// apply the style variant number `n` (or next lowest)
while (n--) {
if (undefined != numberedStyleVariants[n]) {
para.appliedParagraphStyle = numberedStyleVariants[n];
continue paragraphsLoop;
}
}
}
}
};
/**
* Returns a thing with matching property.
* @param {Array|collection} things - the things to look through, eg. PageItems.
* @param {String} key - the property name, eg. 'name'.
* @param {*} value - the value to match.
* @returns {*} - the thing.
*/
function getThing(things, key, value) {
for (var i = 0; i < things.length; i++)
if (things[i][key] == value)
return things[i];
};
Edit: oops, forgot to add demo.indd.
Copy link to clipboard
Copied
Copy link to clipboard
Copied
> Why can't you create styles "on demand"??
No reason. This is just one approach. This way a user can set up different rules on the fly for different indesign documents, without editing the script at all.
I mean, maybe OP doesn't even want styles... in which case the script could just override the text without changing the style at all.