User Defaults! Please Help!

Hey all :slight_smile:
If anyone can answer this i really owe them one! I will give a credit to them too!
Ok here it is:


on launched theObject
   try
      read-saved(firstrun)
      on error
        save() set firstrun to true
    end try
if firstrun is true then
save() set firstrun to false
start_tutorial()
end if

end on

You are great if you can do that! (if that seems to complex, insted you can try to explain to me in dumbest terms how to use UD)

Thanks a million in advance :smiley:

Hi,

here are some simple routines I’m using .
You must make sure, that save_Preferences() will be executed before quitting


property first_run : true

on will finish launching theObject
	register_Preferences()
	read_Preferences()
	if first_run is true then
		clean_user_settings()
		set first_run to false
	end if
end will finish launching

on clean_user_settings()
-- set a few varaibles to a certain value
end clean_user_settings

on register_Preferences()
	tell user defaults
		make new default entry at end of default entries with properties {name:"first_run", contents:1}
	end tell
end register_Preferences

on read_Preferences()
	try
		tell user defaults
			set first_run to contents of default entry "first_run" as boolean
		end tell
		return true
	on error errorText number errorNumber
		log "Error reading the user defaults."
		log "  Error " & errorNumber & ": " & errorText
		return false
	end try
end read_Preferences

on save_Preferences()
	try
		tell user defaults
			set contents of default entry "first_run" to (first_run as integer)
		end tell
		return true
	on error errorText number errorNumber
		log "Could not synchronize user defaults."
		log "  Error " & errorNumber & ": " & errorText
		return false
	end try
end save_Preferences

on will quit theObject
	save_Preferences()
	quit
end will close

thanks, but i couldnt get it to work :frowning:
could you please incorporate it into my example so i canget an idea of how to use it?

Hi,

in your example it’s like

on launched theObject
   register_Preferences()
   read_Preferences()
   if first_run is true then
   	start_tutorial()
   end if
end launched

the structure is the following:

¢ at the beginning define the property first_run (oder which name do you want)
¢ in the launching routine the register_Preferences() handler defines the key in the .plist file and the read_Preferences() handler reads the value(s)
¢ in the closing or quitting routine the save_Preferences() writes the current value(s) back to disk

Note: If the key is already defined register_Preferences() does nothing

at the very beggining or in the handler?
What do u mean my if the key os already defined?

of course at the beginning of the script, properties can only defined at the top level of a script (or script object)

The definition of an simple entry in a .plist file is key for the name of the variable and value for its value.
register_Preferences() defines a simple entry {key:“first_run”, value:1}. If the key already exists, the value won’t be changed

thanks!
Is 1 true and 0 false?

exactly :slight_smile:

Thanks heaps!
It works great BUT it just sets first_run to false straight up, it never gives it a run with it set to true. But it does work, i tweaked the pref file and it does what i want. Do you know why this is?

(PS. With my apps identifyer thingee, can it be anything? at the moment it is com.cns.dashtop is that valid?)

Great! I just found the problem, thanks anyway!
Please reply if you want me to put a special thanks on any app i use it in