Skip to main content
Participant
March 31, 2023
Answered

Script to place an icon at the beginning of a paragraph

  • March 31, 2023
  • 3 replies
  • 1535 views

I'm wondering if anyone can help me with a little script ...

 

I have reocurring documents that consist of several pages. In a document I need to place icons at the beginning of certain paragraphs, e.g. I need to place Icon-A at the beginning of Para-style-A, Icon-B at the beginning of Para-style-B, and Icon-C at the beginning of Para-style-C. I can do this no problem using find/change and GREP, e.g. copying Icon-A to the pasteboard and running a command to find the beginning of Para-style-A and paste the icon. Then repeating the process for the other icons/paragraphs.

 

Is it at all possible that a script could add all the icons to the relevant paragraph styles?

 

Any help is much appreciated.

This topic has been closed for replies.
Correct answer FRIdNGE

I'm just answering to the op's question [without digression]!

 

Take care to give the same name to the para styles and the icon applied object styles to target.  😉

 

 

 

The op just needs to Select All The Icons On Pasteboard and then play the Script below:

 

// by FRIdNGE, Michel Allio [01/04/2023]

// Select All The Icons On Pasteboard [Including Corresponding Object Style (same name as para style)]:
var mySel = app.selection;
for ( var s = 0; s < mySel.length ; s++ ) {
  app.copy(app.select(mySel[s]));
  app.findGrepPreferences = app.changeGrepPreferences = null;
  app.findGrepPreferences.appliedParagraphStyle = app.activeDocument.paragraphStyles.item(mySel[s].appliedObjectStyle.name);
  app.findGrepPreferences.findWhat = "^~a?(.)";
  app.changeGrepPreferences.changeTo = "~c$1";
  app.activeDocument.changeGrep();
}
app.findGrepPreferences = app.changeGrepPreferences = null;
app.selection = null;
alert("Done By FRIdNGE! …")

 

(^/)  The Jedi

3 replies

FRIdNGE
FRIdNGECorrect answer
April 1, 2023

I'm just answering to the op's question [without digression]!

 

Take care to give the same name to the para styles and the icon applied object styles to target.  😉

 

 

 

The op just needs to Select All The Icons On Pasteboard and then play the Script below:

 

// by FRIdNGE, Michel Allio [01/04/2023]

// Select All The Icons On Pasteboard [Including Corresponding Object Style (same name as para style)]:
var mySel = app.selection;
for ( var s = 0; s < mySel.length ; s++ ) {
  app.copy(app.select(mySel[s]));
  app.findGrepPreferences = app.changeGrepPreferences = null;
  app.findGrepPreferences.appliedParagraphStyle = app.activeDocument.paragraphStyles.item(mySel[s].appliedObjectStyle.name);
  app.findGrepPreferences.findWhat = "^~a?(.)";
  app.changeGrepPreferences.changeTo = "~c$1";
  app.activeDocument.changeGrep();
}
app.findGrepPreferences = app.changeGrepPreferences = null;
app.selection = null;
alert("Done By FRIdNGE! …")

 

(^/)  The Jedi

JahrodAuthor
Participant
April 1, 2023

@FRIdNGE That is EXACTLY what I was after! Amazing. Thank you so much!

 

At first I had an error when running the script but I realised it was due to my para styles being within a style group. Would it be possible for you to modify the script so that it can find the para styles in a group?

 

Thank you again. Genius stuff 🙂

JahrodAuthor
Participant
April 2, 2023

The place of the relevant Object Styles is without interest. We just need to detect their name (= para style name).

 

BUT, you're right, you need to clearly indicate where the Para Styles are placed (my original Script: the first level).

 

Sample (in a third level):

 

 

So:

 

app.findGrepPreferences.appliedParagraphStyle = app.activeDocument.paragraphStyleGroups.item("AAAA").paragraphStyleGroups.item("BBBB").paragraphStyles.item(mySel[s].appliedObjectStyle.name);

 

 


Marvellous! Thank you again @FRIdNGE 🤩

m1b
Community Expert
Community Expert
March 31, 2023

Hi @Jahrod, it's probably only worth writing a script if you have hundreds of these to do. Otherwise it will be pretty fast to just do a find next on the paragraph style and paste the (pre-configured) anchored graphic into the first insertion point. It shouldn't take more than a minute to do 50 like this. As @James Gifford—NitroPress said, the paragraph styles may need to do some formatting—it would be good to see a screenshot of your desired result.

- Mark

 

Edit: Oh! and don't forget to set up an Object Style on the anchored graphic frames.

James Gifford—NitroPress
Legend
March 31, 2023

A script can almost certainly be created, but the fundamental layout can be handled in one of two ways: with a bullet Paragraph Style, probably using a Character Style override on the bullet glyph itself, or by defining a Drop Cap, similarly using a Character style to set the 'icon' font and characteristics. (The third way would be to place the icon glyph or image in a frame, and then anchor it so that it takes the place of a drop cap or large bullet, as desired.)

 

Once set up, all of these methods should be easy and quick to apply. A script may be... superfluous. But the script gurus here will ring in on an actual solution once a layout method is chosen.