首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何使用本机macOS工具以编程方式旋转PDF中的所有页面?

如何使用本机macOS工具以编程方式旋转PDF中的所有页面?
EN

Stack Overflow用户
提问于 2017-10-19 23:10:08
回答 6查看 1.5K关注 0票数 1

给定一个文件路径,我需要使用macOS中没有包含的任何内容来旋转PDF文件路径中的所有页面。它的包装器将是AppleScript,所以我还可以访问命令行,因此所有脚本语言都默认安装在macOS中,但不需要brew,或者,例如,pip

EN

回答 6

Stack Overflow用户

回答已采纳

发布于 2017-10-20 20:25:43

这是我用过的解决方案。

  • 自动将PDF分离为与原始PDF相同级别的新文件夹中的各个页面。
  • 一些AppleScript使用图像事件旋转单个页面PDF
  • 第二个自动工作流程,将独立的PDF重新缝合在一起
  • 一个控制一切的AppleScript,它根据需要使用do shell scriptopen每个Automator工作流。
票数 0
EN

Stack Overflow用户

发布于 2017-10-20 15:47:13

PDF页面旋转可以使用标准预览应用程序执行。

打开PDF文档后,按1或2次选项卡键(取决于预览版本),以便使用“cmd a”选择边栏上的所有页面。然后点击'cmd R‘(或L)旋转90%左右(或左)所有选定的页面,最后按“cmd w”关闭窗口。预览将要求在默认情况下保存更改,然后只按enter键保存。

您可以使用来自系统事件的击键指令在Applescript中模拟所有这些键。

要在许多PDF文件中使用这个子程序,您有两个解决方案,仍然在Applescript中:

1)选择一个文件夹并循环遍历该文件夹的所有PDF文件以应用此子例程。

2)在" Finder“中选择您的文件,您的脚本将直接接受所有Finder选定的文件来应用相同的子例程。

这样的GUI脚本并不完美,但Preview不是可编写脚本的...unfortunately )-:

票数 5
EN

Stack Overflow用户

发布于 2017-10-20 18:47:37

据我所知,macOS没有任何一个本机命令行Unix可执行文件,可以旋转PDF中的所有页面(同时保持基于文本的页面基于文本)。sip可以旋转单个页面PDF,但是产生的PDF是封装的图像,而不是文本(如果它是文本库的话)。另外,也不确定是否有一种使用普通的AppleScriptObjC (Cocoa-AppleScript) AppleScript的方法,而不是通过UI脚本(默认的预览应用程序),而不使用和或Python等等。

使用第三方命令行实用程序可能是最简单的,但您说过只能使用macOS的默认部分来完成。因此,我将提供一种AppleScript解决方案,该解决方案使用UI脚本(默认的预览应用程序),可以在没有其他方法的情况下使用或第三方实用程序,等等。

这个解决方案(和编码)假设预览是PDF文档的默认应用程序,并使用它旋转PDF文档中的所有页面。它还被设置为Automator工作流。(尽管还有其他方法来合并下面所示的AppleScript代码。)

首先,在Finder中,复制目标PDF文档,并使用这些文档。

在Automator中,创建一个新的工作流文档,添加以下操作

  • 获取指定的查找项
    • 将复制的目标PDF文档添加到此操作。

  • 运行AppleScript脚本
    • 将默认代码替换为以下代码:

code AppleScript

代码语言:javascript
运行
复制
on run {input}
    set thisLong to 0.25 -- # The value of 'thisLong' is decimal seconds delay between keystrokes, adjust as necessary.
    set theRotation to "r" -- # Valid values are 'l' or 'r' for Rotate Left or Rotate Right.
    set theViewMenuCheckedList to {}
    set theMenuItemChecked to missing value
    repeat with thisItem in input
        tell application "Finder" to open file thisItem -- # By default, in this use case, the PDF file will open in Preview.
        delay 1 --  # Adjust as necessary. This is the only 'delay' not defined by the value of 'thisLong'.
        tell application "System Events"
            perform action "AXRaise" of window 1 of application process "Preview" -- # Just to make sure 'window 1' is front-most.
            delay thisLong
            --  # Ascertain which of the first six 'View' menu items is checked.
            set theViewMenuCheckedList to (value of attribute "AXMenuItemMarkChar" of menu items 1 thru 6 of menu 1 of menu bar item 5 of menu bar 1 of application process "Preview")
            repeat with i from 1 to 6
                if item i in theViewMenuCheckedList is not missing value then
                    set theMenuItemChecked to i as integer
                    exit repeat
                end if
            end repeat
            --  # Process keystrokes based on which 'View' menu item is checked.
            --  # This is being done so the subsequent keystroke ⌘A 'Select All' 
            --  # occurs on the 'Thumbnails', not the body of the document.
            if theMenuItemChecked is not 2 then
                repeat with thisKey in {"2", "1", "2"}
                    keystroke thisKey using {option down, command down}
                    delay thisLong
                end repeat
            else
                repeat with thisKey in {"1", "2"}
                    keystroke thisKey using {option down, command down}
                    delay thisLong
                end repeat
            end if
            repeat with thisKey in {"a", theRotation as text, "s"} -- # {Select All, Rotate Direction, Save}
                keystroke thisKey using {command down}
                delay thisLong
            end repeat
            keystroke theMenuItemChecked as text using {option down, command down} -- # Resets the 'View' menu to the original view.
            delay thisLong
            keystroke "w" using {command down} -- # Close Window.
        end tell
    end repeat
end run

备注:

  • 由于该脚本使用Accessibility UI脚本,当从Automator (或脚本编辑器)运行时,必须将运行中的应用程序添加到系统首选项>SecurityE 287& be;code>E 188PrivacyE 289/code>,以便正确运行。保存为应用程序时,需要添加保存的应用程序。
  • 同样,对于UI脚本,可能需要更改delay命令的值,以便在您的系统上使用(或者根据需要添加其他delay命令,尽管在这种情况下不需要额外的delay命令)。但是,应该先在一组文档上测试这一点,以确保thisLong的值集在您的系统上工作。在我的系统中,这是按编码工作的。
  • 当以这种方式使用UI脚本时,一旦任务启动,就必须远离系统,让它完成对文件的处理。尝试多任务只会使焦点远离手头的任务并导致其失败。
  • 如果您需要旋转超过一次,添加额外的theRotation as text,到: 在{"a“中使用thisKey重复,theRotation作为文本,"s"} -#{选择所有方向,旋转方向,保存} 示例: 在{"a“中用thisKey重复,theRotation作为文本,theRotation作为文本,"s"}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46840180

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档