pdf - Applescript:用于 Appleworks 递归打印为 PDF 的 GUI 编程

标签 pdf printing io applescript

目标:数以千计的旧 Clarisworks 和 Appleworks 文档需要转换为 PDF。

问题:编写页面脚本不是一个选项;它不能可靠地保留格式。 一定是Appleworks。当这个脚本有效时,它就有效;但由于我无法识别的原因,它会跳过文件。

set appleworksFolder to choose folder
tell application "Finder"
    set folderItems to (files of entire contents of appleworksFolder)
    repeat with I from 1 to number of items in folderItems

        set the_doc to item I of folderItems
        set doc_name to name of the_doc as text

            (* Some files are missing metatags and try to save as "Untitled Document",
              this block ensures a file name is unique, later *)

            tell application "Finder"
            set the clipboard to doc_name & ".pdf"
        end tell


            (* Each file exists in a folder with a path.txt file that will later
               be used to put the file back where it was originally stored prior
               to this conversion process *)

        if name of the_doc is not "path.txt" then
            try
                tell application "Finder"

            (* Many files no longer have name extensions and appear as UNIX
               executables if not repaired *)

                    try
                        set nmex to name extension of the_doc as text
                    on error
                        set nmex to "ok"
                    end try

                    if nmex is not "cwk" or "CWK" then
                        set the_doc_str to the_doc as text
                        set doc_path to POSIX path of the_doc_str
                        do shell script "mv '" & doc_path & "' " & "'" & doc_path & ".cwk'"
                    end if
                    delay 1


            (* In case Appleworks hangs or otherwise bungs up, I force-quit
               it at the end of the script; this ensures its closed before 
               it tries to open the next file *)

                    if (application process "Appleworks 6" of application "System Events" exists) then
                        do shell script "killall 'LaunchCFMApp'"
                        delay 1
                    end if

                    tell application "AppleWorks 6"
                        open the_doc
                    end tell

            (* Some of the documents are huge, this delay gives the app time to load
                since this is all GUI-scripted *)

                    delay 5
                    tell application process "Appleworks 6" of application "System Events"

            (* This is where I think I am encountering problems; there are two 
               possible warnings that may or may not appear on opening the doc;
               that Appleworks needs to append a version number to the file (if
               its old) or that the file may be damaged and thus would need to be
               skipped. I get system beeps sometimes in this part, but I don't know
               why! *)

                        if (button "OK" of window 1 of application process "AppleWorks 6" of application "System Events" exists) then
                            delay 0.5
                            keystroke return
                            delay 0.5
                        end if
                        delay 2
                        if (button "OK" of window 1 of application process "AppleWorks 6" of application "System Events" exists) then
                            delay 0.5
                            keystroke return
                            delay 0.5
                        end if
                        delay 2
            (* If the document loads, the Appleworks welcome pane won't be there;
               this part of the script works flawlessly, when it happens. Sometimes
               documents are outside of print margins, hence the press-ok-button 
               conditional *)

                        if not (window "Starting Points" of application process "AppleWorks 6" of application "System Events" exists) then
                            tell application process "Appleworks 6" of application "System Events"
                                keystroke "p" using command down
                                delay 1
                                click menu button "PDF" of window "Print" of application process "AppleWorks 6" of application "System Events"
                                delay 1
                                click menu item "Save as PDF…" of menu "PDF" of menu button "PDF" of window "Print" of application process "AppleWorks 6" of application "System Events"
                                delay 1
                                keystroke "v" using command down
                                click button "Save" of window "Save" of application process "AppleWorks 6" of application "System Events"
                                delay 8
                                keystroke "w" using command down
                                delay 0.5
                                if (button 1 of window 1 of application process "AppleWorks 6" of application "System Events" exists) then
                                    delay 0.5
                                    keystroke "d" using command down
                                    delay 0.5
                                end if
                                delay 0.5
                            end tell
                        end if
                        do shell script "killall 'LaunchCFMApp'"
                        delay 3
                    end tell
                end tell
            end try
        end if
    end repeat
end tell

我想让这个宝贝在一个周末运行数千个文件并创建 PDF,但每次我通宵运行它时,我都会发现几百个正确处理的文档、数百或数千个跳过的文档,并且经常会出现一个错误。打印脚本本身的对话,这显然来自在 Appleworks 上下文之外使用 Command+P。我是一名 Applescript 菜鸟,可以肯定的是,这几周来一直让我发疯!

最佳答案

