Skip to content

Instantly share code, notes, and snippets.

View zverhope's full-sized avatar

Zachary Verleyen-Hope zverhope

View GitHub Profile
@zverhope
zverhope / bookends-generate_bib.py
Last active December 30, 2020 13:35
Auto-generate and regularly update bib file from Bookends library
#!/usr/bin/env -S PATH="${PATH}:/usr/local/bin" PYTHONIOENCODING=UTF-8 LC_ALL=en_US.UTF-8 python
# -*- coding: utf-8 -*-
then = "20201229160152"
import re, shutil, sys
import os.path, time
from datetime import datetime as dt
import json
from subprocess import Popen, PIPE
set myJSONFile to "/Users/zhope/Dropbox/Sundry/Library.json"
set jsonFile to POSIX file myJSONFile
tell application "Keyboard Maestro Engine" to set libraryLastUpdate to getvariable "libraryNextUpdate"
set lastUpdate to date libraryLastUpdate
set theDate to current date
set theSeconds to theDate - lastUpdate
tell application "System Events"
if exists file myJSONFile then
set myBibFile to "/Users/zhope/Dropbox/Sundry/Library.bib"
if myBibFile exists then
set currentBibs to do shell script "/usr/local/bin/pandoc-citeproc --bib2json " & quoted form of myBibFile & " | /usr/local/bin/jq -r ' .[] | \"\\(.id)\"'"
set currentBibs to paragraphs of currentBibs
end if
set bibFile to POSIX file myBibFile
my write_to_file("", bibFile, true)
tell application "Bookends"
set theIDs to «event ToySRUID» "Selection"
repeat with theID in paragraphs of theIDs
tell front library window
try
set myRefs to (publication items whose id is theID)
set myItem to first item of myRefs
set thePath to path of attachment items of myItem
tell application "Bookends"
tell front library window
set theIDs to get id of publication items of group item "Unlinked Attachments"
repeat with theID in theIDs
try
set myRefs to (publication items whose id is theID)
set myItem to first item of myRefs
set {theKey, thePath, theAuthor, theEditor, theTitle} to {citekey, path of attachment items, authors, editors, title} of myItem
if theAuthor = "" then set theAuthor to theEditor
set theRIS to format myItem using "RIS.fmt"
@zverhope
zverhope / be_orphaned_files.scpt
Last active December 16, 2022 08:24
Find orphaned files in Bookends, whether with hierarchical or flat folder structure, and then move to Orphans folder
set theFolder to (path to home folder as text) & "Library:Mobile Documents:iCloud~com~sonnysoftware~bot:Documents"
set BEList to {}
set orphanList to {}
set FinderList to {}
set pathList to {}
set od to AppleScript's text item delimiters
tell application "Finder"
-- create a list of the name of every file in the folder you've set
set FinderList to every file of folder theFolder as alias list