Skip to content

Instantly share code, notes, and snippets.

@alessaba
Last active November 16, 2017 17:17
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 alessaba/3b9c66ee3feb454c2c35392757e98759 to your computer and use it in GitHub Desktop.
Save alessaba/3b9c66ee3feb454c2c35392757e98759 to your computer and use it in GitHub Desktop.
Pythonista Unlock PDF
###############################
# Script made by @ivanmorenoz with a small secure correction from @filippoclaudi #
###############################
# Description: this script uses Pythonista Objc Bridging for unlocking the PDF using PDFKit
from objc_util import ObjCClass, nsurl
from appex import get_file_paths, finish, is_running_extension
from os import remove, path
from console import alert, input_alert, quicklook
def showAlert(msg,file):
alert(msg, file, "OK", hide_cancel_button=True)
def main():
if not is_running_extension():
print('This script is intended to be run from the sharing extension.')
return
files = get_file_paths()
if not files:
alert('No files were specified')
return
for file in files:
filename = path.basename(file)
if not filename.endswith('.pdf'):
showAlert('Only PDF are allowed', filename)
continue
pdf = ObjCClass('PDFDocument').alloc().initWithURL(nsurl(file))
if pdf.isEncrypted():
pwd = password_alert('Password', filename)
if pdf.unlockWithPassword_(pwd):
pdf.writeToFile_(file)
else:
showAlert("Wrong Password", filename)
else:
showAlert("This PDF is not encrypted", filename)
finish()
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment