Add Google Chrome bookmarks

Sharing and discussing custom actions for LaunchBar
waywalker
Posts: 16
Joined: Tue Oct 27, 2009 6:55 pm

Add Google Chrome bookmarks

Post by waywalker » Wed Jun 02, 2010 2:36 am

This script exports Google Chrome bookmarks, which then can be added to LaunchBar as a "Custom HTML Bookmarks File".

  1. Download ChromeExport.jar (see this article for more info)
  2. Copy it to "/usr/local/bin"
  3. Create the following shell script (for example, "~/Export-Chrome-Bookmarks.sh"):

    Code: Select all


    #!/bin/bash

    # modify the path if necessary
    bookmark_file="$HOME/chrome-bookmarks"

    # exports Google Chrome bookmarks
    java -jar /usr/local/bin/ChromeExport.jar > "$bookmark_file"

    # converts Google Chrome bookmarks file to UTF-8 (required for LaunchBar)
    iconv --from-code=MACROMAN --to-code=UTF-8 "$bookmark_file" > "$bookmark_file.html"

  4. Add it to crontab. For example, to run it every hour, add

    Code: Select all

    0   *   *   *   *   /Users/*your-username*/Export-Chrome-Bookmarks.sh > /dev/null 2>&1
  5. In LaunchBar Index select "+" > Web Bookmarks > Custom HTML Bookmarks File... and select the bookmarks file

frustratedlbuser
Posts: 1
Joined: Thu Jun 03, 2010 2:39 am

Re: Add Google Chrome bookmarks

Post by frustratedlbuser » Thu Jun 03, 2010 2:44 am

Thanks for the script, but I'm having trouble with cron/crontab. I can't seem to access the damn thing! I've googled around and really run into no useful help. Below is what I am constantly running into, followed by potential solutions I've tried.

Code: Select all

USER-NAMEs-macbook:~ USERNAME$ crontab -u USERNAME
crontab: must be privileged to use -u
USER-NAMEs-macbook:~ USERNAME$


For this I made sure /usr/lib/cron/cron.deny did not exist. /usr/lib/cron/cron.allow also did not exist. I created it with "sudo pico cron.allow" and had USERNAME as the only text in the file. This did not work. I also turned on the root user via the Accounts System Preference and tried doing everything as root, also with no luck.

I know this isn't exactly a cron support forum but with LaunchBar utterly silent on this, we've got no choice but to ask here.

sjk
Wizard
Wizard
Posts: 315
Joined: Thu Sep 13, 2007 9:46 pm
Location: Eugene
Contact:

Re: Add Google Chrome bookmarks

Post by sjk » Thu Jun 03, 2010 7:26 am

Try:

crontab -e

… in your favorite shell to edit the current user's crontab file. I don't recommending doing it as root.

To search for crontab-related info on the Apple Developer Site, do a Google search for:

site:developer.apple.com crontab

madAdam
Posts: 7
Joined: Fri Jun 25, 2010 10:32 pm

Re: Add Google Chrome bookmarks

Post by madAdam » Sat Jun 26, 2010 4:04 pm

The shell script works perfectly, thanks! It's wonderful to have my Chome bookmarks in Launchbar.

Unfortunately, I can't get cron to run the shell script. I suspect it's because I'm on Snow Leopard, which handles cron differently than earlier versions of OS X. If this is the case, does anyone have a cron fix for Snow Leopard or a solution using the now-preferred launchd?

Please note, I am not a programmer but can usually hack around enough to get things working. :)

Thanks again for your help.

waywalker
Posts: 16
Joined: Tue Oct 27, 2009 6:55 pm

Re: Add Google Chrome bookmarks

Post by waywalker » Sat Jun 26, 2010 5:45 pm

I run Snow Leopard and the crontab seems to work just fine.

Here is what I did:

  • run Terminal
  • type "crontab -e"
  • type "i" to switch to the insert mode
  • paste the line above
  • type ":wq" and Enter

If the script still doesn't run, you may want to remove the " > /dev/null 2>&1" part and then run mail in Terminal to see the results of the script execution.

Alternatively, you can set up a recurring event in your iCal and configure it to run the script.

On another note, now that Safari 5 is out, I find it more enjoyable than Chrome 8)

madAdam
Posts: 7
Joined: Fri Jun 25, 2010 10:32 pm

Re: Add Google Chrome bookmarks

Post by madAdam » Sat Jun 26, 2010 7:10 pm

Thanks, but still can't get cron to work. Here's my non-programmer's workaround:

Run Xmarks bookmark synchronization in Chrome and in another browser of your choice (I use Firefox but Safari would work just as well)
Keep both browsers open so that when you add a bookmark in Chrome it will sync to FIrefox or Safari
Make sure LaunchBar is configured to index your Firefox or Safari bookmarks
As long as Chrome remains your default browser, Launchbar will open your bookmark abbreviations in Chrome

sjk
Wizard
Wizard
Posts: 315
Joined: Thu Sep 13, 2007 9:46 pm
Location: Eugene
Contact:

