Skip to content

Instantly share code, notes, and snippets.

@thespacedoctor
Last active May 5, 2021 15:59
Show Gist options
  • Save thespacedoctor/e91ab0077f5c201a01983e294834a3bc to your computer and use it in GitHub Desktop.
Save thespacedoctor/e91ab0077f5c201a01983e294834a3bc to your computer and use it in GitHub Desktop.
[Import PDFs into Bookends] #bookends #import #reference
#!/usr/bin/env python
# encoding: utf-8
"""
*import a file into bookends database with the filename as the reference title*
:Author:
David Young
:Date Created:
January 30, 2020
Usage:
import-into-bookends-with-title <filePath>
Options:
filePath path to the file to import
-h, --help show this help message
-v, --version show version
-s, --settings the settings file
"""
################# GLOBAL IMPORTS ####################
import sys
import os
from fundamentals import tools
from datetime import datetime, date, time
from subprocess import Popen, PIPE, STDOUT
import time
def main(arguments=None):
"""
*The main function used when ``import-into-bookends-with-title.py`` is run as a single script from the cl*
"""
# SETUP THE COMMAND-LINE UTIL SETTINGS
su = tools(
arguments=arguments,
docString=__doc__,
logLevel="DEBUG",
options_first=False,
projectName=False
)
arguments, settings, log, dbConn = su.setup()
# UNPACK REMAINING CL ARGUMENTS USING `EXEC` TO SETUP THE VARIABLE NAMES
# AUTOMATICALLY
a = {}
for arg, val in list(arguments.items()):
if arg[0] == "-":
varname = arg.replace("-", "") + "Flag"
else:
varname = arg.replace("<", "").replace(">", "")
a[varname] = val
if arg == "--dbConn":
dbConn = val
a["dbConn"] = val
log.debug('%s = %s' % (varname, val,))
filepath = os.path.abspath(a["filePath"])
if os.path.isfile(filepath):
# GET DATE
now = datetime.now()
now = now.strftime("%Y")
# CREATE TITLE
filenameNoExtension = os.path.splitext(os.path.basename(filepath))[0]
filenameNoExtension = filenameNoExtension.replace(
"_", " ").replace("-", " ").strip()
# RUN THE IMPORT
applescript = u"""
tell application "Bookends"
open "/Users/Dave/Library/Application Support/Bookends/dry-papers.bdb"
return «event ToySADDA» "%(filepath)s" given «class RIST»:"TY - NULL" & return & "T1 - %(filenameNoExtension)s" & return
end tell
""" % locals()
cmd = "\n".join(["osascript << EOT", applescript, "EOT"])
p = Popen(cmd, stdout=PIPE, stdin=PIPE, shell=True)
output = p.communicate()[0]
log.debug('output: %(output)s' % locals())
output = output.split("\n")
refID = None
try:
refID = int(output[0])
except:
pass
time.sleep(1)
# RUN THE IMPORT
applescript = u"""
tell application "Bookends"
open "/Users/Dave/Library/Application Support/Bookends/dry-papers.bdb"
tell front library window
set type of (every publication item whose id is %(refID)s) to 18
end tell
end tell
""" % locals()
cmd = "\n".join(["osascript << EOT", applescript, "EOT"])
p = Popen(cmd, stdout=PIPE, stdin=PIPE, shell=True)
return
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment