l i v e c o d e

stsReadFromPList
Mac OS X

Usage
stsReadFromPList(filePath,keyPath)
put stsReadFromPlist(pList,"NAT/Airport/NetworkName")
    >> "PowerMac G4"
put stsReadFromPlist(pList,"NAT/SharingDevices") into tDevicesA
    >> tDevicesA[1] = "en1"
Description
This function lets you read from an OS X pList file (unencoded - Tiger has some pLists encoded) using a "path" of keys in the file. If the value of the data is a simple string, integer, etc., the value is returned. If the value is an array (shown by "<array>" directly under the key in the plist), an array is returned. Note: This does not work with embedded arrays (i.e. <array><array>).

Code
Select the code below and copy it to the clipboard, or retrieve it from Scripter's Scrapbook Online.
function stsReadFromPlist pFile,pKeyPath
  if there is a file pFile then
    put url ("file:" & pFile) into tData
    set the itemDel to "/"
    repeat for each item tItem in pKeyPath
      put offset("" & tItem & "",tData) into tOffset
      if tOffset <> 0 then
        delete char 1 to (tOffset + length("" & tItem & "")) of tData
      else
        return "STSError: Key not found at path '" & pKeyPath & "'"
      end if
    end repeat
    -- remove preceding chars
    repeat
      if char 1 of tData = "<" then exit repeat
      delete char 1 of tData
    end repeat
    if char 1 to 7 of tData ="" then
      -- return an array
      if matchText(tData,"(?si)(.*?)<\/array>",tElements) then
        put "" into tRetValA
        put 0 into tKeyNum
        repeat
          add 1 to tKeyNum
          get matchText(tElements,"(?si)<.*?>(.*?)<\/.*?>",tVal)
          if it is false then exit repeat
          else
            put tVal into tRetValA[tKeyNum]
            get matchChunk(tElements,"(?si)(<.*?>(.*?)<\/.*?>)",tStart,tEnd)
            delete char 1 to tEnd of tElements
          end if
        end repeat
        return tRetValA
      else
        return "STSError: Could not extract array from key path '" & pKeyPath & "'"
      end if
    else
      -- look for  (like  - this is used in loginwindow.plist)
      if matchText(tData,"(?si)<(.*?)\/>",tVal) then
        return tVal
      else
        -- look for Val, Val, etc.
        if matchText(tData,"(?si)<.*?>(.*?)<\/.*?>",tVal) then
          return tVal
        end if
      end if
      return "STSError: Could not extract value from key path '" & pKeyPath & "'"
    end if
  else
    return "STSError: FIle not found."
  end if
end stsReadFromPlist

 Print this code entry

News and Rumors Products Services Developer Resources Contact STS About STS