使用 Automator 或 Applescript 或两者将文档递归打印为 PDF [英] Using Automator or Applescript or both to recursively print documents to PDF

查看:17
本文介绍了使用 Automator 或 Applescript 或两者将文档递归打印为 PDF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有大量旧 Apple 格式 (Appleworks) 的文件(4000 多个).我的雇员需要将它们全部更新为 PDF.通过在 Appleworks 中打开文档并使用系统打印对话框,我可以将它们保存为 PDF——这是理想的.然而,我是 Applescript/Automator 的完整核心.

I have a massive set of files (4000+) that are in an old Apple format (Appleworks). My employed needs them all updated to PDF. By opening the documents in Appleworks and using the system print dialogue, I can save them to PDF—this is ideal. I'm a complete nub with Applescript/Automator, however.

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

Using a Python script I was able to gather all the Appleworks files from my bosses computer and put them in a directory; each file is then in a subdirectory with a .txt file containing its original location (where, eventually, I will have to put them back).

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

I need the script to move recursively through this massive directory, getting every file that's neither a folder nor a .txt document, and save it to PDF in the same directory in which the original file was found. 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 看起来像:

I can get half way with either solution, but don't know how to make them work together. The Applescript I'm using looks like:

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 位,我可以很容易地做到这一点.

This opens the print dialogue but never gets any further and I have no idea why. I realize this script also doesn't deal with putting the document back in its original folder, but in Applescript I could easily enough do this if I could get past the actual printing-to-PDF bit.

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

Meanwhile, in Automator, using this workflow:

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 保留文件源自的路径并确保它打印到该路径.

I then am stuck; using the actual Print Finder Items and choosing Adobe PDF seems to actually do nothing at all, and recording myself using the print to pdf process live is useless because I don't know how to get Automator to retain the path the file originated from and ensure it prints to it.

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

If anyone can help me put this together somehow, I'd be enormously grateful. Thanks.

推荐答案

试试这个:

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

这篇关于使用 Automator 或 Applescript 或两者将文档递归打印为 PDF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