Skip to content

Instantly share code, notes, and snippets.

@ELLIOTTCABLE
Created May 16, 2014 20:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ELLIOTTCABLE/48411c93fd5a70830641 to your computer and use it in GitHub Desktop.
Save ELLIOTTCABLE/48411c93fd5a70830641 to your computer and use it in GitHub Desktop.
Modified PDF workflow to save (dated) PDF to Dropbox
#!/usr/bin/python
#
# webreceipts job-id user title copies options pdf-file
#
import os
import shutil
import sys
from AppKit import *
from datetime import date
from Foundation import *
import locale
def safeFilename(filename):
filename = filename.lstrip('.')
filename = filename.replace(os.path.sep, '-')
if len(filename) == 0:
filename = "Untitled"
elif len(filename) > 245:
filename = filename[0:245]
return filename
def main(argv):
(title, options, pdfFile) = argv[0:3]
destDirectory = os.path.expanduser("~/Dropbox/Documents/Web Receipts/")
# Create the Web Receipts folder if necessary.
try:
os.makedirs(destDirectory)
#Put a ".localized" file in the destDirectory
locFile = ".localized"
destPath = os.path.join(destDirectory, locFile)
file = open(destPath,"w")
file.write('')
file.close()
except:
pass
# Build a file path pointing into the web reciepts folder
# using the document's title and a PDF extension.
title = safeFilename(title)
title = "%s - %s" % (date.today(), title)
destFile = title + ".pdf"
destPath = os.path.join(destDirectory, destFile)
# If the filename we want is already in use then start
# appending numbers until we get a unique name.
# Added localizing the number.
i = 2
while (os.path.exists(destPath)):
locString = NSString.localizedStringWithFormat_("%d", i)
utfVersion = locString.encode("utf-8")
destFile = title + "." + utfVersion +".pdf"
destPath = os.path.join(destDirectory, destFile)
i = i + 1
# Move the file if possible otherwise copy it.
shutil.move(pdfFile, destPath)
if __name__ == "__main__":
main(sys.argv[1:])
@ELLIOTTCABLE
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment