Saturday, July 4, 2020

#1 2020-04-30 01:48:04 pm

LookToWindward
Member
Registered: 2020-04-28
Posts: 14

How to get a Property File Object for an existing Plist File

Hi,

I'm having problem reading back a Property List file I created with "System Events".

Creation:

set myParentDictionary to make new property list item with properties {kind:record}

— File Object in myPropertyListFile
set myPropertyListFile to make new property list file with properties {contents:myParentDictionary, name:pMWLPrefsFilePOSIXPath of me}

tell application "System Events"
    tell property list file pPrefsFileObject of me
        set pModOrderFileURL of me to value of property list item "pModOrderFileURL"
        set pModOrderList of me to value of property list item "pModOrderList"
        set pModInfoList of me to value of property list item "pModInfoList"
    end tell
end tell

—Test Reading Back here, works ok
tell property list file myPropertyListFile
    set pModOrderFileURL of me to (value of property list item "pModOrderFileURL")
    set pModOrderList of me to value of property list item "pModOrderList"
    set pModInfoList of me to value of property list item "pModInfoList"
end tell


It does this if it doesn’t find a prefs file, to create one.

Then on subsequent Runs of the Script I want to restore the properties from the file. I was using the file path, but this is wrong, it need to be the File Object.


—  This doesn’t work since I’m passing the File Path String instead of a File Object
--
tell application "System Events"
    tell property list file pMWLPrefsFilePOSIXPath of me
        set pModOrderFileURL of me to (value of property list item "pModOrderFileURL")
        set pModOrderList of me to value of property list item "pModOrderList"
        set pModInfoList of me to value of property list item "pModInfoList"
    end tell
end tell

But I don’t have the File Object that was around (myPropertyListFile) when it was created it was in a different run of the Script). So, given the file on the disk, how to I get a new a File Object?

I am assuming you can read back the file?!?!? It doesn’t actually say you can, it would be typical of Apple to give you something to write a property list but not give you one to read it back again, and when it comes to AppleScript it wouldn’t surprise me in the least.

Fed up with poor documentation…..

Thanks for any help.

All the Best
Dave

Last edited by LookToWindward (2020-04-30 01:48:53 pm)

Offline

 

#2 2020-04-30 02:04:10 pm

LookToWindward
Member
Registered: 2020-04-28
Posts: 14

Re: How to get a Property File Object for an existing Plist File

Found it! If I change it to:

tell application "System Events"
    set myPropertyListPath to pMWLPrefsFilePOSIXPath of me                            ———Changed------
    tell property list file myPropertyListPath                                            ———Changed------
        set pModOrderFileURL of me to (value of property list item "pModOrderFileURL")
        set pModOrderList of me to value of property list item "pModOrderList"
        set pGameInfoList of me to value of property list item "pGameInfoList"
        set pModInfoList of me to value of property list item "pModInfoList"
    end tell
end tell

That used to read:

    tell property list file  pMWLPrefsFilePOSIXPath of me   

So, assigning it to local storage and using the local made it work! wtf! AppleScript is in a class of its own when it comes to weird happenings!!!

Offline

 

#3 2020-04-30 02:44:59 pm

Yvan Koenig
Member
Registered: 2006-09-14
Posts: 4533

Re: How to get a Property File Object for an existing Plist File

I never used of me but I took care to read carefully the documentation.

Applescript:

set pMWLPrefsFilePOSIXPath to POSIX path of ((path to desktop as text) & "myList.plist") -- was not defined

