Apr 06, '05 09:16:00AM • Contributed by: tocoolcjs
[robg adds: I have not tested these snippets, as I don't have very strong AppleScript skills. But if you're looking for a way to add a basic preference system to your code, these would seem to be a good starting point...]
The code is free. Re-use at will, but please leave this acknowledgement in your code. Feel free to email me and tell me you've found it helpful!
Base for Prefs:
(* Preference system version 1 Chris J. Shull themacgeek@comcast.net http://home.comcast.net/~themacgeek/ The code is free. Re-use at will, but please leave this acknowledgement in your code. Feel free to email me and tell me you've found it helpful! *) ---Base prefs snippet set prefname to "com.ShullianProductions.preftester" --this will be the filename set prefcontent to "para 1" & return & "para 2" --this will be the content of the pref file. NOTE that you can create multiple lines set prefpath to FindPrefFolder() --finds the path to the user's Prefs folder set ContentParaNumber to 1 --is the paragragh that will be found in a single preference finder. change it throughout the script to switch paragragh found ---End of Base
Writing prefs:
--write prefs snippet (overrides original) if CheckIfExists(prefname, prefpath) is true then DeleteAFile(prefname, prefpath) MakeAFile(prefname, prefcontent, prefpath) --end write prefs
Reading Prefs:
---read Prefs snippet if CheckIfExists(prefname, prefpath) then set pref to paragraph ContentParaNumber of ReadAFile(prefname, prefpath) set ThereArePrefs to true else set ThereArePrefs to false end if if ThereArePrefs then display dialog pref --or whatever you want ---End read
Subroutines
---Subroutines-for-preferences-----------------
on UnixfyPath(stringer, origchar, newchar)
set the testlist to the text items of the stringer
repeat with letter in testlist
set let to letter as string
set orig to origchar as string
if let is orig then
set the contents of the letter to newchar
end if
end repeat
set the newpath to "/Volumes/" & (the testlist as text)
return the newpath
end UnixfyPath
on MakeAFile(filename, filecontents, folderpath)
tell application "Finder"
do shell script ("echo "" & filecontents & "" | cat > "" & folderpath & "/" & filename & """)
end tell
end MakeAFile
on ReadAFile(filename, folderpath)
tell application "Finder"
set filecontent to do shell script "cat "" & folderpath & filename & """
end tell
return filecontent
end ReadAFile
on DeleteAFile(filename, folderpath)
tell application "Finder"
do shell script "rm "" & folderpath & filename & """
end tell
end DeleteAFile
on CheckIfExists(filename, folderpath)
tell application "Finder"
set folder_cmd to ("find "" & folderpath & filename & """)
try
do shell script folder_cmd
set worked to true
on error
set worked to false
end try
end tell
return worked
end CheckIfExists
on FindPrefFolder()
tell application "Finder" to set libpath to path to home folder
set UnixPath to UnixfyPath("OS X HD:Users:chris:", ":", "/")
set prefpath to UnixPath & "Library/Preferences/"
return prefpath
end FindPrefFolder
--End-of-Subroutines-for-preferences-----------------
