• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: plist files instead of properties
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: plist files instead of properties


  • Subject: Re: plist files instead of properties
  • From: Luther Fuller <email@hidden>
  • Date: Sat, 10 Jun 2017 15:02:09 -0500

On Jun 10, 2017, at 12:07 PM, Stockly, Ed wrote:

> OK, I'm ready to take the plunge and stop using properties to store data in
> my scripts and use plist files instead.
> I've been looking for definitive information about how to do this, but I'm
> not sure I've found it.
> The scripts will be running on Yosemite and El Capitan.

I have a very large script that stores info in a .plist preference file.
It has been in use for many years, so I have not read it for many years.
(I hope I haven't forgotten any important details.)
I am currently using system version 10.7.5, but I have tested in 10.8.
(I really need to upgrade to 10.9 and later!)
The following handlers and snippets of script will illustrate my method.

My script starts with these properties ...

property defaultPrefsRecord : {|RegistrationKey|:"0", |version|:40020070,
|ArchiveLastKnownLocation|:"", |ArchiveLastKnownName|:"", |DaysToHoldTrash|:15,
|DaysToHoldLinks|:15}
property prefsName : "com.LutherFuller.MailWing" -- = CFBundleIdentifier


Whenever my script needs the stored info, the following getPrefsFile handler is
called
which, if necessary, will call the makeNewPlistFile handler ...
on getPrefsFile()
        set prefsFolder to (path to preferences from user domain)
        set prefsFile to my makeNewPlistFile(prefsFolder, prefsName,
defaultPrefsRecord) -- creates a new .prefs file if it doesn't exist
        tell application "System Events" to set prefsRec to (value of property
list file (prefsFile as text)) --<<<<<<<<<<<<< READ
        try
                set prevVersion to (|version| of prefsRec) -- version read from
prefs file
        on error
                set prevVersion to (|version| of defaultPrefsRecord)
        end try
        set prefsRec to (prefsRec & defaultPrefsRecord)
        tell application "System Events" to set value of property list file
(prefsFile as text) to prefsRec --<<<< REPAIR
        set currentVersion to (|version| of defaultPrefsRecord) -- the version
of this application
        if prevVersion = currentVersion then return prefsFile
        --
        if prevVersion > currentVersion then
                error "You have installed a later version of " & shortTitle &
"." & return & "You should remove this copy of " & shortTitle & "."
        end if
        my updateNewVersion(prefsFile, prefsRec) -- prevVersion < currentVersion
        error number -128
end getPrefsFile --------------------------------------------------------------

on makeNewPlistFile(folderAlias, filename, initialRecord)
        try
                return ((folderAlias as text) & filename & ".plist") as alias
-- the file already exists
        end try
        set newFileName to (folderAlias as text) & filename
        tell application "System Events"
                make new property list item with properties {kind:record,
value:initialRecord}
                make new property list file with properties {contents:the
result, name:newFileName}
                set newFileName to (path of the result)
        end tell
        return (newFileName as alias)
end makeNewPlistFile
--------------------------------------------------------------------

The updateNewVersion handler is very long and un-instructive,
but it does write new data to the .plist preferences file with this one liner
...
        tell application "System Events" to set value of property list file
(prefsFile as text) to newPrefsRec --<<<<<<<<<< WRITE new prefs file


The information returned by getPrefsFile is just an alias to my .plist
preference file.
It is stored in the variable prefsFile, used below.
I read and write this file as illustrated in the following snippets  ...

                tell application "System Events" -- delete archive path from
preference file
                        set value of property list item
"ArchiveLastKnownLocation" of property list file (prefsFile as text) to ""
--<<<<<< WRITE
                        set value of property list item "ArchiveLastKnownName"
of property list file (prefsFile as text) to "" --<<<<<< WRITE
                end tell

                tell application "System Events"
                        value of property list item "ArchiveLastKnownLocation"
of property list file (prefsFile as text) -- <<<<<<<<<<<<< READ
                        the result & (value of property list item
"ArchiveLastKnownName" of property list file (prefsFile as text)) --
<<<<<<<<<<<<< READ
                end tell --  = ArchiveLastKnownLocation & ArchiveLastKnownName

But, does this still work in the latest system version? (I hope so!)
Let us know what happens.

Note: In the line
        set prefsRec to (prefsRec & defaultPrefsRecord)
values of prefsRec over-write values in defaultPrefsRecord. Look up the
definition of "&".




 _______________________________________________
Do not post admin requests to the list. They will be ignored.
AppleScript-Users mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
Archives: http://lists.apple.com/archives/applescript-users

This email sent to email@hidden

References: 
 >plist files instead of properties (From: "Stockly, Ed" <email@hidden>)

  • Prev by Date: Re: plist files instead of properties
  • Next by Date: Re: plist files instead of properties
  • Previous by thread: Re: plist files instead of properties
  • Next by thread: Re: plist files instead of properties
  • Index(es):
    • Date
    • Thread