-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathChromeBookmarkEditor.py
75 lines (53 loc) · 1.72 KB
/
ChromeBookmarkEditor.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#!/usr/bin/python
# Hide python rocket ship from popping up in Dock when run.
import AppKit
info = AppKit.NSBundle.mainBundle().infoDictionary()
info['CFBundleIconFile'] = u'PythonApplet.icns'
info['LSUIElement'] = True
from ScriptingBridge import SBApplication
class ChromeApp(object):
def __init__(self):
self.chrome = SBApplication.applicationWithBundleIdentifier_("com.google.Chrome")
class Chrome(ChromeApp):
def __init__(self):
super(Chrome, self).__init__()
self.bookmarksBar = Folder(self.chrome.bookmarksBar())
self.otherBookmarks = Folder(self.chrome.otherBookmarks())
class Folder(ChromeApp):
def __init__(self, root):
super(Folder, self).__init__()
self.root = root
self.folders = self.root.bookmarkFolders()
self.bookmarks = self.root.bookmarkItems()
def title(self):
return self.root.title()
def setTitle_(self, title):
self.root.setTitle_(title)
def delete(self):
self.root.delete()
def getFolder(self, title):
for folder in self.folders:
if str(folder.title()) == title:
return Folder(folder)
return None
def getBookmark(self, title):
for bookmark in self.bookmarks:
if str(bookmark.title()) == title:
return bookmark
return None
def addFolder(self, title):
properties = dict(
title=title
)
new_folder = self.chrome.classForScriptingClass_("bookmark folder").alloc().initWithProperties_(properties)
self.folders.append(new_folder)
def addBookmark(self, title, url):
properties = dict(
title=title,
URL=url
)
new_bookmark = self.chrome.classForScriptingClass_("bookmark item").alloc().initWithProperties_(properties)
self.bookmarks.append(new_bookmark)
def removeAll(self):
for item in (self.folders + self.bookmarks):
item.delete()