Highlighted

InDesign Script - find/replace - store in a variable what is in "find what", "find format"

New Here ,
Mar 03, 2020

Copy link to clipboard

Copied

Hello,
I need to write a script which detects if there is anything and store whatever is there in one or more variables the following:
- the grep expression from "find what",
- the format from "find format",
- the grep expression from "change to" and
- the format from "change format".

 

I need to restore the grep expressions inside "find what" and "change to" and their formats after a reset grep preferences.
I did it for grep expression, but I don't imagine how to do this trick for the formats.

Thanks!

Adobe Community Professional
Correct answer by Peter_Kahrel | Adobe Community Professional

You can't actually do it like that, you need to use the properties:

 

var currFindPrefs = app.findGrepPreferences.properties;

var currChangePrefs = app.changeGrepPreferences.properties; 

 

Then, when you need to change back to what was there:

 

app.findGrepPreferences.properties = currFindPrefs;

app.changeGrepPreferences.properties = currChangePrefs;

 

P.

Topics

Scripting

Views

500

Likes

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

InDesign Script - find/replace - store in a variable what is in "find what", "find format"

New Here ,
Mar 03, 2020

Copy link to clipboard

Copied

Hello,
I need to write a script which detects if there is anything and store whatever is there in one or more variables the following:
- the grep expression from "find what",
- the format from "find format",
- the grep expression from "change to" and
- the format from "change format".

 

I need to restore the grep expressions inside "find what" and "change to" and their formats after a reset grep preferences.
I did it for grep expression, but I don't imagine how to do this trick for the formats.

Thanks!

Adobe Community Professional
Correct answer by Peter_Kahrel | Adobe Community Professional

You can't actually do it like that, you need to use the properties:

 

var currFindPrefs = app.findGrepPreferences.properties;

var currChangePrefs = app.changeGrepPreferences.properties; 

 

Then, when you need to change back to what was there:

 

app.findGrepPreferences.properties = currFindPrefs;

app.changeGrepPreferences.properties = currChangePrefs;

 

P.

Topics

Scripting

Views

501

Likes

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
Adobe Community Professional ,
Mar 03, 2020

Copy link to clipboard

Copied

There are lots of different properties that can be set under the Find/Change Format portion. To get them all, you would do: 

 

var currFindPrefs = app.findGrepPreferences;

var currChangePrefs = app.changeGrepPreferences; 

 

Then, when you need to change back to what was there:

 

app.findGrepPreferences = currFindPrefs;

app.changeGrepPreferences = currChangePrefs;

 

This would include the find and change expressions under app.findGrepPreferences.findWhat and app.changeGrepPreferences.changeTo

 

If you want to wipe everything out, you do: 

 

app.findGrepPreferences = NothingEnum.NOTHING;

app.changeGrepPreferences = NothingEnum.NOTHING;

 

Likes

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
Reply
Loading...
Adobe Community Professional ,
Mar 04, 2020

Copy link to clipboard

Copied

You can't actually do it like that, you need to use the properties:

 

var currFindPrefs = app.findGrepPreferences.properties;

var currChangePrefs = app.changeGrepPreferences.properties; 

 

Then, when you need to change back to what was there:

 

app.findGrepPreferences.properties = currFindPrefs;

app.changeGrepPreferences.properties = currChangePrefs;

 

P.

Likes

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
Reply
Loading...
New Here ,
Mar 04, 2020

Copy link to clipboard

Copied

Thank you, guys!

Very much!

Only the solution of Peter works.

 

Likes

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
Reply
Loading...
Adobe Community Professional ,
Mar 04, 2020

Copy link to clipboard

Copied

Had that feeling I was forgetting something. Glad it worked out. 

Likes

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
Reply
Loading...
New Here ,
Mar 13, 2020

Copy link to clipboard

Copied

Hello!
I had to come back.

The solution from Peter works only at half, if I can say so.

Doesn't works for paragraph (character) styles in style groups.

The code is the one from Peter:

var currFindPrefs = app.findGrepPreferences.properties; 
var currChangePrefs = app.changeGrepPreferences.properties; 
ResetGrepPref();
app.findGrepPreferences.properties = currFindPrefs; 
app.changeGrepPreferences.properties = currChangePrefs;
function ResetGrepPref(){
	app.findGrepPreferences = NothingEnum.nothing; 	
	app.changeGrepPreferences = NothingEnum.nothing; 
}

