3 Replies Latest reply: Dec 19, 2010 4:02 AM by John Maisey
c_kyr Level 2 Level 2 (235 points)
Can I use applescript or automator to export safari bookmarks? I would like to periodically save an html copy of my bookmarks. Is it possible?
Thanks.

MacBook Pro i7 2.66GHz, Mac OS X (10.6.5)
  • 1. Re: applescript or automator to export safari bookmarks
    Tony T1 Level 6 Level 6 (8,380 points)
    c_kyr wrote:
    Can I use applescript or automator to export safari bookmarks? I would like to periodically save an html copy of my bookmarks. Is it possible?


    As far as I know, not to html. Bookmarks are in a binary .plist format. You can convert to a readable xml format with the following Applescript:

    <pre style="
    font-family: Monaco, 'Courier New', Courier, monospace;
    font-size: 10px;
    font-weight: normal;
    margin: 0px;
    padding: 5px;
    border: 1px solid #000000;
    width: 720px;
    color: #000000;
    background-color: #E6E6EE;
    overflow: auto;">
    do shell script "/usr/bin/plutil -convert xml1 $HOME/Library/Safari/Bookmarks.plist -o $HOME/Desktop/Bookmarks.xml"</pre>
  • 2. Re: applescript or automator to export safari bookmarks
    Pierre L. Level 4 Level 4 (3,860 points)
    The following short script should save an html copy of your Safari bookmarks on the desktop. What do you exactly mean by “periodically” though?


    *tell application "Safari" to activate*
    *tell application "System Events" to tell process "Safari"*
    *   click menu item "Export Bookmarks…" of menu 1 of menu bar item "File" of menu bar 1*
    *   keystroke "d" using command down* -- to save on the desktop
    *   click button "Save" of window 1*
    *   if sheet 1 of window 1 exists then click button "Replace" of sheet 1 of window 1*
    *end tell*


    Don't forget to activate [GUI Scripting|http://www.macosxautomation.com/applescript/uiscripting/index.html].

    Message was edited by: Pierre L.
  • 3. Re: applescript or automator to export safari bookmarks
    John Maisey Level 5 Level 5 (6,880 points)
    You can also use System Events to read the .plist file and build the HTML. Something like:

    set myHTML to cjmSafariBookmarkHTML()

    on cjmSafariBookmarkHTML()
    set urlListText to "<html><body>" & return & "<h1>Safari Bookmarks - " & (short date string of (current date)) & "</h1>" & return
    set theDBPath to (POSIX path of (((path to current user folder) as string) & "Library:Safari:Bookmarks.plist"))
    tell application "System Events"
    try
    set myBundle to value of property list items of property list file theDBPath
    repeat with mySet in (item 2 of myBundle)
    try
    if (|Children| of mySet) is not equal to {} then
    set urlListText to urlListText & "<h2>" & (|Title| of mySet) & "</h2>" & return
    set urlListText to urlListText & "<ul>" & return
    repeat with myDict in (|Children| of mySet)
    set urlListText to urlListText & "<li><a href=\"" & (URLString of myDict) & "\">" & (|title| of URIDictionary of myDict) & "</a></li>" & return
    end repeat
    set urlListText to urlListText & "<ul>" & return
    end if
    end try
    end repeat
    on error theErr
    activate
    display dialog theErr
    end try
    end tell
    set urlListText to urlListText & "</body></html>"
    return urlListText
    end cjmSafariBookmarkHTML



    You can then save the resulting text as a file.

    Best wishes

    John M

    Message was edited by: John Maisey - fixed formatting.