-
-
Notifications
You must be signed in to change notification settings - Fork 18
/
main.py
executable file
·250 lines (224 loc) · 9.12 KB
/
main.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
#!/usr/bin/python
# -*- coding: UTF-8 -*-
# ~/Library/Application Support/com.apple.sharedfilelist/
# https://www.mac4n6.com/blog/2017/10/17/script-update-for-macmrupy-v13-new-1013-sfl2-mru-files
import os
import re
import sys
import json
import time
import pinyin
import plistlib
import ccl_bplist
from mac_alias import Bookmark
try:
from urllib import unquote # Python 2.X
except ImportError:
from urllib.parse import unquote # Python 3+
def BLOBParser_human(blob):
# http://mac-alias.readthedocs.io/en/latest/bookmark_fmt.html
try:
b = Bookmark.from_bytes(blob)
return "/" + u"/".join(b.get(0x1004, default=None))
except Exception as e:
print(e)
# for 10.13
def ParseSFL2(MRUFile):
itemsLinkList = []
try:
with open(MRUFile, "rb") as plistfile:
plist = ccl_bplist.load(plistfile)
plist_objects = ccl_bplist.deserialise_NsKeyedArchiver(
plist, parse_whole_structure=True)
if plist_objects["root"]["NS.keys"][0] == "items":
for item in plist_objects["root"]["NS.objects"][0]["NS.objects"]:
attributes = dict(zip(item["NS.keys"], item["NS.objects"]))
# print(attributes["Bookmark"].decode("iso-8859-1"))
if "Bookmark" in attributes:
if isinstance(attributes["Bookmark"], str):
itemLink = BLOBParser_human(attributes["Bookmark"])
elif isinstance(attributes["Bookmark"], bytes):
itemLink = BLOBParser_human(attributes["Bookmark"])
else:
itemLink = BLOBParser_human(attributes["Bookmark"]['NS.data'])
# print(itemLink)
itemsLinkList.append(itemLink)
return itemsLinkList
except Exception as e:
print("#ERROR:", e)
# for 10.11, 10.12
def ParseSFL(MRUFile):
itemsLinkList = []
try:
with open(MRUFile, "rb") as plistfile:
plist = ccl_bplist.load(plistfile)
plist_objects = ccl_bplist.deserialise_NsKeyedArchiver(
plist, parse_whole_structure=True)
if plist_objects["root"]["NS.keys"][2] == "items":
items = plist_objects["root"]["NS.objects"][2]["NS.objects"]
for n, item in enumerate(items):
# item["URL"]['NS.relative'] file:///xxx/xxx/xxx
filePath = item["URL"]['NS.relative'][7:]
# /xxx/xxx/xxx/ the last "/" make basename func not work
if filePath[-1] == '/':
filePath = filePath[:-1]
itemsLinkList.append(filePath)
return itemsLinkList
except Exception as e:
print(e)
# for Finder
def ParseFinderPlist(MRUFile):
itemsLinkList = []
try:
with open(MRUFile, "rb") as plistfile:
plist = ccl_bplist.load(plistfile)
for item in plist["FXRecentFolders"]:
if "file-bookmark" in item:
blob = item["file-bookmark"]
elif "file-data" in item:
blob = item["file-data"]["_CFURLAliasData"]
itemLink = BLOBParser_human(blob)
# exclude / path
if itemLink == "/":
continue
itemsLinkList.append(itemLink)
return itemsLinkList
except Exception as e:
print(e)
# for Sublime Text 3
def ParseSublimeText3Session(sessionFile):
with open(sys.argv[1]) as ff:
jsonObj = json.load(ff)
itemsLinkList = jsonObj["settings"]["new_window_settings"]["file_history"][0:15]
return itemsLinkList
# deprecated
def ParseMSOffice2016Plist(MRUFile):
itemsLinkList = []
try:
plistfile = plistlib.readPlist(MRUFile)
for item in plistfile:
itemsLinkList.append(unquote(item[7:]).decode('utf8'))
return itemsLinkList
except Exception as e:
# When read binary property list
# xml.parsers.expat.ExpatError: not well-formed (invalid token)
print(e)
# deprecated
def ParseMSOffice2019Plist(MRUFile):
itemsLinkList = []
try:
with open(MRUFile, "rb") as plistfile:
plist = ccl_bplist.load(plistfile)
itemsLinkList = [unquote(item[7:].encode("utf8")) for item in plist.keys()]
return itemsLinkList
except Exception as e:
# When read xml property list
# ccl_bplist.BplistError: Bad file header
print(e)
def ParseMSOfficePlist(MRUFile):
itemsLinkList = []
try:
# office 2019
with open(MRUFile, "rb") as plistfile:
plist = ccl_bplist.load(plistfile)
itemsLinkList = [unquote(item[7:].encode("utf8")).decode('utf8') for item in plist.keys()]
return itemsLinkList
except ccl_bplist.BplistError as e:
# office 2016
plistfile = plistlib.readPlist(MRUFile)
for item in plistfile:
itemsLinkList.append(unquote(item[7:]).decode('utf8'))
return itemsLinkList
except Exception as e:
print(e)
def checkFileList(fileList):
newFileList = []
excludedFolderList = os.environ["ExcludedFolders"].split(":") if os.environ["ExcludedFolders"] else []
excludedFilesList = os.environ["ExcludedFiles"].split(":") if os.environ["ExcludedFiles"] else []
for filePath in fileList:
if not os.path.exists(filePath):
continue
fileExclude = False
for exFilePath in excludedFilesList:
exFilePath = os.path.expanduser(exFilePath)
# 检测是否为文件路径
if not os.path.isfile(exFilePath):
break
if os.path.samefile(filePath, exFilePath):
fileExclude = True
break
for exFolderPath in excludedFolderList:
exFolderPath = os.path.expanduser(exFolderPath)
# 检测是否为文件夹路径
if not os.path.isdir(exFolderPath):
continue
# change "/xxx/xx" to "/xxx/xx/"
# for distinguish "/xxx/xx" and "/xxx/xx x/xx"
exFolderPath = os.path.join(exFolderPath, "")
# change "/xxx/xx" to "/xxx/xx/" for comparing exFolderPath "/xxx/xx/" and filePath "/xxx/xx"
fileFullPath = (filePath + "/") if os.path.isdir(filePath) else filePath
if fileFullPath.startswith(exFolderPath):
fileExclude = True
break
if not fileExclude:
newFileList.append(filePath)
return newFileList
# convert "abc你好def" to "abc ni hao def"
def convert2Pinyin(filename):
# convert "你好" to " ni hao "
def c2p(matchObj):
return " " + pinyin.get(matchObj.group(), format="strip", delimiter=" ") + " "
# replace chinese character with pinyin
return re.sub(u'[\u4e00-\u9fff]+', c2p, filename)
if __name__ == '__main__':
allItemsLinkList = []
for filePath in sys.argv[1:]:
if filePath.endswith(".sfl2"):
if __debug__: print("#FileType: sfl2") # noqa
itemsLinkList = ParseSFL2(filePath)
if filePath.endswith(".sfl3"):
if __debug__: print("#FileType: sfl3") # noqa
itemsLinkList = ParseSFL2(filePath)
elif filePath.endswith(".sfl"):
if __debug__: print("#FileType: sfl") # noqa
itemsLinkList = ParseSFL(filePath)
elif filePath.endswith("com.apple.finder.plist"):
if __debug__: print("#FileType: com.apple.finder.plist") # noqa
itemsLinkList = ParseFinderPlist(filePath)
elif filePath.endswith(".securebookmarks.plist"):
if __debug__: print("#FileType: .securebookmarks.plist") # noqa
itemsLinkList = ParseMSOfficePlist(filePath)
elif filePath.endswith(".sublime_session"):
if __debug__: print("#FileType: sublime_session") # noqa
itemsLinkList = ParseSublimeText3Session(filePath)
allItemsLinkList.extend(itemsLinkList)
allItemsLinkList = checkFileList(allItemsLinkList)
# use current app to open recent documents of current app
currentAppPath = os.getenv("currentAppPath")
if currentAppPath:
result = {"variables": {"currentAppPath": currentAppPath}, "items": []}
else:
result = {"items": []}
for n, item in enumerate(allItemsLinkList):
# remove records of file not exist
if not os.path.exists(item):
continue
modifiedTimeSecNum = os.path.getmtime(item)
modifiedTime = time.strftime("%m-%d %H:%M", time.localtime(modifiedTimeSecNum))
filename = os.path.basename(item)
temp = {
"type": "file",
"title": filename,
"autocomplete": filename,
# replace "." with space for searching filename extension
"match": filename.replace('.', ' ') + " " + convert2Pinyin(filename),
"icon": {"type": "fileicon", "path": item},
"subtitle": u"❖" + modifiedTime + u" ❥" + item.replace(os.environ["HOME"], "~"),
"arg": item
}
result['items'].append(temp)
if result['items']:
print(json.dumps(result))
else:
# when result list is empty
print('{"items": [{"title": "None Recent Record","subtitle": "(*´・д・)?"}]}')