If I choose to use a paragraph (character) style from a group style (or any combination of them),  it can not be restored in the "find format" or "change format".

Is there a way to be restored?

Only ones that are outside of style groups can be restored.

I did screenshots before and after run the script.

Check the images below.

Please, help me again.

Thank you.

 before.png

after.png

 

 

 

 

Likes

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
Reply
Loading...
Adobe Community Professional ,
Mar 17, 2020

Copy link to clipboard

Copied

You're out of luck: turns out that there's no way to record the identity of a style inside a group. Not by .properties, but also not by recordint it manually, so to speak. I thought something like this should be possible:

var currFindPrefs = app.findGrepPreferences.properties;
var parStyleId = app.findGrepPreferences.appliedParagraphStyle.id;

And later, to restore the dialog's state:

app.findGrepPreferences.properties = currFindPrefs;
app.findGrepPreferences.appliedParagraphStyle = app.activeDocument.paragraphStyles.itemByID (parStyleId);

But all this doesn't work. Please log a bug at https://indesign.uservoice.com

 

P

Likes

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
Reply
Loading...
New Here ,
Mar 17, 2020

Copy link to clipboard

Copied

Peter, thank you again very very much.
You spare me to waste time for searching a trick.
I did logged a bug (I hope I did it correct).
 

Likes

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
Reply
Loading...
Adobe Community Professional ,
Mar 17, 2020

Copy link to clipboard

Copied

Hi Peter,

indeed, this here:

 

app.findGrepPreferences.appliedParagraphStyle

 

returns a string . The name of a paragraph style or an empty string in case the style was not specified.

The name will not indicate if the style is part of a style group.

 

The following line is not provoking an error directly:

 

app.findGrepPreferences.appliedParagraphStyle.id

 

 

To get an error message like "undefined is not an object" you need to look further:

 

app.findGrepPreferences.appliedParagraphStyle.id.constructor.name

 

 

So how could we test for the style itself and not only for a name?

First we can tell if an empty string is returned with appliedParagraphStyle.

If not one could loop the allParagraphStyles array of a document and see into property name to find the right style, but one cannot be sure if two styles share the same name or if two or more open documents share the same style names with different properties. Even if you have only one document open and the returned name is unique you cannot be sure if that style is the right one, because the inital target document was perhaps closed by the user recently.

 

My suggestion without testing anything:

Loop the allParagraphStyles array of a given document. If a match of the name is found, add a new text frame with contents that the current GREP pattern definitely will find, format the text with the matching style. My assumption is: If then findGrep() is successful on that text you hit the right style. Do that for every open document, not only for the active one.

 

Preserve the matching text. And later, after doing a different grepFind() with different parameters, select that preserved text and use it as basis for defining appliedParagraphStyle with app.findGrepPreferences:

 

app.findGrepPreferences.appliedParagraphStyle =
app.selection[0].appliedParagraphStyle;

 

 

Regards,
Uwe Laubender

( ACP )

Likes

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
Reply
Loading...
Adobe Community Professional ,
Mar 18, 2020

Copy link to clipboard

Copied

Ha ha! You're right, it returns a string. How awful. With the kind of workaround you outline I would just forget about restoring the dialog and tell the customer (or myself) 'Sorry, duckie, won't work.'

 

P.

Likes

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
Reply
Loading...
Adobe Community Professional ,
Mar 19, 2020

Copy link to clipboard

Copied

Yes, Peter!

Of course I'd do that as well 🙂

Too much effort to restore the dialog.

 

However, we could debate:

Should the architecture of app.findGrepPreferences.appliedParagraphStyle or app.findGrepPreferences.appliedCharacterStyle be changed?

 

Regards,
Uwe Laubender

( ACP )

Likes

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
Reply
Loading...
Adobe Community Professional ,
Mar 19, 2020

Copy link to clipboard

Copied

This comes up from time to time. Some things don't work with string references, some do. And whether something works with a string reference depends on the platform: Mac and Windows don't show the same behaviour with strings.

 

But object references always work, on both Mac and Windows. So for Adobe to use string reference for anything at all is very bad. All references and bindings should be (to) objects. So

app.findGrepPreferences.appliedParagraphStyle

should return an object.

P.

Likes

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
Reply
Loading...