Search code examples
pdfapplescriptacrobatfilemaker

Embed Applescript in Global Field to (Export Container and Trigger Printing with Acrobat)


I am trying to tie together a filemaker script that will export PDFs to a temporary space and use and apple script to print them.

I was able to cull together info from this and some other boards to create an applescript that will print the PDFs using Acrobat from a folder.

I have already created a script that finds the Related attachments and exports them to the desktop.

What I'm having trouble with is merging the two.

I need to export the PDF to a folder or temporary place and trigger the apple script to initiate the printing...

This great Suggestion was provided by Chuck of chivalrysoftware.com/…...

Calculate the location to export by appending the filename to Get( TemporaryPath ). Export the container field contents to FileMaker to that path. Save the path to a global field in FileMaker Use an embedded AppleScript to access the global field path Use AppleScript to open the file in Preview and print it

This is my apple script:

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

My Filemaker script is:

Go to Related Record[
    Show only related records; From table: 'Attachments";
    Using layout: "Attachements Report' (Attachments); New window
]
Enter Find Mode 
Constrain Found Set [Restore]
Sort Records [Restore; No dialog]
# After finding the related attachments and constraining them to the specific type
# we rename and export them to the desktop
Go to Record/Request/Page [First] 
Loop
    Set Variable [$Path; Value:
        Get ( DesktopPath ) & Attachments::Record number & "-"
            & Attachment Type List 2::Prefix_z & Lien::Lien_ID_z1]
    Export Field Contents [Attachments::file_c; $Path]
    Go to Record/Request/Page [Next: Exit after last] 
End Loop 
Close Window [Current Window] 

Solution

  • First of all, the FileMaker part. Create a global text field in one of your tables. It looks like the Attachments table would be the best place for it. I'll call it g_applescript_parameter for this.

    Now we're going to use your $Path variable, which given the calc you've provided should be something like /Aslan/Users/chuck/Desktop/1234-ABC4321. I'd recommend appending a .pdf to the end of it since you'll be exporting PDF files. This may help later.

    Also, I would recommend that you use Get( TemporaryPath ) instead of Get( DesktopPath ). Anything you place in the temporary folder will be automatically deleted when you quit FileMaker, which means you don't have to write anything to clean up the desktop folder later and you don't have to manually trash them either. Let FileMaker do that work for you. :)

    Regardless, FileMaker uses a path of the form filemac:/volumeName/directoryName/fileName (see the notes in the Specify output file dialog box for the Export Field Contents script step). So you should also prepend filemac: to the beginning of your path variable.

    All told, your $Path should be set to something like this:

    "filemac:" & Get( DesktopPath ) & Attachments::Record number & "-" &
        Attachment Type List 2::Prefix_z & Lien::Lien_ID_z1 & ".pdf"
    

    So your export path for FileMaker should work better now. But AppleScript requires a different format for the path to the same file. Given the above, AppleScript's version should be something like /Users/chuck/Desktop/1234-ABC4321.pdf. In other words, everything after the drive name. Fortunately FileMaker can get the drive name with the Get( SystemDrive ) function. For me that function returns /Aslan/. So if we take the $Path variable as defined above and remove filemac: and the name of the drive as defined by Get( SystemDrive ) and add an extra slash at the beginning, that would convert our FileMaker path into an AppleScript path:

    "/" & Substitute( $Path; "filemac:" & Get( SystemDrive ); "" )
    

    Use Set Variable to create an $ASPath variable and set it to the above.

    Now within your loop store the contents of the $ASPath variable within that global text field:

    Loop
        Set Variable[ $Path; …]
        Set Variable[ $ASPath; …]
        Set Field[Attachments::g_applescript_parameter; $ASPath)
        Export Field Contents[Attachments::file_c; $Path]
        Go to Record/Request/Page[Next; Exit after last]
    End Loop
    

    Now AppleScript can extract that information. I'm assuming that given an accurate file being passed to the batchprint function, batchprint will work, so keep that, but remove everything before it and use something like this:

    set _pdf_path to contents of cell "g_applescript_parameter" of current layout
    batchprint(_pdf_path)
    
    on batchprint(mycurrentfile)
        ...
    end batchprint
    

    Add a Perform AppleScript step after the Export Field Contents step and place the above code in it.

    Note that the first line of the above AppleScript will only work as written from within FileMaker. If you're testing this outside of FileMaker in, for example, Script Editor, then you'll need to make that first line read

    tell applicaiton "FileMaker" to set _pdf_path ...
    

    You don't need to do this within the Perform AppleScript script step because by default commands are sent to the enclosing FileMaker application.