How to Change a String in a Plist File?

Hey Hans,

The red action is disabled...

-Chris

Hey Chris,

That’s a little confusing indeed. I disabled it since it causes an error. Instead, I have added the green actions.

But getting the red action to work would be optimal.

Best,
Hans

1 Like

So what you'll have to do is read the "parent", find out the value of the key, then use that in the Set command.

Then the app is stupid :wink: The whole point of key/value pairs is that they can be treated as an unordered collection and referenced by key rather than position!

Getting down to the nitty-gritty -- why are you trying to do this, and why are you changing preferences so often that you need a macro (complete with variables). Bonus question -- why hasn't the app got its own damn plist, instead of hijacking Apple's?!

2 Likes

I will try to do that.

I understand what you say and I agree with your conclusion. To give the developer some slack: the app is very impressive and good. He supports 3 operating systems and has to do everything on his one. Quite an achievement.

To begin with your last question: you are absolutely right!

Background of my questions:

I can't stand it to repeat the same stupid clicks over and over, e.g. to create a new project with all desired resources.

Like Chris said (paraphrasing here): If you have to repeat the same steps several times a day, you should automate it.

It's the "create a new project with all desired resources" that does the trick. Usually when I see this kind of "multiple preferences" scenario it's because people want to swap between 2, 3, or many presets -- it would be quicker to hand-craft multiple plists and then have a macro/script that copied the appropriate one into the Preferences folder. But since you want to create these (and other resources) on the fly it makes sense to continue down the PlistBuddy route.

Is the templatesMap dictionary you posted above, with a single key/value pair, the one you want to change? Or does the real thing have more pairs? If more, will the one you want to change always be the first (you may have to try setting some values and reading them back, to make sure it remains first whatever operation you perform)? Or are any other pairs unchanging across versions of your plist?

Yes, I was thinking along these lines: create a very basic and clean plist and copy this every time to the prefs folder.

[quote="Nige_S, post:25, topic:12648"]Is the templatesMap dictionary you posted above, with a single key/value pair, the one you want to change? Or does the real thing have more pairs? If more, will the one you want to change always be the first (you may have to try setting some values and reading them back, to make sure it remains first whatever operation you perform)? Or are any other pairs unchanging across versions of your plist?
[/quote]

I have to do further investigations. However, note that I'll be probably copying a clean plist on every run of the macro.

BTW: The best thing would be if the developer would listen once more to my good ideas and implement the project management ideas himself :stuck_out_tongue_winking_eye:

If it is always the first key/value pair in the dictionary, it's easy enough to extract using simple *nix utilities chained together. You know that, for templatesMap, PlistBuddy will return

Dict {
    the_key_to_find = New abc_pt.xml
    Goggomobil.xlf = test_pt.xml
    Maybe_more_keys = and_more_values
}

...and you want to find "everything on the second line that's before the =, and without the spaces either end".

An easy way to get line 2 is to head the first 2 lines then tail 1.
An easy way to get everything up to = is to cut on = and get the first field.
An easy way to trim beginning and end spaces but keep any that are between words (because you never know :wink: ) is to pass the string to xargs.

Putting that all together:

myVar=$( /usr/libexec/PlistBuddy -c "Print :/:translator/:templatesMap/" ~/Desktop/Archive/com.apple.java.util.prefs\ copy.plist | head -2 | tail -1 | cut -d = -f 1 | xargs )

...which, for the above example, will set $myVar to the_key_to_find -- you can then use $myVar later in the same "Execute Shell Script" action to delete the pair, ready to add your new key/value.

There's probably "better" ways to do this using some funky regex pattern in sed or something, but IMO this way is easy to understand, easy to maintain, and isn't too inefficient!

1 Like