• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Select text based on paragraph style selected in Paragraph styles panel

Guru ,
May 28, 2018 May 28, 2018

Copy link to clipboard

Copied

I need a script that will find the first instance of text formatted with the paragraph style selected in the Paragraph Styles panel.

I found an answer from Jump_Over (Jarek) from 2013 that referenced this (https://forums.adobe.com/message/5469994#5469994  😞

if text tool is inactive use:

app.activeDocument.textDefaults.appliedParagraphStyle; 

otherwise:

app.selection[0].appliedParagraphStyle;

But not being a scripter I have no idea what to do next.

I tried running that one line of the script

script question 1.png

but got a Syntax Error

Script question.png

I hate the idea of asking you to write something for me without compensation, but it's only for teaching purposes and I will give credit to any and all who help.

Thanks,

Sandee

TOPICS
Scripting

Views

3.2K

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , May 28, 2018 May 28, 2018

No worries – we'll make you into a Script-writer yet!

First off: Jarek's snippet uses JavaScript syntax, and from your screenshot I can determine you entered it into an AppleScript editor … For JavaScript, you need Adobe's ExtendScript Toolkit (ESTK) Editor – or any other plain text editor you are comfortable with.

Jarek's snippet returns the current applied paragraph style, but it does not do anything else with that. It's the same as entering

42

into a calculator; i.e., "thanks for letting me know

...

Votes

Translate

Translate
Community Expert ,
May 28, 2018 May 28, 2018

Copy link to clipboard

Copied

No worries – we'll make you into a Script-writer yet!

First off: Jarek's snippet uses JavaScript syntax, and from your screenshot I can determine you entered it into an AppleScript editor … For JavaScript, you need Adobe's ExtendScript Toolkit (ESTK) Editor – or any other plain text editor you are comfortable with.

Jarek's snippet returns the current applied paragraph style, but it does not do anything else with that. It's the same as entering

42

into a calculator; i.e., "thanks for letting me know that, and what now?" A more complete code snippet would be this:

/* check if the text cursor is inside some text first */

if (app.selection.length == 1 && app.selection[0].hasOwnProperty("baseline"))

alert (app.activeDocument.selection[0].appliedParagraphStyle.name);

else

alert (app.activeDocument.textDefaults.appliedParagraphStyle.name);

which, when run, will display the name of the current selected style in an alert box. Line 01 is just a comment; line 02 checks if the text cursor is clicked somewhere inside a story. A text "selection" is always exactly one item, as you cannot (yet) select more than one text item at a time. The text selection itself is always a single object -- the 'position' of the cursor --, and it does not matter if you actually selected some text or not. Just imagine the blinking cursor as having selected exactly zero characters. (Which is precisely what it does.)

Having "1" item selected is not all, because you can also select one rectangle, one table cell, or one image, so the next step is to verify if what you have selected is actually a 'text item'. There are lots of different text items, ranging from individual characters, and even the insertion points in between them, all the way up to words, lines, paragraphs, and entire stories – but they all have properties in common. I think it was Dave Saunders who proposed to test for "baseline"; I have been using this for years, and it never failed me.

With the selection worked out, you can access its applied paragraph style, and, as above, display interesting stuff such as its name. You can also "paste" this style name into the Find What formatting field – in a script, that would be a simple assignment. Next step is to search for the "find what" formatting, and if it finds anything, select and zoom in on it. This is what that would look like:

/* check if the text cursor is inside some text first */
if (app.selection.length == 1 && app.selection[0].hasOwnProperty("baseline"))
style = app.activeDocument.selection[0].appliedParagraphStyle;
else
style = app.activeDocument.textDefaults.appliedParagraphStyle;

/* clear out any possible previous Find text and formatting */
app.findTextPreferences = null;

/* set the paragraph style */
app.findTextPreferences.appliedParagraphStyle = style;

/* go! */
list = app.activeDocument.findText();

/* test if one or more items are found */
if (list.length > 0)
{
list[0].select();
app.activeWindow.zoomPercentage = app.activeWindow.zoomPercentage;
} else
{
alert ('style '+style.name+' not found');
}

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
May 28, 2018 May 28, 2018

Copy link to clipboard

Copied

Hi Theunis,

Some weeks ago, I've shared [totally for free!] a scripts set about this kind of game! …

InDesign Secrets Public Group | Facebook

I prefer Grep!

var style = app.activeDocument.textDefaults.appliedParagraphStyle; 

app.findGrepPreferences = null;

app.findGrepPreferences.findWhat = "^[^\\r]+\\r?";

app.findGrepPreferences.appliedParagraphStyle = style;

list = app.activeDocument.findGrep(); 

if ( list.length > 0 ) list[0].select(); 

else alert( 'style ' + style.name + ' not found' ); 

app.findGrepPreferences = null;

Best,

Michel, from FRIdNGE

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guru ,
May 29, 2018 May 29, 2018

Copy link to clipboard

Copied

LATEST

Hooray! It works!

This is going to help me enormously when modifying paragraph styles. This way I can jump to an instance of that style without having to go to Find/Change which is very cumbersome.

The Adobe team should consider creating this command for the Paragraph Styles panel.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines