Skip to content

Commit

Permalink
Add Snippet Trigger to paste citations
Browse files Browse the repository at this point in the history
  • Loading branch information
deanishe committed Jul 17, 2018
1 parent 4e4fc92 commit d246c39
Show file tree
Hide file tree
Showing 8 changed files with 171 additions and 39 deletions.
Binary file not shown.
125 changes: 106 additions & 19 deletions src/info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,19 @@
<false/>
</dict>
</array>
<key>256A2232-79C6-4956-9EDA-3EEDB0EE1CDF</key>
<array>
<dict>
<key>destinationuid</key>
<string>F9040898-2FD2-4C79-B34F-1EEAD90BE256</string>
<key>modifiers</key>
<integer>0</integer>
<key>modifiersubtext</key>
<string></string>
<key>vitoclose</key>
<false/>
</dict>
</array>
<key>2B20149E-1275-476A-9CAA-254CB2DC74E0</key>
<array>
<dict>
Expand Down Expand Up @@ -368,7 +381,7 @@
<array>
<dict>
<key>destinationuid</key>
<string>F9040898-2FD2-4C79-B34F-1EEAD90BE256</string>
<string>256A2232-79C6-4956-9EDA-3EEDB0EE1CDF</string>
<key>modifiers</key>
<integer>0</integer>
<key>modifiersubtext</key>
Expand All @@ -378,7 +391,7 @@
</dict>
<dict>
<key>destinationuid</key>
<string>A950947F-8EA7-4749-AF55-2C7F9D6E695E</string>
<string>CDDDFF55-9266-40A0-B22F-89192559E0E3</string>
<key>modifiers</key>
<integer>0</integer>
<key>modifiersubtext</key>
Expand Down Expand Up @@ -459,6 +472,19 @@
<false/>
</dict>
</array>
<key>CDDDFF55-9266-40A0-B22F-89192559E0E3</key>
<array>
<dict>
<key>destinationuid</key>
<string>A950947F-8EA7-4749-AF55-2C7F9D6E695E</string>
<key>modifiers</key>
<integer>0</integer>
<key>modifiersubtext</key>
<string></string>
<key>vitoclose</key>
<false/>
</dict>
</array>
<key>D78E4F18-6D77-4514-A55F-8894C014AB60</key>
<array>
<dict>
Expand Down Expand Up @@ -856,6 +882,25 @@ variables={allvars}
<key>version</key>
<integer>1</integer>
</dict>
<dict>
<key>config</key>
<dict>
<key>externaltriggerid</key>
<string>citations</string>
<key>passinputasargument</key>
<false/>
<key>passvariables</key>
<true/>
<key>workflowbundleid</key>
<string>self</string>
</dict>
<key>type</key>
<string>alfred.workflow.output.callexternaltrigger</string>
<key>uid</key>
<string>27AB4926-3249-4A80-85B5-6176911ACE7D</string>
<key>version</key>
<integer>1</integer>
</dict>
<dict>
<key>config</key>
<dict>
Expand Down Expand Up @@ -917,38 +962,38 @@ EOS</string>
<dict>
<key>config</key>
<dict>
<key>externaltriggerid</key>
<string>citations</string>
<key>passinputasargument</key>
<false/>
<key>passvariables</key>
<key>inputstring</key>
<string>{var:action}</string>
<key>matchcasesensitive</key>
<true/>
<key>workflowbundleid</key>
<string>self</string>
<key>matchmode</key>
<integer>0</integer>
<key>matchstring</key>
<string>show-citations</string>
</dict>
<key>type</key>
<string>alfred.workflow.output.callexternaltrigger</string>
<string>alfred.workflow.utility.filter</string>
<key>uid</key>
<string>27AB4926-3249-4A80-85B5-6176911ACE7D</string>
<string>2C059A51-AE82-49BA-A5E9-13824D15C4EC</string>
<key>version</key>
<integer>1</integer>
</dict>
<dict>
<key>config</key>
<dict>
<key>inputstring</key>
<string>{var:action}</string>
<string>{var:autopaste}</string>
<key>matchcasesensitive</key>
<true/>
<key>matchmode</key>
<integer>0</integer>
<key>matchstring</key>
<string>show-citations</string>
<string></string>
</dict>
<key>type</key>
<string>alfred.workflow.utility.filter</string>
<key>uid</key>
<string>2C059A51-AE82-49BA-A5E9-13824D15C4EC</string>
<string>256A2232-79C6-4956-9EDA-3EEDB0EE1CDF</string>
<key>version</key>
<integer>1</integer>
</dict>
Expand Down Expand Up @@ -1022,6 +1067,7 @@ EOS</string>
<string>flags=()
test -n "$bibliography" &amp;&amp; flags+=(--bibliography)
test -n "$autopaste" &amp;&amp; flags+=(--paste)
./zh copy $flags "$style" "$id"</string>
<key>scriptargtype</key>
Expand Down Expand Up @@ -1070,6 +1116,27 @@ test -n "$bibliography" &amp;&amp; flags+=(--bibliography)
<key>version</key>
<integer>1</integer>
</dict>
<dict>
<key>config</key>
<dict>
<key>argument</key>
<string>.
/------- COPY CITATION IN --------\
query={query}
variables={allvars}
\---------------------------------/</string>
<key>cleardebuggertext</key>
<false/>
<key>processoutputs</key>
<true/>
</dict>
<key>type</key>
<string>alfred.workflow.utility.debug</string>
<key>uid</key>
<string>CDDDFF55-9266-40A0-B22F-89192559E0E3</string>
<key>version</key>
<integer>1</integer>
</dict>
<dict>
<key>config</key>
<dict>
Expand Down Expand Up @@ -1719,6 +1786,17 @@ Edit the `ZOTERO_DIR` variable to point to Zotero 5's data directory if you aren
<key>ypos</key>
<integer>1140</integer>
</dict>
<key>256A2232-79C6-4956-9EDA-3EEDB0EE1CDF</key>
<dict>
<key>colorindex</key>
<integer>4</integer>
<key>note</key>
<string>$autopaste is unset</string>
<key>xpos</key>
<integer>1290</integer>
<key>ypos</key>
<integer>420</integer>
</dict>
<key>27AB4926-3249-4A80-85B5-6176911ACE7D</key>
<dict>
<key>colorindex</key>
Expand Down Expand Up @@ -1814,7 +1892,7 @@ Edit the `ZOTERO_DIR` variable to point to Zotero 5's data directory if you aren
<key>colorindex</key>
<integer>4</integer>
<key>xpos</key>
<integer>1490</integer>
<integer>1690</integer>
<key>ypos</key>
<integer>590</integer>
</dict>
Expand Down Expand Up @@ -1988,7 +2066,7 @@ Edit the `ZOTERO_DIR` variable to point to Zotero 5's data directory if you aren
<key>note</key>
<string>Copy citation to pasteboard</string>
<key>xpos</key>
<integer>1250</integer>
<integer>1450</integer>
<key>ypos</key>
<integer>560</integer>
</dict>
Expand Down Expand Up @@ -2061,6 +2139,15 @@ Edit the `ZOTERO_DIR` variable to point to Zotero 5's data directory if you aren
<key>ypos</key>
<integer>590</integer>
</dict>
<key>CDDDFF55-9266-40A0-B22F-89192559E0E3</key>
<dict>
<key>colorindex</key>
<integer>4</integer>
<key>xpos</key>
<integer>1290</integer>
<key>ypos</key>
<integer>590</integer>
</dict>
<key>D78E4F18-6D77-4514-A55F-8894C014AB60</key>
<dict>
<key>note</key>
Expand Down Expand Up @@ -2111,9 +2198,9 @@ Edit the `ZOTERO_DIR` variable to point to Zotero 5's data directory if you aren
<key>note</key>
<string>Show "generating your citation" status message</string>
<key>xpos</key>
<integer>1250</integer>
<integer>1450</integer>
<key>ypos</key>
<integer>380</integer>
<integer>390</integer>
</dict>
<key>F912A5F2-6586-498F-8A32-C11B59C687F5</key>
<dict>
Expand All @@ -2137,7 +2224,7 @@ Edit the `ZOTERO_DIR` variable to point to Zotero 5's data directory if you aren
<string></string>
</dict>
<key>version</key>
<string>1.0.1</string>
<string>1.1</string>
<key>webaddress</key>
<string>https://github.com/deanishe/zothero</string>
</dict>
Expand Down
24 changes: 24 additions & 0 deletions src/lib/pasteboard.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,32 @@
from AppKit import NSPasteboard
from Foundation import NSData

