Skip to main content
Participant
March 6, 2023
Answered

GREP Styles puzzle: apply character style to the first half of every word

  • March 6, 2023
  • 2 replies
  • 2376 views

I recently ran across the below text sample, apparently produced by bionic-reading.com. In this sample it's simply a matter of bolding the the first 50% (rounded up) of characters in every word, though the site gives other controls.

 

Putting aside whether this styling is actually advantageous, is there a way to implement this (bold first 50% of every word) with a GREP style? Where would you begin? 

This topic has been closed for replies.
Correct answer FRIdNGE

 

(^/)  The Jedi

2 replies

Participant
March 8, 2023

You can do it e.g. with the script below, for all stories in the active document. The script gives the character style 'bold', so you have to define it beforehand.

 

var doc = app.activeDocument;
for (var s = 0; s < doc.stories.length; s++){
	for (var w = 0; w < doc.stories[s].words.length; w++){
		for (var c = 0; c < Math.ceil(doc.stories[s].words[w].length/2); c++){
			doc.stories[s].words[w].characters[c].appliedCharacterStyle = 'bold';
			}
		}
	}

 

Alternatively, for selected text frames, not including overflow text:

var selTextFrames = app.selection;
for (var t = 0; t < selTextFrames.length; t++){
	for (var w = selTextFrames[t].words.length-1; w >=0 ; w--){
		if (selTextFrames[t].words[w].isValid){
			for (var c = 0; c < Math.ceil(selTextFrames[t].words[w].length/2); c++){
				selTextFrames[t].words[w].characters[c].appliedCharacterStyle = 'bold';
				}
			}
		}
	}
Robert at ID-Tasker
Legend
March 8, 2023

Nice. 

 

But instead of inside for() to apply CharStyle - you can get the number of chars - from .ceil() - and use itemByRange - I'm not JS guy and replying from my phone so... 

 

Robert at ID-Tasker
Legend
March 6, 2023

I'm not an expert in GREP - but I think you would need to search for each length separately.

 

Something like this:

 

1st step to "split" words - where "{2}" determines number of letters in each "half":

 

 

Then style upto %:

 

 

Then just remove "%" styled Bold.

 

But I'm sure someone will be able to do it in one or max two steps 😉

 

Or it could be easily scripted.

 

FRIdNGE
FRIdNGECorrect answer
March 6, 2023

 

(^/)  The Jedi

CJ CramerAuthor
Participant
March 6, 2023

Brute force! Sure 🙂