AppleScript Studio & User Defaults: Blank String Won't Save [SOLVED]

Discussion in 'Mac Programming' started by HiRez, Nov 7, 2007.

  1. HiRez macrumors 603


    Jan 6, 2004
    Western US
    Hi, I'm trying to use user defaults with AppleScript Studio. I'm setting up user defaults like so (I edited this function down, this is just one of many):

    on will finish launching theObject
    		tell user defaults
    			make new default entry at end of default entries with properties {name:DEFAULT_CLEAN_PREFIX_KEY, contents:DEFAULT_CLEAN_PREFIX}
    		end tell		
    		call method "synchronize" of object user defaults
    	on error errMsg number errNum
    		display dialog "Error registering user defaults: " & errMsg
    	end try
    end will finish launching
    When the variable DEFAULT_CLEAN_PREFIX is a string of 1 or more characters, this works fine: the default entry is created. The problem is when I want to set the initial value to "" (an empty string). In this case, user defaults seems to detect this and does not make a defaults entry for that key. This royally screws up my script later when I go to access that value (AppleScript complains with an error of course).

    So why can't I make a new default entry with a blank string? This seems like it might be quite a common thing that people would want to do. Is there some special AppleScript keyword I'm missing that would help me out here ("null" does not work, it gives me the memory location of null when I try to access it)? Now I can work around this by setting the default initially to some string of 1+ characters, then immediately rewriting the default as "". That works but it sure seems like an ugly hack that shouldn't be necessary (then again, I'm finding that AppleScript requires many ugly hacks on a regular basis). Any ideas? TIA.

    EDIT: OK ignore this whole thing, I guess. After deleting the prefs file a few times and recompiling it seems to be working now (even though I changed nothing). Something is definitely goofy but I can't pin it down. *sigh* AppleScript is always so wonderful and so frustrating at the same time.

Share This Page