Delete a key from plist

First I read this and I can’t seem to get this to work.

Let’s say I have this plist:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
	<key>PDN8176440</key>
	<dict>
		<key>content</key>
		<string>Some Content</string>
		<key>rundates</key>
		<array>
			<string>5/27/2012</string>
			<string>6/3/2012</string>
			<string>6/10/2012</string>
		</array>
	</dict>
	<key>PDN8221358</key>
	<dict>
		<key>content</key>
		<string>Some More Content</string>
		<key>rundates</key>
		<array>
			<string>6/3/2012</string>
			<string>6/10/2012</string>
			<string>6/17/2012</string>
		</array>
	</dict>
</dict>
</plist>

Which for this example resides here:

set theOutputFolder to path to desktop folder as string
set thePListPath to theOutputFolder & "myPListFile"

(defaults doesn’t want the plist extension it seems)

And I wish to delete the key “PDN8176440”


set keytogo to "PDN8176440"
	set mycommand to ("defaults delete " & (quoted form of POSIX path of thePListPath) & " " & keytogo) as string
	do shell script mycommand

Results in:
(note my path in the script I’m using is different than my example so it says path to plist because it’s desktop or whatever… it’s an example, the script does find the file.)

There is no (PDN8176440) default for the (path to plist:myPListFile.plist) domain.
Defaults have not been changed.

Perhaps I’m not understand how to use defaults because this returns the list:

	set mycommand to ("defaults read " & (quoted form of POSIX path of thePListPath)) as string 
	return do shell script mycommand

So what am I doing wrong here?

I ran your example and it works fine… no errors. I can read and delete from the plist with your code. I didn’t change a thing.

Very interesting, mostly because it works for me as well. I was using a function and changing it to:

(keytogo as string)

actually did the trick, so it as an issue of coercion and my issue wasn’t where I thought it was.

on removekeys(keytogo, thePListPath)
	set mycommand to ("defaults delete " & (quoted form of POSIX path of thePListPath) & " " & (keytogo as string)) as string
	do shell script mycommand
end removekeys

Unlikely. The coercion’s entirely superfluous, since everything in the preceding concatenation is text anyway and so the result of the concatenation’s text too.

The error message you were getting suggests that the “plist” name extension wasn’t actually being dropped on the run(s) where it occurred.