这并不是真正解决您的问题,因为它非常具体且量身定制,但我有一些可能有用的提示:

  • 您无需关闭“起点”窗口。当您告诉 AppleWorks 打开文档时,它将被忽略。

  • 尝试使用尽可能少的模拟击键。例如,与其模拟 cmd+P 打开“打印”对话框,不如模拟单击应用程序 Print… 菜单中的 File 命令:

    click menu item "Print…" of menu "File" of menu bar 1 of application process "AppleWorks 6"
    

    正确的方法是执行 cmd+W 来关闭文档,而不是执行

    tell application "AppleWorks 6" to close front document saving no
    
  • 如果您确实需要模拟击键,请确保它们被正确的应用程序接收,方法是在 keystroke 命令之前使用 tell app "AppleWorks 6" to activate 激活它,以防万一它当时不是最前面的应用程序。

  • 在检查其他对话框窗口时,使用 if (button 1 of window 1 of (*...*) exists) 是一个坏主意,因为在我的机器上,例如,在“打印”对话框打开时检查此窗口会导致脚本挂起几分钟(可能是永久的) ),并且因为总是建议通过名称而不是数字来寻址窗口(因为 window 1 只是最前面的窗口,并且很快就会成为另一个窗口)。因此,最好检查名为 such and such 的窗口是否存在。

  • 为了处理数千个您预计会发生错误的文件,您需要重组代码并重新考虑错误处理。例如,您可以将错误信息记录到文本文件中,以便查看哪些文件被跳过以及原因。要将程序逻辑与错误处理和详细信息分开,您可以使用处理程序,因此您的主循环可能如下所示:

    set logfile to alias "Macintosh HD:Users:user:Desktop:errors.log"
    open for access logfile with write permission
    
    repeat with the_doc in folderItems
        try
            open_with_appleworks(the_doc)
            print_to_pdf()
            close_document_and_quit_appleworks()
        on error error_message
            write error_message & "\n" to logfile
            close_document_and_quit_appleworks()
        end try
    end repeat
    
    close access logfile
    

    以类似的方式,您也可以记录成功消息。然后,在单个处理程序中,您应该指定有关如何准确执行每个步骤的详细信息。他们还可以提供一些要记录的错误信息:

    to open_with_appleworks(the_doc)
        tell app "AppleWorks 6"
            try
                (* do your stuff here *)
            on error err_msg
                (* re-signal the error to main loop! *)
                error "Failed to open " & (the_doc as text) & err_msg
            end try
        end tell
    end open_with_appleworks
    
  • 硬编码延迟会大大减慢您的脚本速度:虽然只有某些文档可能需要几秒钟才能打开,但您的脚本总是等待 5秒 – 即使文档立即打开。最好循环检查文档是否已打开:

    repeat while not (exists front document)
        -- just wait
        -- or, delay 0.1 -- if you really want to
    end repeat
    

    这同样适用于几乎所有其他延迟,其中大多数延迟都是可以避免的,因为在实践中它们会更短或更长。与其拖延半秒,希望那时警告或对话框已经出现,不如检查一下它是否存在。

  • (最后一个提示,然后我会保持安静)不幸的是,您似乎已经经历了将所有要处理的文件复制到一个文件夹中的麻烦,记住在一些“path.txt”中它们来自...如果文件位于某个文件夹的子文件夹中并且可以通过过滤器找到,则可以避免这种情况:您只需使用该特定搜索创建一个“smsart 文件夹”( example ),然后在 AppleScript 中输入 set theFiles to choose file with multiple selections allowed ,然后选择该特定智能文件夹中的所有文件,就可以了。然后,该脚本可以通过询问 file fapplication "Finder"(这将返回 Finder get parent of f 引用,可以轻松地将其转换为 filealias 地址)轻松找出 POSIX file 的位置。然后,由于您知道通过“打印”文档生成的 PDF 文件的名称和位置,因此您可以将该 PDF 移动到其源文档所在的文件夹。

关于pdf - Applescript:用于 Appleworks 递归打印为 PDF 的 GUI 编程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10092896/

相关文章:

python - 如何使用多个参数正确格式化 Python Print?

haskell - GHCi 与 runhaskell 的 getLine 功能差异

c++ - 我在哪里放置程序读取的文件名?

android - 在 cordova 应用程序 (android) 中打开 pdf

macOS 中的 Swift 打印

html - 如何在 HTML5 范围输入类型旁边显示值?

Java 文件 I/O 性能随时间下降

java - 搜索用于将 GUI 组件写入 PDF 的 Java 库或第 3 方库

pdf - Tess4J - 在资源路径中找不到 native 库 (linux-x86-64/libtesseract.so)

pdf - 使用 pdf.js 在网站上显示 PDF