How do i pass variables’ values between subsequent applescript runs? (persistent value between script executions)

applescriptplist

I have an applescript that I run like a cron job. Within this applescript, i have variables that act like boolean switches (i.e. on/off values). With subsequent runs, depending on whether the boolean variables' values are on/off i need to take certain actions.

Q) What are the different ways this can be done? (would appreciate sample code).

Cheers.


Answer) plist or property files seem to be the way to go.

credit to daviesgeek for pointing me in the right direction. for those interested i've written helper sub-routines as follows:

-- read the plist file
tell application "System Events"
   tell property list file plistfile_path
      tell contents
         set someLocalVariable to value of property list item "someLocalVariable"
      end tell
   end tell
end tell


on create_plist_file(file_path)
    -- create the plist file
    tell application "System Events"
         -- create an empty property list dictionary item
        set the parent_dictionary to make new property list item with properties {kind:record}

        -- create new property list file using the empty dictionary list item as contents
        set this_plistfile to ¬
            make new property list file with properties {contents:parent_dictionary, name:file_path}
            -- add new property list items of each of the supported types
            make new property list item at end of property list items of contents of this_plistfile ¬
                with properties {kind:boolean, name:"someLocalVariable", value:false}
            -- make new property list item at end of property list items of contents of this_plistfile ¬
            --     with properties {kind:string, name:"stringKey", value:"string value"}
    end tell
end create_plist_file



on set_someLocalVariable(bool_val)
    tell application "System Events"
        tell property list file plistfile_path
            tell contents
                set value of property list item "someLocalVariable" to bool_val
            end tell
        end tell
    end tell
end set_wasAllTransfersStarted

Best Answer

You can write to a .plist file as such:

--Empty preference file
set theEmptyPListData to "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
<!DOCTYPE plist PUBLIC \"-//Apple Computer//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">
<plist version=\"1.0\">
<dict/>
</plist>"

--set preference list path and open with write permissions
set theOutputFolder to path to preferences folder from user domain as string
set thePListPath to theOutputFolder & "AppleScript.plist"
set thePListFile to open for access thePListPath with write permission

--check to see if the plist variable is set
try
    tell application "System Events"
        tell property list file thePListPath
            tell contents
                value of property list item "browser"
            end tell
        end tell
    end tell
on error
    tell application "System Events"
        if not (exists file thePListFile) then
            set webBrowser to the button returned of (display dialog "What web browser to you want to use?
(Choose carefully, as this will not appear again)" buttons {"Safari", "Firefox", "Google Chrome"})

            set eof of thePListFile to 0
            write theEmptyPListData to thePListFile starting at eof

            tell property list file thePListPath
                tell contents
                    set value to {|browser|:webBrowser}
                end tell
            end tell
        end if
    end tell
end try

This script will ask for the user's browser and it will save it across runs of the script.