Skip to content

Instantly share code, notes, and snippets.

@JavadocMD
Created November 12, 2016 00:18
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save JavadocMD/b4ea4da3ee7b571867b4c7128ab6e50d to your computer and use it in GitHub Desktop.
Save JavadocMD/b4ea4da3ee7b571867b4c7128ab6e50d to your computer and use it in GitHub Desktop.
A script to open Chrome bookmarks or entire folders of bookmarks by name. (Windows)
#!/usr/bin/env python
import os, subprocess, json
# Get all bookmarks matching the given name(s)
def get_bookmarks(profile_dir, names):
with open(os.path.join(profile_dir, 'Bookmarks')) as f:
j = json.load(f)
results = []
# There are two root-level bookmark folders: one for the bookmark bar and one for all others.
results.extend(search_bookmark(j['roots']['bookmark_bar'], names))
results.extend(search_bookmark(j['roots']['other'], names))
return results
# Recurse through a bookmark structure accumulating matches
# Does not recursively explore a matched folder: just adds its direct children
def search_bookmark(bm, names):
acc = []
if bm['name'] in names:
if bm['type'] == 'url':
acc.append(bm['url'])
elif bm['type'] == 'folder':
for child in bm['children']:
if child['type'] == 'url':
acc.append(child['url'])
elif bm['type'] == 'folder':
for child in bm['children']:
acc.extend(search_bookmark(child, names))
return acc
def open_in_chrome(chrome, profile, *urls):
cmd = '"{0}" --profile-directory="{1}" {2}'.format(chrome, profile, " ".join(map(str,urls)))
subprocess.Popen(cmd)
# Basic validation for path to Chrome executable
def chrome_exe(s):
if not s.endswith('chrome.exe'):
msg = '"{0}" does not appear to be the path to a Chrome executable (must end in chrome.exe)'.format(s)
raise argparse.ArgumentTypeError(msg)
if not os.path.isfile(s):
msg = '"{0}" not found'.format(s)
raise argparse.ArgumentTypeError(msg)
return s
# Basic validation for path to Chrome's user data
def existing_dir(s):
if not os.path.isdir(s):
msg = '"{0}" not found'.format(s)
raise argparse.ArgumentTypeError(msg)
return s
if __name__ == "__main__":
import argparse
# Defaults
default = dict()
default['chrome'] = 'C:\Program Files (x86)\Google\Chrome\Application\chrome.exe'
default['chrome_user_data'] = os.path.join(os.getenv('LOCALAPPDATA'), 'Google', 'Chrome', 'User Data')
default['profile'] = 'Default'
argp = argparse.ArgumentParser(description='Opens a Chrome Bookmark or Bookmark Folder')
argp.add_argument('bookmark', nargs='+', help='The name of the bookmark or folder to open.')
argp.add_argument('--profile', '-p', help='The Chrome name of the profile to use; e.g., "Default", "Profile 1", "Profile 2", etc.', default=default['profile'])
argp.add_argument('--chrome', '-c', type=chrome_exe, help='The full path to the Chrome executable.', default=default['chrome'])
argp.add_argument('--chrome-user-data', '-u', type=existing_dir, help='The full path to Chrome user data storage.', default=default['chrome_user_data'])
args = argp.parse_args()
profile_dir = os.path.join(args.chrome_user_data, args.profile)
urls = get_bookmarks(profile_dir, args.bookmark)
if len(urls) > 0:
open_in_chrome(args.chrome, args.profile, *urls)
else:
print 'No bookmarks found.'
@sergio-augusto-tech
Copy link

could you help me? i want to run this script but i do not know how ot run pyhton or how to pass arguments into it

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment