1

我有大量的文件(4000+)是旧的 Apple 格式(Appleworks)。我的雇员需要将它们全部更新为 PDF。通过在 Appleworks 中打开文档并使用系统打印对话框,我可以将它们保存为 PDF——这是理想的。但是,我对 Applescript/Automator 很熟悉。

使用 Python 脚本,我能够从我老板的计算机上收集所有 Appleworks 文件并将它们放在一个目录中;然后,每个文件都位于一个子目录中,其中包含一个包含其原始位置的 .txt 文件(最终,我将不得不将它们放回原处)。

我需要脚本在这个庞大的目录中递归移动,获取既不是文件夹也不是 .txt 文档的每个文件,并将其保存到与原始文件相同的目录中的 PDF 中。IE。

/Appleworks/Boss_File_1/

将包含

/Appleworks/Boss_File_1/Boss_file_1.cwk/Appleworks/Boss_File_1/path.txt

但最终还必须包含/Appleworks/Boss_File_1/Boss_File_1.pdf

我可以用任何一种解决方案获得一半,但不知道如何让它们一起工作。我正在使用的 Applescript 看起来像:

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
    if name of the_doc is not "path.txt" then
        try
            tell application "AppleWorks 6"
                open the_doc
                tell application "System Events"
                    tell process "Appleworks"
                        keystroke "p" using command down
                        click menu button "PDF" of window "Print"
                        click menu item "Save as PDF…" of menu 1 of menu button "PDF" of window "Print"
                        click button "Save" of window "Save"

                    end tell
                end tell
            end tell
        end try
    else
        tell application "Finder"
            delete the_doc
        end tell
    end if
end repeat

结束告诉`

这会打开打印对话,但永远不会进一步,我不知道为什么。我意识到这个脚本也不处理将文档放回其原始文件夹中,但在 Applescript 中,如果我能够通过实际的打印到 PDF 位,我可以轻松地做到这一点。

同时,在 Automator 中,使用此工作流程:

Get Specified Finder Items
Get Folder Contents
Filter Finder Items (by kind and then by file extension is not .txt)
Open Finder Items (with Appleworks)

然后我被卡住了;使用实际Print Finder Items和选择Adobe PDF似乎实际上根本没有做任何事情,并且使用实时打印到 pdf 过程记录自己是无用的,因为我不知道如何让 Automator 保留文件来源的路径并确保它打印到它。

如果有人能以某种方式帮助我把它放在一起,我将不胜感激。谢谢。

4

3 回答 3

2

使用页面转换

如果您有Pages(iWork 的一部分),它可以打开 .cwk 文件并将它们保存为 PDF:只需将您的if块替换为:

if (the_doc's name extension is not "txt") then
    set newName to my makeNewFileName(the_doc, "pdf")
    try
        tell application "Pages"
            open (the_doc as alias)
            set thisDoc to front document
            save thisDoc as "SLDocumentTypePDF" in newName
            close thisDoc saving no
        end tell
    on error
        display dialog "Error: cannot export " & (name of the_doc) & " to PDF."
    end try
end if

(您将需要此自定义功能makeNewFileName):

(* prepare new file name with extension ext *)
on makeNewFileName(finderItem, ext)
    tell application "Finder"
        set fname to finderItem's name
        set thePath to (finderItem's container) as alias as text
        return (thePath & (text 1 thru ((length of fname) - (length of (finderItem's name extension as text))) of fname) & ext)
    end tell
end makeNewFileName

完整的工作脚本

图形用户界面脚本

或者,您可以尝试在 AppleWorks 上编写 GUI 脚本,但它的缺点是您无法以编程方式指定保存 PDF 文件的位置。

这个片段对我有用:

tell application "AppleWorks 6"
    open the_doc
    activate

    tell application "System Events" to tell process "AppleWorks"
        keystroke "p" using command down
        delay 1 -- or longer, if it takes longer
        click menu button "PDF" of window "Print"
        click menu item "Save as PDF…" of menu 1 of menu button "PDF" of window "Print"
        delay 1 -- or longer
        click button "Save" of window "Save"
    end tell
end tell

不幸的是,AppleWorks 似乎没有正确听取 AppleScript 的close命令,因此您可能需要通过模拟 cmd+W 击键来关闭文件。

于 2012-03-22T16:07:51.100 回答
1

试试这个:

set appleworksFolder to choose folder
set thePath to POSIX path of appleworksFolder as string

tell application "Finder"
set folderItems to files of appleworksFolder
repeat with aFile in folderItems
    set {name:fileName, name extension:nameExtension} to aFile
    set filePath to POSIX path of (aFile as alias) as string

    if nameExtension is not "txt" then
        set theLocation to POSIX path of (aFile as text)
        set baseName to text 1 thru ((get offset of "." & nameExtension in fileName) - 1) of fileName
        set destLocation to (thePath & baseName & ".pdf")
        set theCommand to "/System/Library/Printers/Libraries/./convert -f \"" & filePath & "\"" & " -o " & "\"" & destLocation & "\"" & " -j \"application/pdf\""
        do shell script theCommand

    else
        tell application "Finder" to delete aFile
    end if
end repeat
end tell
于 2012-03-22T13:07:31.917 回答
0

我今天需要用一堆 RTF 收据在 Mountain Lion 上执行此操作;我是这样做的:

#!/bin/bash
for file in *.rtf ; do
filename=$(basename "$file")
/usr/sbin/cupsfilter "$file" > "$filename.pdf"
done

工作得很好;超级容易。没有 Automator 或 AppleScript 愚蠢。

于 2012-12-12T22:41:40.627 回答