create a plist to remember preferences in AS

Hello all,

I would like to know if you could give me pointers on how to deal with plist files.
What I’m trying to do is make my app remember the transparency level, set by the user with a slidebar.

I have tried following a tutorial online but it doesnt seem to work, and I dont know why! Needless to say, I havent found a tut here that deals with plist files…

Im sure its nonsense, but heres my code so far :

-- Transparency
on action theObject
	
	set valeur to (contents of slider "transparence" of window of theObject) as real
	set (alpha value of window "main") to valeur
	set valeur2 to (contents of slider "transparence" of window of theObject) as real
	set (alpha value of window of theObject) to valeur2
	
end action

--- LOAD PREFERENCES ---
on chargerPrefs()
	tell user defaults
		set valeur to contents of "transparence"
	end tell
end chargerPrefs

--- REMEMBER PREFERENCES ---
on stockerPrefs()
	tell user defaults -- Création de la p-list
		make new default entry at end of default entries with properties {name:"transparence", contents:valeur}
		set contents of default entry "transparence" to valeur
	end tell
	return true
end stockerPrefs

--- SAVE PREFERENCES ---
on should quit theObject
	log "should quit"
	stockerPrefs()
end should quit

Im sure theres something specific about the transparency level that Im not getting, but Im kind of new at this …

Thanks for any help you could provide me with !

BS0D

Hi,

before using user defaults you have to register the key / value objects
in the awake from nib or will finish launching handler

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

But it’s much easier to use Cocoa bindings. This doesn’t require any code.
In Interface Builder select the slider choose Bindings in the inspector window,
enable Bind to User Defaults Controller and type transparence into the field Model Key Path

Hello StefanK,

Thanks for the reply, but I’m very new at this and I’m not getting it !

Could you tell me what part of my code is wrong, and how I should use the code you posted ?

The plist file isnt even created by my app… I’m so confused !

EDIT | Alright, now the transparency slidebar does stay in the same place thanks to the binding operation you told me about. However, its just the bar that stays in the same place, the transparency is always the same when I reopen the app !

I’m wondering if a plist is read at opening …

try this, in the chargerPrefs the default entry keyword was missing


on awake from nib
	registerPrefs()
	chargerPrefs()
end awake from nib

-- Transparency
on action theObject
	
	set valeur to (contents of slider "transparence" of window of theObject) as real
	set (alpha value of window "main") to valeur
	set valeur2 to (contents of slider "transparence" of window of theObject) as real
	set (alpha value of window of theObject) to valeur2
	
end action

--- REGISTER PREFERENCES ---
on registerPrefs()
	tell user defaults
		make new default entry at end of default entries with properties {name:"transparence", contents:0.5}
		register
	end tell
end registerPrefs

--- LOAD PREFERENCES ---
on chargerPrefs()
	tell user defaults
		set valeur to contents of default entry "transparence"
	end tell
end chargerPrefs

--- REMEMBER PREFERENCES ---
on stockerPrefs()
	tell user defaults -- Création de la p-list
		set contents of default entry "transparence" to valeur
	end tell
	return true
end stockerPrefs

--- SAVE PREFERENCES ---
on should quit theObject
	log "should quit"
	stockerPrefs()
end should quit

Hello,

Thanks for the advice; I added “default entry” there, but it’s still the same, the transparency does not appear on opening, even though the slidebar is in the place where I left it.

What should I link to “awake from nib”, that slidebar or the main window?

awake from nib will be called before the window loads,
so this is the place to make default settings.

The registerPrefs() handler instantiates the user defaults. The default value will be ignored if the key exists in the .plist file
The chargerPrefs() reads the user defaults into the appropriate variables.
If you want to set the silder, you must do this also in the awake from nib handler

on awake from nib
	registerPrefs()
	chargerPrefs()
	set float value of slider "transparence" of window "main" to valeur
end awake from nib

alright its just not working, I get an error saying that the variable valeur is not defined
Im giving up the transparency thing…

Thanks anyway

Your code implied that the variable valeur is a global or a property,
which makes the variable visible from everywhere