Skip to main content
Janus Bahs Jacquet
Inspiring
February 15, 2025
Answered

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

  • February 15, 2025
  • 2 replies
  • 542 views

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?

Correct answer TᴀW

.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!)

2 replies

Robert at ID-Tasker
Legend
February 16, 2025
Legend
February 17, 2025

🤔 🤔

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.

 

Janus Bahs Jacquet
Inspiring
February 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. 🤦🏼‍

TᴀW
TᴀWCorrect answer
Legend
February 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!)

Janus Bahs Jacquet
Inspiring
February 16, 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!