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

Storing an immutable copy of an internal application object (e.g., FindTextPreference)

Contributor ,
Feb 15, 2025 Feb 15, 2025

I have a script that I often use repeatedly while using the Find/Change dialogue in the UI.

 

The script makes some text substitutions in the document, and because it uses the application’s FindTextPreferences for this, the Find/Change dialogue is also updated in the UI. This is kind of annoying, because then I have to reset it and enter the search criteria I was using every time the script is run.

 

I figured this would be easy to fix; just do this:

 

 

var initialPreferences = app.findTextPreferences;

app.findTextPreferences = NothingEnum.NOTHING;
// do rest of stuff here

app.findTextPreferences = initialPreferences;

 

 

Unfortunately, that doesn’t work – the variable value appears to be assigned by reference, so every time I change anything app.findTextPreferences, the variable is also updated. The FindTextPreference class doesn’t have duplicate() method.

 

Is there a way to store an immutable ‘snapshot’ of the preferences that I can then use to reset the Find/Change dialogue’s state when the script is done?

 

Moreover, is it even possible to assign the preferences like that? The API docs indicate that you can (findTextPreferences is listed as read/write, expecting a FindTextPreference object or NothingEnum.NOTHING), but when I try to assign it, I get an error 30477:

 

> Invalid value for set property 'findTextPreferences'. Expected NothingEnum enumerator, but received FindTextPreference.

 

So is it not even possible to set it like that?

TOPICS
Scripting
498
Translate
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 2 Correct answers

Contributor , Feb 15, 2025 Feb 15, 2025

And of course the answer comes to me five minutes after posting: just use the `properties` property!

 

 

var initialPreferences = app.findTextPreferences.properties;

app.findTextPreferences = NothingEnum.NOTHING;
// do rest of stuff here

app.findTextPreferences.properties = initialPreferences;

 

 

So simple. 🤦🏼‍

Translate
People's Champ , Feb 15, 2025 Feb 15, 2025

.properties will catch most stuff, but it's only a shallow copy. For those properties that are themselves objects, IIRC it won't work.

FWIW, the way I deal with it is this https://www.id-extras.com/tidy-up-after-yourself/

(written 11 years ago, but still works!)

Translate
Contributor ,
Feb 15, 2025 Feb 15, 2025

And of course the answer comes to me five minutes after posting: just use the `properties` property!

 

 

var initialPreferences = app.findTextPreferences.properties;

app.findTextPreferences = NothingEnum.NOTHING;
// do rest of stuff here

app.findTextPreferences.properties = initialPreferences;

 

 

So simple. 🤦🏼‍

Translate
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
People's Champ ,
Feb 15, 2025 Feb 15, 2025

.properties will catch most stuff, but it's only a shallow copy. For those properties that are themselves objects, IIRC it won't work.

FWIW, the way I deal with it is this https://www.id-extras.com/tidy-up-after-yourself/

(written 11 years ago, but still works!)

Translate
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
Contributor ,
Feb 15, 2025 Feb 15, 2025

Ahaaa! There’s a whole method for doing just that – I would never have found that (because I wasn’t expecting it to exist). That sounds a lot more reliable. Thank you!

Translate
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
Community Expert ,
Feb 15, 2025 Feb 15, 2025

Thanks @TᴀW, I didn't know about those methods. Nice!

- Mark

Translate
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
LEGEND ,
Feb 15, 2025 Feb 15, 2025
Translate
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
Mentor ,
Feb 17, 2025 Feb 17, 2025
LATEST

🤔

For completeness sake, I think this does not preserve the previous user choice of the "Query" dropdown …

Looking at the OM, I haven't found a way to get that value.

 

Translate
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