-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathclipboard.lua
148 lines (119 loc) · 3.75 KB
/
clipboard.lua
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
-- ClipboardManager
-- based on TextClipboardHistory.spoon by Diego Zamboni
-- https://github.com/Hammerspoon/Spoons/blob/28c3aa65e2de1b12a23659544693f06dd4dc9836/Source/TextClipboardHistory.spoon/init.lua
-- License: MIT - https://opensource.org/licenses/MIT
--
local clipboard = {}
clipboard.frequency = 0.8
clipboard.maxItems = 100
clipboard.ignoredIdentifiers = {
["org.nspasteboard.TransientType"] = true,
["org.nspasteboard.ConcealedType"] = true,
["org.nspasteboard.AutoGeneratedType"] = true
}
clipboard.ignoredApplications = {
["com.markmcguill.strongbox.mac"] = true
}
clipboard.emptyHistoryItem = {
text = "《Clipboard is empty》",
image = hs.image.imageFromName("NSCaution")
}
clipboard.clearHistoryItem = {
text = "《Clear Clipboard History》",
action = "clear",
image = hs.image.imageFromName("NSTrashFull")
}
----------------------------------------------------------------------
local itemSetting = "clipboard.items"
local clipboardHistory
local chooser
local previousFocusedWindow
local function getItem(content)
return {
text = content:gsub("\n", " "):gsub("%s+", " "),
subText = #content .. " characters",
data = content
}
end
local function dedupeAndResize(list)
local result = {}
local hashes = {}
for _, v in ipairs(list) do
if #result >= clipboard.maxItems then
break
end
local hash = hs.hash.MD5(v)
if not hashes[hash] then
table.insert(result, v)
hashes[hash] = true
end
end
return result
end
local function addContentToClipboardHistory(content)
table.insert(clipboardHistory, 1, content)
clipboardHistory = dedupeAndResize(clipboardHistory)
end
local function processSelectedItem(value)
local actions = {
clear = clipboard.clearAll
}
if previousFocusedWindow ~= nil then
previousFocusedWindow:focus()
end
if value == nil or type(value) ~= "table" then
return
end
if value.action and actions[value.action] then
actions[value.action]()
elseif value.data then
addContentToClipboardHistory(value.data)
hs.pasteboard.setContents(value.data)
hs.eventtap.keyStroke({ "cmd" }, "v")
end
end
local function populateChooser()
local menuData = hs.fnutils.imap(clipboardHistory, getItem)
table.insert(menuData, #menuData == 0 and clipboard.emptyHistoryItem or clipboard.clearHistoryItem)
return menuData
end
local function shouldIgnoreLatestPasteboardEntry()
if hs.fnutils.some(hs.pasteboard.pasteboardTypes(), function(v) return clipboard.ignoredIdentifiers[v] end) then
return true
end
if hs.fnutils.some(hs.pasteboard.contentTypes(), function(v) return clipboard.ignoredIdentifiers[v] end) then
return true
end
if clipboard.ignoredApplications[hs.application.frontmostApplication():bundleID()] then
return true
end
return false
end
local function handleNewPasteboardContent(content)
if shouldIgnoreLatestPasteboardEntry() or content == nil then
return
end
addContentToClipboardHistory(content)
end
function clipboard.start()
clipboardHistory = {}
chooser = hs.chooser.new(processSelectedItem)
chooser:choices(populateChooser)
hs.pasteboard.watcher.interval(clipboard.frequency)
pasteboardWatcher = hs.pasteboard.watcher.new(handleNewPasteboardContent)
pasteboardWatcher:start()
end
function clipboard.clearAll()
hs.pasteboard.clearContents()
clipboardHistory = {}
end
function clipboard.toggleClipboard()
if chooser:isVisible() then
chooser:hide()
return
end
chooser:refreshChoicesCallback()
previousFocusedWindow = hs.window.focusedWindow()
chooser:show()
end
return clipboard