使用Automator或Applescript或两者以递归方式将文档打印为PDF

时间:2012-03-22 12:40:26

标签: pdf-generation applescript automator

我有大量的文件(4000+),采用旧的Apple格式(Appleworks)。我的雇员需要将他们全部更新为PDF。通过在Appleworks中打开文档并使用系统打印对话框,我可以将它们保存为PDF - 这是理想的选择。不过,我是Applescript / Automator的完整小结。

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

我需要脚本以递归方式移动到这个庞大的目录中,使每个文件既不是文件夹也不是.txt文档,并将其保存到原始文件所在的同一目录中的PDF 结果的。即

/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似乎实际上什么都不做,并且使用print to pdf process live录制自己是没用的,因为我不知道如何让Automator保留文件来自的路径并确保它打印到它。

如果有人能帮我把这个放在一起,我会非常感激。感谢。

3 个答案:

答案 0 :(得分:2)

使用Pages转换

如果您有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

complete working script

GUI脚本

或者,您可以在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键击来关闭该文件。

答案 1 :(得分: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

答案 2 :(得分:0)

我今天需要在山狮上用一堆RTF收据做这件事;这是我如何做到的:

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

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