2

目标:成千上万的旧 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 菜鸟,可以肯定的是,这已经让我发疯了好几个星期!

4

1 回答 1

2
于 2012-04-11T12:25:28.417 回答