Remove multiple PDF passwords with workflow or applescript in a folder

允我心安 提交于 2019-12-11 06:43:01

问题


How do I remove passwords from multiple PDF files using Applescript or by creating a Workflow in OS X?

My scenario is that I have multiple password protected PDF files in a folder. I know the passwords for all, which is same. I want to be able to run a Workflow on this folder so that all PDFs inside it are unlocked by the workflow.

OR run an Applescript shell code on all these files at once

I also preferably want to be able to create a way where putting / moving / pasting any PDF in the folder automatically unlocks it :)

Help appreciated !!


Update:

I have tried pdftk. The following code works awesome in Terminal, once pdftk is installed

pdftk secured.pdf input_pw foopass output unsecured.pdf

Now I want to be able to create a workflow that runs this command on selected files or on all the files in a folder


回答1:


Have you heard of "Folder Actions"? It's a way to attach an applescript to a folder so that whenever a new file is added to the folder the applescript is run. A quick google search turned up this which will give you directions on how to set it up. You can do more google searching if you still have questions.

Here's an applescript you can use with folder actions. I didn't test it but it should work (it's basic code). This will do its stuff on only pdf files. Other files you add to the folder will be left alone. NOTE: you have to put in your values for the first 4 variables of the script.

Good luck.

on adding folder items to theFolder after receiving theItems

    -- enter your values here
    set pdftkPosixPath to "/usr/bin/pdftk"
    set pWord to "foopass"
    set appendedName to "_unlocked" -- text to append to the file name
    set shouldTrash to true -- true or false, move the locked file to the trash after unlocking?

    set fContainer to theFolder as text
    repeat with anItem in theItems
        try
            tell application "System Events"
                set fName to name of anItem
                set fExt to name extension of anItem
            end tell

            if fExt is "pdf" and fName does not contain appendedName then
                set baseName to (text 1 thru -5 of fName) & appendedName & ".pdf"
                set newPath to fContainer & baseName
                do shell script (quoted form of pdftkPosixPath & space & quoted form of POSIX path of anItem & " input_pw " & quoted form of pWord & " output " & quoted form of POSIX path of newPath)

                if shouldTrash then
                    tell application "Finder" to move anItem to trash
                end if
            end if
        end try
    end repeat
end adding folder items to

EDIT: here's how you can ask for a password. Note that if you want to see the text then remove "with hidden answer".

display dialog "Enter a password:" default answer "" with icon note with hidden answer
set theAnswer to text returned of the result
if theAnswer is not "" then set pWord to theAnswer



回答2:


The AppleScript command to execute a shell script is do shell script... So something like this:

do shell script "pdftk secured.pdf input_pw foopass output unsecured.pdf"

should work. At this point I see 2 options:

  1. write an AppleScript script that ask the user for the folder or get it from the Finder selection and then execute the command for each file in the folder;
  2. write an Automator workflow that get the files from the folder using already available actions and then attach a new action that execute the AppleScript script.

For option 2 you can set an Automator workflow as in the following image.



来源:https://stackoverflow.com/questions/14352933/remove-multiple-pdf-passwords-with-workflow-or-applescript-in-a-folder

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!