-
Notifications
You must be signed in to change notification settings - Fork 4
Example: using Zowie with DEVONthink
When using Zotero for reference management, you may on occasion want to work with the PDF files from outside of Zotero. For example, if you're a DEVONthink user, you will at some point discover the power of indexing your local Zotero database from DEVONthink. However, when viewing or manipulating the PDF files from outside of Zotero, you may run into the following problem: when looking at a given PDF file, how do you find out which Zotero entry it belongs to?
Using Zowie to write the Zotero select link into the Finder comments of every PDF file means that within DEVONthink, the Zotero link will appear in the Annotations & Reminders inspector, in the Finder Comments box. For example:
In my DEVONthink setup, I index the Zotero storage
folder where Zotero writes PDF attachments. Then, I use a smart rule in DEVONthink to run Zowie. The rule is set up to act on the indexed folder, and trigger when a file is added. Here is the rule definition:
Here is the script invoked by the smart rule:
# =======================================================================
# @file Run Zowie on newly indexed PDF.applescript
# @brief Script for DEVONthink smart rule to run Zowie on new additions
# @author Michael Hucka <[email protected]>
# @license MIT license; please see the file LICENSE in the repo
# @repo https://github.com/mhucka/devonthink-hacks
# =======================================================================
# The following function is based on code posted by user "mb21" on
# 2016-06-26 at https://stackoverflow.com/a/38042023/743730
on substituted(search_string, replacement_string, this_text)
set AppleScript's text item delimiters to the search_string
set the item_list to every text item of this_text
set AppleScript's text item delimiters to the replacement_string
set this_text to the item_list as string
set AppleScript's text item delimiters to ""
return this_text
end substituted
on performSmartRule(selectedRecords)
repeat with _record in selectedRecords
# In my environment, Zotero takes time to upload a newly-added
# PDF to the cloud. The following delay is needed to give time
# for the upload to take place, so that when Zowie runs and
# queries Zotero via the network API, the data will be there.
delay 30
set raw_path to the path of the _record
# Some chars in file names are problematic due to having special
# meanings in shell command strings. Need to quote them with 2
# blackslashes, b/c the 1st backslash will be removed when the
# shell command string is handed to the shell.
set sanitized_path to substituted("&", "\\\\&", raw_path)
# Another problem with file names is embedded single quotes. The
# combination of changing the text delimiter and using the
# AppleScript "quoted form of" below, seems to do the trick.
set AppleScript's text item delimiters to "\\\\"
set result to do shell script ¬
"/usr/local/bin/zowie -s -q " & (quoted form of sanitized_path)
# Display a DEVONthink notification if an error occurred.
if result is not equal to "" then
display notification result
end if
end repeat
end performSmartRule
The nice thing about having the Zotero select link accessible from DEVONthink is that you can create smart rules to manipulate it further. For example, I use the following smart rule to copy the Zotero select link from the Finder Comments to the "URL" field of each PDF file from my Zotero storage folder indexed in DEVONthink:
where the following is the simple embedded script:
on performSmartRule(theRecords)
tell application id "DNtp"
repeat with theRecord in theRecords
set URL of theRecord to (get comment of theRecord)
end repeat
end tell
end performSmartRule
Notice that the triggering event for "perform the action" is "every minute". The reason is the following: the file is already indexed and imported by DEVONthink, and what is done by the smart rule to run Zowie is merely to change the Finder comment on the file. None of the other DEVONthink rule event type will be triggered by such a change, so all that we are left with is the option to run the rule on a schedule.