Twitter Updates

Tuesday, 9 September 2008

An Applescript Preference Systems

I found the basis for an applescript preference systems at macosxhints.
I have corrected a few typos and here it is.

tell application "Finder"

end tell

Preference system version 1

Chris J. Shull

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 "net.munkymorgy.pref" --this will be the filename
set prefcontent to "default" --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

--write prefs snippet (overrides original)
if CheckIfExists(prefname, prefpath) is true then DeleteAFile(prefname, prefpath)
MakeAFile(prefname, prefcontent, prefpath)
display dialog (prefpath & " " & prefname)
--end write prefs

---read Prefs snippet
if CheckIfExists(prefname, prefpath) then
set pref to paragraph ContentParaNumber of ReadAFile(prefname, prefpath)
set ThereArePrefs to true
set ThereArePrefs to false
end if
if ThereArePrefs then display dialog pref --or whatever you want
---End read

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 "/" & (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 & "\"")
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 userName to do shell script "whoami"
set UnixPath to UnixfyPath("Users:" & userName & ":", ":", "/")
set prefpath to UnixPath & "Library/Preferences/"
return prefpath
end FindPrefFolder


No comments: