Using Automator or Applescript or both to recursively print documents to PDF

♀尐吖头ヾ 提交于 2019-12-24 17:12:33

问题


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.

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).

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/

will contain

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

But must eventually also contain /Appleworks/Boss_File_1/Boss_File_1.pdf

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

end tell`

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.

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)

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.


回答1:


Try this:

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:


Convert using Pages

If you have Pages (part of iWork), it can open .cwk files and save them as PDF: just replace your if block with this:

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

(you will need this custom function 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 scripting

Alternatively, you could do GUI scripting upon AppleWorks as you attempted, but it has the disadvantage that you cannot programmatically specify where to save the PDF file.

This snippet works for me:

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

Unfortunately, AppleWorks doesn't seem to properly listen to AppleScript's close command, therefore you may need to close the file by also simulating the cmd+W keystrokes.




回答3:


I needed to do this today on Mountain Lion with a bunch of RTF receipts; here's how I did it:

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

Worked great; super easy. No Automator or AppleScript silliness.



来源:https://stackoverflow.com/questions/9822487/using-automator-or-applescript-or-both-to-recursively-print-documents-to-pdf

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!