Write to .plist

Having trouble with code (as usual), trying to write to plist.

set tempScript1 to POSIX path of ((choose file) as text)
            log "tempScript1 is: " & tempScript1
            field1's setStringValue_(tempScript1)
            log "Writing to .Plist"
            do shell script "defaults write " & (((current application's NSBundle's mainBundle()'s bundlePath()) as text) & "/Contents/Info.Plist") & " script1Path " & tempScript1
            log "Plist reads: " & (current application's NSBundle's mainBundle()'s objectForInfoDictionaryKey_("script1Path")) as string
            set script1 to (current application's NSBundle's mainBundle()'s objectForInfoDictionaryKey_("script1Path")) as string

Everything seems to work except the writing to plist. It doesn’t return an error because it’s wrapped in a try, end try and on error but it doesn’t work either because the log has “Plist reads:” and blank. When I check the plist of the application, it’s blank too. This is annoying :@

Help!

set infoPlist to (current application's NSBundle's mainBundle()'s bundlePath()'s stringByAppendingPathComponent_("Contents/Info")) as string --> no .plist extension !
do shell script "defaults write " & infoPlist & " script1Path " & tempScript1

but Cocoa has much better methods to write into Info.plist

Ah right, ok, thanks. I’ll try it out when I get home. Are there any Cocoa-style methods I could use?

Ok, I’ve tried it out now. Couple of things though…

  1. When writing to .plist , it doesn’t like it if there’s a space anywhere in the name of the path. I know I can use delimiters to take out spaces, but I don’t know what to replace them with…

  2. My .plist starts off blank, when I compile. I go to preferences (of my application) and pick a .scpt , the path gets written to the .plist fine. However, after that, if I choose a different script, it writes with no errors, but when it logs the .plist value, it comes up as the old value. Even if I quit it and relaunch, when it reads the .plist at startup, the value still has the old value. Why? And how to fix?

It seems that you’re going to change the path information in Info.plist frequently.
Info.plist is not the right place to do that.
user defaults, which reads/writes into an application specific property list file in ~/Library/Preferences is
designated for this purpose

Ok, how do I read and write to/from user defaults?

please search this forum for NSUserDefaults. There are some threads with sample code

Ok, I looked at your post: http://macscripter.net/viewtopic.php?pid=108401#p108401 and followed what you said in there. How do I call a method of an Obj-C class on ASOC? That’s the only thing I’m getting an error on…

call method "setupDefaults" of class "DefaultsController"

Call method is AppleScript Studio code, which is deprecated.
Sorry, I don’t have Snow Leopard and cannot test any ASOC code.

Again, please search for keyword NSUserDefaults only in the AppleScriptObjC forum.