【问题标题】:Remove multiple PDF passwords with workflow or applescript in a folder使用工作流或文件夹中的 applescript 删除多个 PDF 密码
【发布时间】:2021-05-28 21:10:48
【问题描述】:

如何使用Applescript 或在OS X 中创建Workflow 从多个PDF 文件中删除密码?

我的情况是我在一个文件夹中有多个受密码保护的 PDF 文件。我知道所有人的密码,这是相同的。我希望能够在此文件夹上运行工作流,以便工作流解锁其中的所有 PDF。

或者一次在所有这些文件上运行 Applescript shell 代码

我还希望能够创建一种在文件夹中放置/移动/粘贴任何 PDF 的方式自动解锁它:)

帮助赞赏!!


更新

我试过pdftk。安装 pdftk 后,以下代码在 Terminal 中运行良好

pdftk secured.pdf input_pw foopass output unsecured.pdf

现在我希望能够创建一个工作流,在选定文件或文件夹中的所有文件上运行此命令

【问题讨论】:

  • 对于今天遇到此问题的其他人,请尝试qpdf(可通过自制软件获得:brew install qpdf
  • 对于 qpdf,使用的命令是 qpdf --decrypt --password=PASSWORD --replace-input MY.PDF
  • qpdf 有效,即使对于安全设置为禁用打印的 pdf 也是如此。

标签: macos applescript


【解决方案1】:

执行 shell 脚本的 AppleScript 命令是 do shell script... 所以是这样的:

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

应该可以。 此时我看到了 2 个选项:

  1. 编写一个 AppleScript 脚本,询问用户文件夹或从 Finder 选择中获取它,然后为文件夹中的每个文件执行命令;
  2. 编写一个 Automator 工作流程,使用已有的操作从文件夹中获取文件,然后附加一个执行 AppleScript 脚本的新操作。

对于选项 2,您可以设置 Automator 工作流程,如下图所示。

【讨论】:

  • 我知道do 的事情。我想创建一个允许我在多个文件上运行此脚本的工作流。第 2 点是我需要帮助的地方
  • 感谢您的回答,为屏幕和代码 +1。但我接受另一个,因为它提到了文件夹操作
【解决方案2】:

您听说过“文件夹操作”吗?这是一种将 applescript 附加到文件夹的方法,以便每当将新文件添加到文件夹时,applescript 就会运行。一个快速的谷歌搜索出现了this,它将为您提供有关如何设置它的指导。如果您仍有疑问,可以进行更多谷歌搜索。

这是一个可用于文件夹操作的 applescript。我没有测试它,但它应该可以工作(它是基本代码)。这只会在 pdf 文件上完成它的工作。您添加到文件夹中的其他文件将被单独保留。 注意:您必须输入脚本前 4 个变量的值。

祝你好运。

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

编辑:您可以通过以下方式要求输入密码。请注意,如果您想查看文本,请删除“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

【讨论】:

  • 如果我想以on run {input, parameters} 运行它,以便能够右键单击并选择要解锁的PDF,我会丢失文件夹名称(fContainer)。如何从 applescript 中的文件中获取该文件夹名称?
  • 更新:获得文件夹名称。现在我需要能够提示用户输入密码,如果为空,则使用默认密码。
  • 我在帖子中添加了一个编辑部分,向您展示如何要求输入密码。
  • 搞定了。谢谢你。我假设我需要把我的默认密码放在"",这样即使没有输入密码,它也使用默认的
  • 您可以这样做,或者只使用变量 pWord,两者都可以。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-04-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-07-25
相关资源
最近更新 更多