Is there a way to store a variable in a file?

i have a app that I want to run. When I competely close it, I want it to start where it left off. So, I thought I could make a script that would check to see if a file exists. If it does not, it will create it and then store text in it. The text would contain a word or letter where I left off last with the program. Next time I run the program, the script will check for the folder, then read the text it made, and then leave off from there. Is there a way to do this? I understand a full blown app can store data. But, is there a way to make a file that script code can read text from?

doc:

Try something like this:

Early in your script, define a variable to be the log file, complete with path.
Then, test for the presence of the file. If it does not exist, call a handler to write the file, and write the beginning value to the file. (This should only have to happen once; the first time the script is executed.) When the script is done with the handler, go back and read the file, get the starting value, and off it goes.
At the end of the script, write the value you want to preserve to the file.
When the script is run next, it will test for the presence of the file, it will be there this time, so now you can read in the previously saved value, assign it to the proper variable, and off you go.

Hope this helps.

You can also use the “defaults” tool, which is pretty simple. Eg:

try
	set oldValue to (do shell script "defaults read myScriptName myVar")
on error --> undefined
	set oldValue to "Default value!"
	do shell script "defaults write myScriptName myVar " & quoted form of oldValue
end try

display dialog "Current stored value is:" & return & return & oldValue & ¬
	return & return & "Enter new one..." with icon note default answer oldValue

do shell script "defaults write myScriptName myVar " & quoted form of (text returned of result)

This will create a file called “myScriptName.plist” at ~/Library/Preferences, containing an entry called “myVar”, where you store whatever text (and retrieve it when needed).

My preferred method is this one by hhas (from this thread):

property _prefs : {foo:1, bar:"hello"} -- [a record containing your persistent preferences]

on _prefsFile()
	return POSIX file (POSIX path of (path to preferences) & "MyAppletPrefs") -- [your prefs filename here]
end _prefsFile

on loadPrefs()
	try
		set _prefs to read _prefsFile() as record
	on error number -43 -- file not found; use default values
	end try
end loadPrefs

on savePrefs()
	set f to open for access _prefsFile() with write permission
	write _prefs to f as record
	close access f
end savePrefs

I agree. The nice thing about using the ‘defaults’ tool is that is the recommended way to store preferences. I made a pair of handlers (based on what jj wrote, and what I’ve done before) that allow you to name the plist appropriately as well. The read handler allows you to specify a default value if there is none when you try to read. The write handler can only store basic data types (strings, number), not records or lists. The defaults command has options to store arrays (like lists) and dictionaries (like records…well, actually, more like real hashes, not the somewhat-crippled AppleScript records), but these handlers don’t cover the necessary conversions:

property appName : "TestReadWriteDefaults"

on run
	set testReadPrefs to {appDomain:"com.danshockley", appName:appName, readKey:"testKey", defaultValue:"default"}
	readDefaultsPref(testReadPrefs)
	
	set testWritePrefs to {appDomain:"com.danshockley", appName:appName, writeKey:"testKey", writeValue:"someSimpleValue"}
	
	writeDefaultsPref(testWritePrefs)
	
end run


on readDefaultsPref(prefs)
	-- version 1.0, Daniel A. Shockley
	-- prefs should look like: {appDomain:"com.YOURDOMAIN", appName:"NAME", readKey:"SOME_KEY" [, defaultValue:"OPTIONAL_DEFAULT" ]}
	
	set defaultPrefs to {defaultValue:""}
	set prefs to prefs & defaultPrefs
	
	set defaultsApp to appDomain of prefs & "." & appName of prefs
	
	try
		set readCommand to "defaults read " & quoted form of defaultsApp & space & quoted form of (readKey of prefs)
		--return readCommand
		set oldValue to (do shell script readCommand)
	on error --> undefined
		set oldValue to defaultValue of prefs
		set writeCommand to "defaults write " & quoted form of defaultsApp & space & ¬
			quoted form of (readKey of prefs) & space & quoted form of (oldValue)
		do shell script writeCommand
	end try
	return oldValue
	
end readDefaultsPref


on writeDefaultsPref(prefs)
	-- version 1.0, Daniel A. Shockley
	-- prefs should look like: {appDomain:"com.YOURDOMAIN", appName:"NAME", writeKey:"SOME_KEY" [, writeValue:"SOME_VALUE" ]}
	
	set defaultPrefs to {}
	set prefs to prefs & defaultPrefs
	
	set defaultsApp to appDomain of prefs & "." & appName of prefs
	
	try
		set writeCommand to "defaults write " & quoted form of defaultsApp & space & ¬
			quoted form of (writeKey of prefs) & space & quoted form of (writeValue of prefs)
		(*		set the clipboard to writeCommand
		return writeCommand*)
		do shell script writeCommand
	on error errMsg number errNum
		error errMsg number errNum
		return false
	end try
	return true
	
end writeDefaultsPref

If you want to use the defaults command this way in multiple scripts, then you may consider making a “library” and including it when needed.

property _domain : "DefaultDomain"

on readKey(_key, _defaultValue)
	try
		do shell script "/usr/bin/defaults read " & quoted form of _domain & space & quoted form of _key
	on error
		writeKey(_key, _defaultValue)
		return _defaultValue
	end try
end readKey

on writeKey(_key, _value)
	do shell script "/usr/bin/defaults write " & quoted form of _domain & space & quoted form of _key & space & quoted form of _value
end writeKey

-- Path to the "library" script
property myPrefs : load script alias ((path to scripts folder as Unicode text) & "Lib:Defaults.scpt")

on run
	tell myPrefs to set _domain to "Test"
	
	repeat
		tell myPrefs to readKey("Hello", "Default")
		
		display dialog ("Current value: " & result & return & return & "Enter new value:") default answer ""
		
		tell myPrefs to writeKey("Hello", text returned of result)
	end repeat
end run