No internet connection
  1. Home
  2. How to

Read specific item from PList

By John Michael Caldwell @johnmcald
    2023-03-03 23:12:45.236Z

    I don't think this has been covered yet and I'm having a hard time finding resources for this...

    Basically trying to get the value of an item or key in a plist.

    Current script:

    let plistValue = sf.file.readPList({
        path: '/Users/Shared/PList.plist'
    }).pList
    log(plistValue)
    

    This comes back with the entire plist. I know how to do it in AppleScript:

    sf.system.execAppleScript({
        script: `
        set plistPath to ":Users:Shared:PList.plist"
    	
    	tell property list file plistPath
    		set plistValue to get value of property list item "Item X"
    	end tell
        `
    });
    ;
    

    Must be something similar but I haven't found it yet. Everything on the internet is a little confusing to me. Hopefully someone here has an idea of how to accomplish this.

    Solved in post #2, click to view
    • 9 replies
    1. Kitch Membery @Kitch2023-03-04 02:13:50.557Z

      Hi @johnmcald,

      Without seeing the returned information I can't provide an exact solution, however...

      This script you provided should return a JSON object with includes key-value pairs.

      let plistValue = sf.file.readPList({
          path: '/Users/Shared/PList.plist'
      }).pList;
      
      log(plistValue);
      

      You can then use the following methods to get the keys, values & entries from the JSON.

      log(plistValue["KeyNameHere"]) //This will give you the value from the key-value pair. (This is probably what you are after.)
      
      log(Object.keys(plistValue)) //This will give you a list of all the keys.
      
      log(Object.values(plistValue)) //This will give you a list of all the values.
      
      log(Object.entries(plistValue)) //This will give you a list of all the entries. (The keys and Values)
      

      I hope that helps.
      Rock on!

      ReplySolution
      1. JJohn Michael Caldwell @johnmcald
          2023-03-04 03:04:20.546Z

          Ah! That worked perfectly! I don't know why I can't ever find this stuff...

          Thanks!

          1. Kitch Membery @Kitch2023-03-04 06:16:03.406Z

            Awesome! :-)

          2. In reply toKitch:
            JJohn Michael Caldwell @johnmcald
              2023-03-06 22:04:39.243Z

              @Kitch Alternativley, if I wanted to write to the same plist, how would I do that?

              I gather it's something like this but fumbling with how to target a certain key to change the value of it??...

              sf.file.writePList({
                  path: '/Users/Shared/PList.plist'
              });
              

              Is there a certain dictionary or reference to refer to that would have these answers as straightforward as you put them? I've checked the online SoundFlow documentation for plists and json but doesn't seem to get as specific as I need to understand it...

              Thanks!

              1. Kitch Membery @Kitch2023-03-06 23:44:50.021Z

                Hi @johnmcald,

                all you need to do is feed the JSON back into the writePList() method. Like this.

                sf.file.writePList({
                    path: '~/Desktop/PList.plist',
                    pList:{
                        "Property_1":"Value 1"
                    }
                });
                

                As far as documentation, it will be quite some time before we get to detailing information like this, so your best bet is to ask here in the forum. :-)

            • J
              In reply tojohnmcald:
              John Michael Caldwell @johnmcald
                2023-03-07 00:00:33.025Z

                Got it! And thank you!

                1. Kitch Membery @Kitch2023-03-07 00:11:51.197Z

                  Just a word of warning... overwriting files on your computer (like PList files) should be done with caution. As I'm not sure how well the writePList() method has been tested. You'd essentially be doing a double conversion of the PList file that could result in readability file type and formatting issues. AFAIK

                  1. In reply tojohnmcald:
                    Kitch Membery @Kitch2023-03-07 00:12:54.340Z

                    Reading PList files is fine though. But be sure to back up any Plist files before you write over them.

                    1. JJohn Michael Caldwell @johnmcald
                        2023-03-07 00:27:36.030Z

                        @Kitch Got it. Currently, this is specifically for a record/increment take number kind of script where I'll be reading a string, incrementing the number by 1 and writing that new number back to the plist as a string.

                        If it's unstable is it better to use SoundFlow's execute AppleScript to write to the plist? I would just need to find a way to pass a variable from the SoundFlow language to the AppleScript language if that's even possible...