tell application "System Events"
   set myList1 to {"Peter", "Paul", "Mary"}
   set infos1 to "were American"
   tell me to set alias1 to path to library folder from user domain -- “tell me” is required because we are in a tell application block
   set anURL1 to URL of alias1
   -- at first run it contains nothing
   -- set myParentDictionary to make new property list item with properties {kind:record} -- not really needed, see below.
   
   -- Create the file and return a reference to it
   -- set myPropertyListFile to make new property list file with properties {contents:myParentDictionary, name:pMWLPrefsFilePOSIXPath}
   set myPropertyListFile to make new property list file with properties {name:pMWLPrefsFilePOSIXPath} -- work the same and requires less code
   -- end
   -- tell application "System Events"
   tell myPropertyListFile -- trigger the file using its reference
       -- tell property list file pMWLPrefsFilePOSIXPath -- trigger the file using its pathname works to
       -- tell property list file myPropertyListFile -- your awful mixture fails logically
       make new property list item at end with properties {kind:URL, name:"pModOrderFileURL", value:anURL1}
       make new property list item at end with properties {kind:list, name:"pModOrderList", value:myList1}
       make new property list item at end with properties {kind:string, name:"pModInfoList", value:infos1}
   end tell
   -- end tell
   delay 2
   -- Test Reading Back here, works ok

   -- tell property list file pMWLPrefsFilePOSIXPath
   tell myPropertyListFile
       set pModOrderFileURL to (value of property list item "pModOrderFileURL") --> "file:///Users/**********/Library/"
       set pModOrderList to value of property list item "pModOrderList" --> {"Peter", "Paul", "Mary"}
       set pModInfoList to value of property list item "pModInfoList" --> "were American"
   end tell
end tell
(*
It does this if it doesn’t find a prefs file, to create one.

Then on subsequent Runs of the Script I want to restore the properties from the file. I was using the file path, but this is wrong, it need to be the File Object.
*)

delay 2
--
--

set myList2 to {"John", "Paul", "Ringo"}
set infos2 to "were English"
set alias2 to path to documents folder -- No need for “tell me” because we aren't in a tell application block
tell application "System Events"
   set anURL2 to URL of alias2
   tell property list file pMWLPrefsFilePOSIXPath
       set value of property list item "pModOrderFileURL" to anURL2
       set value of property list item "pModOrderList" to myList2
       set value of property list item "pModInfoList" to infos2
   end tell
end tell
delay 2
tell application "System Events"
   tell property list file pMWLPrefsFilePOSIXPath
       set pModOrderFileURL to (value of property list item "pModOrderFileURL") --> "file:///Users/**********/Documents/"
       set pModOrderList to value of property list item "pModOrderList" --> {"John", "Paul", "Ringo"}
       set pModInfoList to value of property list item "pModInfoList" --> "were English"
   end tell
end tell

The documentation IS correct. The attention paid to it during your reading was not !
If you return to your original thread you will see that I already used correct syntax which you didn't re-use.

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) jeudi 30 avril 2020 22:43:23

Edited to show two syntaxes required to achieve the same task in two different environments.

Last edited by Yvan Koenig (2020-04-30 03:51:32 pm)

Offline

 

#4 2020-04-30 03:09:43 pm

Yvan Koenig
Member
Registered: 2006-09-14
Posts: 4533

Re: How to get a Property File Object for an existing Plist File

Oops I forgot to post the alternate syntax usable when a file already exists.

Applescript:

set pMWLPrefsFilePOSIXPath to POSIX path of ((path to desktop as text) & "myList.plist") -- was not defined

tell application "System Events"
   -- Open the file and return a reference to it
   if exists disk item pMWLPrefsFilePOSIXPath then
       -- Creates a reference to the property list file
       set myPropertyListFile to property list file pMWLPrefsFilePOSIXPath
   else
       error "The file “" & pMWLPrefsFilePOSIXPath & "” is not available."
   end if
   tell myPropertyListFile
       set pModOrderFileURL to (value of property list item "pModOrderFileURL") --> "file:///Users/**********/Documents/"
       set pModOrderList to value of property list item "pModOrderList" --> {"John", "Paul", "Ringo"}
       set pModInfoList to value of property list item "pModInfoList" --> "were English"
   end tell
end tell

CAUTION. We may start with a file object to create a property list file.
But, although I don't remember having see that written, to read it, it seems that the descriptor MUST be a POSIX path (or a reference to such item).
My attempts to use an alias or a file («class furl») failed with this kind of error:

error "Erreur dans System Events : Impossible de convertir alias \"SSD 1000:Users:**********:Desktop:myList.plist\" en type integer." number -1700 from alias "SSD 1000:Users:**********:Desktop:myList.plist" to integer

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) jeudi 30 avril 2020 23:08:25

Last edited by Yvan Koenig (2020-04-30 03:25:40 pm)

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)