Applescript:GUI编程,用于为Appleworks递归打印为PDF

Applescript:GUI编程,用于为Appleworks递归打印为PDF,pdf,printing,io,applescript,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) repea

目标:成千上万的旧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。我是一个苹果书呆子,可以肯定,这几个星期来一直让我发疯

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

  • 您不需要关闭“起点”窗口。当您告诉AppleWorks打开文档时,它将被取消

  • 尽量少用模拟按键。例如,与其模拟
    cmd+P
    打开打印对话框,不如模拟在其
    文件
    菜单中单击应用程序的
    Print…
    命令:

    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
    
  • 如果您确实需要模拟击键,请确保它们被正确的应用程序接收,方法是在
    击键
    命令之前,使用
    告诉应用程序“AppleWorks 6”激活
    ,以防此时它不是最前面的应用程序

  • 在检查其他对话框窗口时,如果((*…*)窗口1的按钮1存在)使用
    是一个坏主意
    ,因为在我的机器上,例如,在“打印”对话框打开时检查此选项会导致脚本挂起数分钟(可能是永久挂起),而且,因为通常建议按名称而不是数字来称呼窗口(因为
    window 1
    只是最前面的窗口,很快就会变成另一个窗口)。因此,最好检查是否存在名为某某的窗口

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

    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文件夹”,然后在AppleScript
    文件中设置以选择允许多个选择的文件
    ,然后选择该特定智能文件夹中的所有文件,你很好。然后,脚本可以通过询问
    应用程序“Finder”
    获取f的父级
    (这将返回一个Finder
    文件
    引用,该引用可以轻松地转换为
    别名
    POSIX文件
    地址)来找到
    文件f
    的位置。然后,由于您知道通过“打印”文档生成的PDF文件的名称和位置,因此可以将该PDF移动到其源文档所在的文件夹中


这正是我一直在寻找的建议——作为一个核心,我需要一些时间来实施这些建议。但总的来说,这就是我想要的,一种使这个脚本足够健壮的方法,使我不用手动打开4000多个文件并保存为PDF。非常感谢你花时间为我写下这一切!!!不过我有一个问题。。。Appleworks错误窗口似乎没有标题,它们没有使用系统对话框,这就是我使用“窗口1”的原因。知道我如何检索这些信息吗?在显示您想要定位的错误窗口的状态下,运行一个小测试脚本,告诉应用程序“系统事件”,以获取应用程序进程“AppleWorks 6”窗口的属性。。这将为您提供windows的词典列表;例如,对话框窗口有一个属性
说明:“dialog”
,您可以稍后检查该属性(例如
存在窗口1,窗口1的说明是“dialog”