from workflow.util import run_jxa

# Some common UTIs
UTI_HTML = 'public.html'
UTI_TEXT = 'public.rtf'
UTI_PLAIN = 'public.utf8-plain-text'
UTI_URL = 'public.url'
UTI_URL_NAME = 'public.url-name'

# JXA script to simulate CMD+V keypress via Carbon.
# Unaffected by other modifiers the user may be holding down, unlike
# System Events.
PASTE_SCRIPT = """
ObjC.import('Carbon');
var source = $.CGEventSourceCreate($.kCGEventSourceStateCombinedSessionState);
var pasteCommandDown = $.CGEventCreateKeyboardEvent(source, $.kVK_ANSI_V, true);
$.CGEventSetFlags(pasteCommandDown, $.kCGEventFlagMaskCommand);
var pasteCommandUp = $.CGEventCreateKeyboardEvent(source, $.kVK_ANSI_V, false);
$.CGEventPost($.kCGAnnotatedSessionEventTap, pasteCommandDown);
$.CGEventPost($.kCGAnnotatedSessionEventTap, pasteCommandUp);
"""


def nsdata(s):
"""Return an NSData instance for string `s`."""
Expand Down Expand Up @@ -52,3 +71,8 @@ def set(contents):
for uti in contents:
data = nsdata(contents[uti])
pboard.setData_forType_(data, uti.encode('utf-8'))


