mitchBVI
mitchBVI

Reputation: 11

Use Applescript to pass Path and File Name to Save as Print Dialog box for PDF's

I have a script that opens an email and then I want to save it as a PDF. The script works but I cannot figure out how to pass the path and folder name to the dialog box that results after I call Save as a PDF. This is my script so far.

global FlName

tell application "Mail"
    set theMsg to selection
    open selection
    set Dialogresult to the button returned of (display dialog "Process this email and Print as PDF" buttons {"Yes", "No"} default button "Yes")
    if Dialogresult is "Yes" then
        repeat with selectMsg in theMsg
            tell selectMsg
                --set background color to red --Show message processed
                set FlName to subject
                set check to count every word of FlName
                --display dialog check
                set FlName to (my FixFileName(FlName)) --strip bad characters
            end tell
        end repeat
        set process_name to "Mail"
        activate application process_name
        tell application "System Events"
            tell process process_name
                --display dialog "proposed File Name" & return & FlName
                keystroke "p" using command down
                delay 2
                set PDFref to sheet 1 of window 1
                click menu button "PDF" of PDFref
                click menu item "Save as PDF…" of menu 1 of menu button "PDF" of PDFref
                tell application "Mail"
                    display dialog "Proposed Name " & my FlName default answer FlName & "change in the Box if Not OK"
                    set FlName to text returned of result
                end tell
                keystroke FlName
            end tell
        end tell
    else
        set Dialogresult to the button returned of (display dialog "Close this eMail" buttons {"Yes", "no"} default button "No")
        if Dialogresult is "Yes" then
            close window 1
        end if
    end if
end tell

on FixFileName(str) --Deletes characters that cannot be used in file names
    set fixed_string to {}
    set bad_char to {":", "/"}
    repeat with c from 1 to (count every character in str)
        if bad_char contains (character c of str) then
            set end of fixed_string to "-"
        else
            set end of fixed_string to (character c of str)
        end if
    end repeat
    fixed_string as string
end FixFileName

Peter

Upvotes: 0

Views: 2532

Answers (3)

ch3n
ch3n

Reputation: 11

One other possiblity to consider is writing a separate PDF Applescript and adding it to the PDF pulldown menu via "Edit Menu". (Not sure what application you would have to TELL... Check out this link for a lead: http://hints.macworld.com/article.php?story=2004122222145585) Here's the excerpt from Mac Help:

You can add your own items, such as applications and AppleScript scripts, to the PDF pop-up menu. For example, you can add an AppleScript script that applies a Quartz filter to a PDF file, or you can add an application that opens the PDF file immediately after it’s created.

If you create a Print workflow plug-in in Automator, that plug-in is added automatically. For more information, open Automator, choose Help

Automator Help, and search for “Print workflow plug-in.”

To add an item to the PDF pop-up menu:

Choose File > Print.

Choose Edit Menu from the PDF pop-up menu.

Click the Add (+) button and select the item you want to add.

For example, if you want to open a newly created PDF file in Adobe Acrobat, select Adobe Acrobat. If you’ve created an AppleScript script that applies a Quartz filter to a document, choose that script.

An alias to the item is saved in the PDF Services folder in your Library folder.

Upvotes: 1

fireshadow52
fireshadow52

Reputation: 6516

Following @Chuck's answer, yes that is kind of kludgy, but there is the choose file name command, which lets the user choose the name as well as the location...

set the FlName to (choose file name with prompt "Choose a name and location for the new PDF document:" default name "untitled.pdf")

However, this does not actually create the file. The commands below do.

open for access FlName
close access FlName

Upvotes: 0

Chuck
Chuck

Reputation: 4902

If you're unable to set the name of the PDF when it's generated, but know where it was saved and what it was saved as, use AppleScript to rename and move the file to where you want instead. It might be kind of kludgy to select the location and the name, using a display dialog to let the user choose the name and a choose folder to allow them to select the location.

Upvotes: 0

Related Questions