Re: Add Google Chrome bookmarks

Post by sjk » Sat Jun 26, 2010 10:10 pm

madAdam wrote:Thanks, but still can't get cron to work.

You can run:

crontab -l

… in a Terminal window to list the contents (if any) of the crontab file.

ericdeko411
Posts: 2
Joined: Fri Oct 09, 2009 7:53 am
Contact:

Re: Add Google Chrome bookmarks

Post by ericdeko411 » Sun Jun 27, 2010 3:57 am

madAdam wrote:Thanks, but still can't get cron to work. Here's my non-programmer's workaround:

Run Xmarks bookmark synchronization in Chrome and in another browser of your choice (I use Firefox but Safari would work just as well)
Keep both browsers open so that when you add a bookmark in Chrome it will sync to FIrefox or Safari
Make sure LaunchBar is configured to index your Firefox or Safari bookmarks
As long as Chrome remains your default browser, Launchbar will open your bookmark abbreviations in Chrome



Thanks for posting something like this Madadam. having the same challenges and could not get this to work :(

madAdam
Posts: 7
Joined: Fri Jun 25, 2010 10:32 pm

Re: Add Google Chrome bookmarks

Post by madAdam » Mon Jun 28, 2010 8:12 pm

sjk wrote:You can run:

crontab -l

… in a Terminal window to list the contents (if any) of the crontab file.



I can create the crontab, no problem. It shows up when I run crontab -l.

Unfortunately, cron never runs the schedule.

After spending several hours troubleshooting cron to no avail, I decided to use my Xmarks workaround instead.

Thanks for trying to help but some of us are not adept enough at Unix and OS X programming to make this work. I wonder if using launchd would be any easier.

sjk
Wizard
Wizard
Posts: 315
Joined: Thu Sep 13, 2007 9:46 pm
Location: Eugene
Contact:

Re: Add Google Chrome bookmarks

Post by sjk » Tue Jun 29, 2010 12:21 am

madAdam wrote:Unfortunately, cron never runs the schedule.

Which version of OS X are you running? I've noticed cron-related issues on Leopard that have been fixed in Snow Leopard.

I wonder if using launchd would be any easier.

Having launchd run scripts directly should be more reliable than running them indirectly with launchd running cron, but writing launchd plist config files isn't necessarily easier than adding crontab entries. There are plenty of Apple-supplied examples in LaunchAgents and LaunchDaemons directories under /System/Library, even though many could be updated (e.g. to use KeepAlive instead of the deprecated OnDemand key).

kaz219
Posts: 12
Joined: Mon Jan 29, 2007 12:05 pm

Re: Add Google Chrome bookmarks

Post by kaz219 » Wed Jul 28, 2010 12:12 pm

Just for the sake of it: you could use xmarks to synchronize your bookmarks between chrome and firefox, and since launchbar indexes Firefox’ Bookmarks… You just would have to start firefox from time to time, to keep it in sync.

Since I will keep Firefox around for a while (its add-ons are still the best by far), it’s the simplest solution for me.

dwnfthr
Posts: 3
Joined: Thu Sep 16, 2010 8:53 am

Re: Add Google Chrome bookmarks

Post by dwnfthr » Thu Sep 16, 2010 7:05 pm

Thank you for making the instructions and the shell script!

waywalker wrote:
  • run Terminal
  • type "crontab -e"
  • type "i" to switch to the insert mode
  • paste the line above
  • type ":wq" and Enter



Thank you for these instructions. I would have been helpless without them.

In the last step, I believe you have to hit ESC before entering the text, and the text should be ":wq!".

janedenone
Posts: 1
Joined: Fri Oct 22, 2010 9:43 pm

Re: Add Google Chrome bookmarks

Post by janedenone » Fri Oct 22, 2010 9:47 pm

Hi,

I appreciate Dan Fraser's work, but this can be done without mixing Java and bash scripting (Python 2.6 or later required for the json module, so it works out of the box in OS X 10.6):

Code: Select all

#!/usr/bin/python

import json
import datetime
import cgi
import os
import sys

user = os.getlogin()
input_filename = "/Users/%s/Library/Application Support/Google/Chrome/Default/Bookmarks" % user
# modify if necessary
output_filename = "/Users/%s/Documents/chrome-bookmarks.html" % user

input_file = open(input_filename)
bookmark_data = json.load(input_file)
output_file = open(output_filename, 'w', 0)
sys.stdout = output_file

def print_bookmarks(bookmarks):
   for entry in bookmarks:
      if entry['type'] == 'folder':
         if not len(entry['children']) == 0:
            print ('<DT><H3 FOLDED ADD_DATE="%s">%s</H3>' % (entry['date_added'], entry['name'])).encode('utf-8')
            next_folder = entry['children']
            print '<DL><p>'
            print_bookmarks(next_folder)
            print '</DL><p>'
      else:
         print ('<DT><A DATE_ADDED="%s" HREF="%s">%s</A>' % (entry['date_added'], cgi.escape(entry['url']), entry['name'])).encode('utf-8')


print '<!DOCTYPE NETSCAPE-Bookmark-file-1>\n<Title>Bookmarks</Title>\n<H1>Bookmarks</H1><DL>\n'

roots = bookmark_data['roots']

for entry in roots:
   print ('<DT><H3 FOLDED ADD_DATE="%s">%s</H3>' % (roots[entry]['date_added'], entry)).encode('utf-8')
   print_bookmarks(roots[entry]['children'])
   print "</DL><p>"
   
print "</DL>"


Save this as a script, schedule it via crontab, and you're done.

- Jan

teedoff087
Posts: 14
Joined: Wed Sep 08, 2010 8:43 pm

Re: Add Google Chrome bookmarks

Post by teedoff087 » Sat Nov 13, 2010 5:20 am

janedenone wrote:Hi,

I appreciate Dan Fraser's work, but this can be done without mixing Java and bash scripting (Python 2.6 or later required for the json module, so it works out of the box in OS X 10.6):

Code: Select all

#!/usr/bin/python

import json
import datetime
import cgi
import os
import sys

user = os.getlogin()
input_filename = "/Users/%s/Library/Application Support/Google/Chrome/Default/Bookmarks" % user
# modify if necessary
output_filename = "/Users/%s/Documents/chrome-bookmarks.html" % user

input_file = open(input_filename)
bookmark_data = json.load(input_file)
output_file = open(output_filename, 'w', 0)
sys.stdout = output_file

def print_bookmarks(bookmarks):
   for entry in bookmarks:
      if entry['type'] == 'folder':
         if not len(entry['children']) == 0:
            print ('<DT><H3 FOLDED ADD_DATE="%s">%s</H3>' % (entry['date_added'], entry['name'])).encode('utf-8')
            next_folder = entry['children']
            print '<DL><p>'
            print_bookmarks(next_folder)
            print '</DL><p>'
      else:
         print ('<DT><A DATE_ADDED="%s" HREF="%s">%s</A>' % (entry['date_added'], cgi.escape(entry['url']), entry['name'])).encode('utf-8')


print '<!DOCTYPE NETSCAPE-Bookmark-file-1>\n<Title>Bookmarks</Title>\n<H1>Bookmarks</H1><DL>\n'

roots = bookmark_data['roots']

for entry in roots:
   print ('<DT><H3 FOLDED ADD_DATE="%s">%s</H3>' % (roots[entry]['date_added'], entry)).encode('utf-8')
   print_bookmarks(roots[entry]['children'])
   print "</DL><p>"
   
print "</DL>"


Save this as a script, schedule it via crontab, and you're done.

- Jan

Excellent! Works like a charm.

Instead of using cron, though, I used Lingon to create a launchd .plist file.

teedoff087
Posts: 14
Joined: Wed Sep 08, 2010 8:43 pm

Re: Add Google Chrome bookmarks

Post by teedoff087 » Sun Nov 14, 2010 10:49 am

Lingon didn't work correctly, on second glance. Must be due to the fact that it's been out of development for over 2 years. I've been messing around with launchd and it turns out that when used correctly, it is incredibly useful and effective. I've created a launchd .plist file (without Lingon) that will monitor the Chrome bookmarks file for changes and execute janedenone's bookmark export script (make sure you save a copy of the script to a directory in your home folder). I've made a few comments (denoted with "#" symbols, for non-programmers) that need to be customized by you before they will work correctly. Make sure you remove them before you save the .plist file.

First, you need to create a new file in your ~/Library/LaunchAgents/ directory using your favorite plain text editor (I use vim). It needs to be named something like com.blah.chrome_export.plist. I named mine org.myname.ExportChromeBookmarks.plist.

Code: Select all

<?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>Label</key>
   <string>org.myname.ExportChromeBookmarks</string> # This should be the same as the name of your .plist file except without the .plist extension.
   
   <key>ProgramArguments</key>
   <array>
      <string>/Users/myname/export_chrome_bookmarks.sh</string> # This should be the full path to the bookmark exporting script you saved. Make sure your script is executable otherwise this won't work (use the chmod +x command in terminal on the script).
   </array>   

   <key>WatchPaths</key>
   <array>
      <string>/Users/myname/Library/Application Support/Google/Chrome/Default/Bookmarks</string> # Full path to Chrome bookmark file. You should only have to change the username here.
   </array>

   <key>Nice</key>
   <integer>1</integer>
</dict>
</plist>

Once you've made your changes and saved the file to your ~/Library/LaunchAgents folder, you need to load it so launchd will run the script correctly. Open terminal and type:

Code: Select all

launchctl load ~/Library/LaunchAgents/com.myname.ExportChromeBookmarks.plist # make sure you use the filename you chose

You may have to run janedenon's exporting script once manually just so the html bookmarks are written the first time (for use in LaunchBar).

This works great for me. Hopefully someone else gets some use out of it.

Post Reply