def paste():
"""Simulate CMD+V to paste clipboard."""
run_jxa(PASTE_SCRIPT)
1 change: 0 additions & 1 deletion src/lib/workflow/update.py
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,6 @@ def check_update(github_slug, current_version, prereleases=False):
wf().logger.warning('no valid releases for %s', github_slug)
wf().cache_data('__workflow_update_status', {'available': False})
return False
# raise ValueError('no valid releases for %s', github_slug)

wf().logger.info('%d releases for %s', len(releases), github_slug)

Expand Down
9 changes: 6 additions & 3 deletions src/lib/workflow/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -305,15 +305,18 @@ def appinfo(name):
AppInfo: :class:`AppInfo` tuple or ``None`` if app isn't found.
"""
cmd = ['mdfind', '-onlyin', '/',
cmd = ['mdfind', '-onlyin', '/Applications',
'-onlyin', os.path.expanduser('~/Applications'),
'(kMDItemContentTypeTree == com.apple.application &&'
'(kMDItemDisplayName == "{0}" || kMDItemFSName == "{0}.app"))'
.format(name)]

path = run_command(cmd).strip()
if not path:
output = run_command(cmd).strip()
if not output:
return None

path = output.split('\n')[0]

cmd = ['mdls', '-raw', '-name', 'kMDItemCFBundleIdentifier', path]
bid = run_command(cmd).strip()
if not bid: # pragma: no cover
Expand Down
2 changes: 1 addition & 1 deletion src/lib/workflow/version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.33
1.36
15 changes: 8 additions & 7 deletions src/lib/zothero/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@
# Entry from the Zotero database).
INDEX_SCHEMA = """
CREATE VIRTUAL TABLE search USING fts3(
id, title, year, creators, authors, editors,
tags, collections, attachments, notes, abstract, all
`id`, `title`, `year`, `creators`, `authors`, `editors`,
`tags`, `collections`, `attachments`, `notes`, `abstract`, `all`
);
CREATE TABLE modified (
Expand Down Expand Up @@ -374,13 +374,14 @@ def _update(self, zot, force=False):
# Fulltext search
sql = u"""
UPDATE search
SET title = ?, year = ?, creators = ?,
authors = ?, editors = ?,
tags = ?, collections = ?,
attachments = ?, notes = ?,
abstract = ?, all = ?
SET `title` = ?, `year` = ?, `creators` = ?,
`authors` = ?, `editors` = ?,
`tags` = ?, `collections` = ?,
`attachments` = ?, `notes` = ?,
`abstract` = ?, `all` = ?
WHERE id = ?
"""
# log.debug('sql=%s, data=%r', sql, data)
c.execute(sql, data[1:] + [e.id])

# JSON data
Expand Down
Loading

0 comments on commit d246c39

Please sign in to comment.