How to read specific key value from plist file

Discussion in 'Mac Programming' started by saqibjaan, Sep 2, 2012.

  1. saqibjaan macrumors member

    Joined:
    Feb 1, 2012
    Location:
    Lahore
    #1
    Here is the History.plist of Safari browser. I want to read only 2 specific values from this plist file. (www.yahoo.com AND lastVisitedDate).
    How can I do it ? Kindly help.

    ?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
    <plist version="1.0">
    <dict>
    <key>WebHistoryDates</key>
    <array>
    <dict>
    <key></key>
    <string>http://www.yahoo.com/</string>
    <key>D</key>
    <array>
    <integer>1</integer>
    </array>
    <key>lastVisitedDate</key>
    <string>368300984.8</string>
    <key>title</key>
    <string>Yahoo!</string>
    <key>visitCount</key>
    <integer>1</integer>
    </dict>
     
  2. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #2
    Code:
    PLIST_BUDDY="/usr/libexec/PlistBuddy"
    THE_PLIST="$HOME/Library/Safari/History.plist"
    
    "$PLIST_BUDDY" -c "print :WebHistoryDates:0" "$THE_PLIST" \
     | awk '($1 == "=")  { url = $2 }
      /lastVisitedDate/ { print $3, url }'
    
    You may need to revise it, depending on your OS version.

    Also see this thread for more info.
     
  3. saqibjaan thread starter macrumors member

    Joined:
    Feb 1, 2012
    Location:
    Lahore
    #3
    Can you please tell me how to do this in Objective-C ?

    Kindly help me.
     
  4. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #4
    Read the Property List Programming Guide.

    See the section titled "Serializing a Property List", and the link to the NSPropertyListSerialization class.
     

Share This Page