Search code examples
pdfapplescriptcontainersbatch-processingfilemaker

Applescript to (if/then) determine file type and choose correct program to open and print file (within batch sequence)


I have culled together an applescript with great help from @chuck and other board posts to effectively batch print a list of files exported from filemaker containers to a folder called "print" on my desktop.

The problem I'm running into now is some of those container exports are not PDF (its a mix of Jpg, PNG, Tif and PDF) and will not open using acrobat (using preview for the PDF or any other PDF viewer is out of the question for a myriad of reasons)... This problem is effectively shutting down the work flow because of error messages from acrobat that must be manually clicked off before the script will proceed to the next file.

My question is can applescript be commanded to determine the file type first and choose a different program to open the document with and trigger the print command and close window before moving onto the next document in the sequence.

(i.e. if .pdf then use acrobat print close window, if not use preview to open file, print close window, repeat until all files have been printed.)

Below is my current working code.(FYI) This script is running within a filemaker script that is creating the "Print" folder on the desktop and exporting the container fields to the folder.

`set myFolder to (path to desktop folder as text) & "Print:"

set myfiles to list folder myFolder without invisibles

repeat with myfile in myfiles

set mycurrentfile to ((myFolder as string) & (myfile as string)) as string
batchprint(mycurrentfile)

end repeat

on batchprint(mycurrentfile)

tell application "Adobe Acrobat Pro"
    activate -- bring up acrobat
    open alias mycurrentfile -- acrobat opens that new file    
    tell application "System Events"
        tell process "Acrobat"
            click menu item "Print..." of menu 1 of menu bar item "File" of menu bar 1
            click button "Print" of window "Print"
            tell application "System Events"
                tell process "Acrobat"
                    click menu item "Close" of menu 1 of menu bar item "File" of menu bar 1
                end tell
            end tell
        end tell
    end tell
end tell

tell application "Finder" -- to move the printed file out 
    set x to ((path to desktop folder as text) & "Printed PDFs:")
    if alias x exists then
        beep
    else
        make new folder at the desktop with properties {name:"Printed PDFs"}
    end if
    move alias mycurrentfile to folder "Printed PDFs"
end tell

end batchprint`


Solution

  • Use Finder to test for the file type and the extension, then use your Acrobat tell block if it's a pdf, and use Preview or whatever if it is not. Here's the code structure for that:

    tell application "Finder" to set {fType, nExt} to ({file type, name extension} of file mycurrentfile)
    if (fType is "PDF ") or (nExt is "pdf") then
        -- Open mycurrentfile with Acrobat
        tell application "Adobe Acrobat Pro"
        ...     
    else
        -- Open mycurrentfile with something else
        tell application "Preview"
        ...
    end if