A One-Liner to Keep Track of User Preferences in LiveCode

I have kept this a secret for a long time, but after using this in many projects, it is now time to publish my user preferences library for LiveCode/Revolution.

Below follow the scripts without comments. I will write more about it in a future blog post.

function saveGlobalsToVar theGlobals
  do “global” && theGLobals
  put theGlobals & return into tempVar
  repeat for each item x in theGlobals
    do “put urlEncode(” & x & “) & return after tempVar”
  end repeat
  return tempVar
end saveGlobalsToVar

on readVarToGlobals theVar
     — breakpoint
     put line 1 of theVar into myGlobals
     delete line 1 of theVar
     do “global” && myGlobals
     repeat with x = 1 to number of items of myGlobals
          put “put urlDecode(line x of theVar) into” && item x of myGlobals into myScript
          do myScript
     end repeat
end readVarToGlobals

on writePrefs theGlobals,thePrefsFilename
  put saveGlobalsToVar(theGlobals) into myPrefs
  put prefsFile(thePrefsFilename) into myPrefsFile
  set the filetype to “sysppref”
  open file myPrefsFile for binary write
  write compress(myPrefs) to file myPrefsFile
  close file myPrefsFile
end writePrefs

on readPrefs thePrefsFilename
  put prefsFile(thePrefsFilename) into thePrefsFilename
  if there is a file thePrefsFilename then
    open file thePrefsFilename for binary read
    read from file thePrefsFilename until EOF
    close file thePrefsFilename
    put decompress(it) into myPrefs
    — put myPrefs
    readVarToGlobals myPrefs
  else
    return “Error: File does not exist.”
  end if
end readPrefs

function prefsFile theFilename
     if gU3App is true then
          put $u3_app_data_path & slash & theFilename into myFilePath
          replace backslash with slash in myFilePath
     else if the platform is “MacOS” then
          put specialFolderpath(“preferences”) & “/” & theFilename into myFilePath
     else if the platform is “win32” then
          put specialFolderpath(26) & “/” & theFilename & “.ini” into myFilePath
     else if the platform is “Linux” then
          put $HOME & slash & dot & theFilename into myFilePath
     else
          put stackToFilename(theFilename) into myFilePath
     end if
     return myFilePath
end prefsFile

2 thoughts on “A One-Liner to Keep Track of User Preferences in LiveCode

Leave a Reply

Your email address will not be published. Required fields are marked *


*