forked from deanishe/alfred-pwgen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pasteboard.py
97 lines (72 loc) · 2.46 KB
/
pasteboard.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
# encoding: utf-8
#
# Copyright (c) 2017 Dean Jackson <[email protected]>
#
# MIT Licence. See http:https://opensource.org/licenses/MIT
#
# Created on 2017-04-01
#
"""
Pasteboard: private copy + paste
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Mark data copied to pasteboard as "concealed", so clipboard
managers ignore it.
"""
from __future__ import print_function, absolute_import
import subprocess
from AppKit import NSPasteboard
from Foundation import NSData
# setting org.nspasteboard.ConcealedType to anything
# informs clipboard managers to ignore these data
# http:https://nspasteboard.org
UTI_PRIVATE = 'org.nspasteboard.ConcealedType'
JS_PASTE = """
ObjC.import('Carbon');
// Simulate CMD+V keypress via Carbon. Unaffected by other modifiers
// the user may be holding down.
function paste() {
var src = $.CGEventSourceCreate($.kCGEventSourceStateCombinedSessionState);
var pasteCommandDown = $.CGEventCreateKeyboardEvent(src, $.kVK_ANSI_V, true);
$.CGEventSetFlags(pasteCommandDown, $.kCGEventFlagMaskCommand);
var pasteCommandUp = $.CGEventCreateKeyboardEvent(src, $.kVK_ANSI_V, false);
$.CGEventPost($.kCGAnnotatedSessionEventTap, pasteCommandDown);
$.CGEventPost($.kCGAnnotatedSessionEventTap, pasteCommandUp);
}
function run(argv) {
paste()
}
"""
def nsdata(obj):
"""Convert ``object`` to `NSData`."""
if isinstance(obj, unicode):
s = obj.encode('utf-8')
else:
s = str(obj)
return NSData.dataWithBytes_length_(s, len(s))
def copy(data, uti='public.utf8-plain-text', private=True):
"""Put ``data`` on pasteboard with type ``uti``.
If ``private`` is ``True`` (the default), the data are
marked as "concealed", so clipboard managers will ignore
them.
Args:
data (object): Data to put on pasteboard
uti (str, optional): UTI for data
private (bool, optional): Whether to hide the data from
clipboard managers
"""
pbdata = {uti: data}
if private:
pbdata[UTI_PRIVATE] = 'whateva'
pboard = NSPasteboard.generalPasteboard()
pboard.clearContents()
for uti, data in pbdata.items():
if isinstance(uti, unicode):
uti = uti.encode('utf-8')
pboard.setData_forType_(nsdata(data), uti)
def paste():
"""Simulate CMD+V.
Simulate keypress via Carbon, which unlike AppleScript,
ignores the keys the user has their fat fingers on.
"""
cmd = ['/usr/bin/osascript', '-l', 'JavaScript', '-e', JS_PASTE]
subprocess.check_call(cmd)