Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

BollywooD

macrumors 6502
Original poster
Apr 27, 2005
372
46
Hamburg
how do I delete a key in a plist file, in a postinstall script?

I have tried:
Code:
#!/bin/sh

defaults delete $HOME/Library/Preferences/com.apple.Safari myKeyToDelete

the script deletes the entire plist file though, not the key?

what am I doing wrong.
 
IIRC, defaults does not use the path to the plist file, instead it uses the reverse-DN of the domain you want to alter. In this case it will be com.apple.Safari

So, according to the man page, you would want to call:
Code:
/usr/bin/defaults delete com.apple.Safari <KeyName>
 
IIRC, defaults does not use the path to the plist file, instead it uses the reverse-DN of the domain you want to alter. In this case it will be com.apple.Safari

So, according to the man page, you would want to call:
Code:
/usr/bin/defaults delete com.apple.Safari <KeyName>

I tried this:

Code:
#!/bin/sh

/usr/bin/defaults delete myKeyToDelete

which generates an error in the installer, and doesn't delete the key.
and this one:

Code:
#!/bin/sh

MY_UNUSED_KEY="com.apple.Safari myKeyToDelete"
if [  -d "MY_UNUSED_KEY" ]; then
	echo "Removing unused preferences"
	/usr/bin/defaults delete "MY_UNUSED_KEY"
fi

which doesnt generate an error, but also doesnt delete the key?


this shouldnt be that difficult....
 
I tried this too:

Code:
/usr/bin/defaults delete ~/Library/Preferences/com.apple.Safari myKeyToDelete

this deletes the entire plist file....


stupid PackageMaker!?...
 
I also posted the question to the installer-dev mailing lists, and found the solution to be:

Code:
su $USER -c "defaults delete com.apple.Safari myKeyToDelete"

:D
 
Code:
#!/bin/sh

MY_UNUSED_KEY="com.apple.Safari myKeyToDelete"
if [  -d "MY_UNUSED_KEY" ]; then
	echo "Removing unused preferences"
	/usr/bin/defaults delete "MY_UNUSED_KEY"
fi

which doesnt generate an error, but also doesnt delete the key?

this shouldnt be that difficult....

That shell script doesn't work because you're using the literal string "MY_UNUSED_KEY", rather than expanding the value of the shell variable. To expand the shell variable, you should use "$MY_UNUSED_KEY".

Also, your defaults delete ... line is wrong for a second reason: you're quoting the expanded shell variable. So even if you had this:

Code:
/usr/bin/defaults delete "$MY_UNUSED_KEY"
It would still be wrong, because it would expand to this:

Code:
/usr/bin/defaults delete "com.apple.Safari myKeyToDelete"


It pays to actually understand the tools you're working with, whether that's the defaults command or the syntax of shell variables. RTFM.

It also pays to read replies before posting other failed attempts. RTFR.
 
It pays to actually understand the tools you're working with, whether that's the defaults command or the syntax of shell variables. RTFM.

Thanks for the crash course, Im only just gettng to grips with Obj-C, and now I need to learn another language....
:)
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.