Applescript to automate "Save as PDF" in print menu

Discussion in 'Mac Programming' started by dmgibney, Jul 27, 2011.

  1. macrumors newbie

    Jul 27, 2011

    I've spent some time searching for a solution but haven't found anything, hence this post. Here's what I would like to do...I often find myself using the "save as PDF" option in the print menu to archive things that I do online (bank receipts, order confirmations, etc...) I save the PDFs to either of two places, a personal records folder for my personal transactions, and a business folder for my business transactions.

    I am trying to write an applescript that I could select from the script menu that would:

    1 - Open the print menu (cmd+p)
    2 - Choose "Save as PDF…" from the print menu
    3 - Prompt me to send it to either my personal or business folder
    4 - Save the PDF to a subfolder for the current year (HD/users/username/docs/personal_records/2011)

    I am new to applescripting, all I've been able to do so far is get the print menu of the current application to open. I did that with this code:

    tell application "System Events"
    keystroke "p" using {command down} -- activate print menu item
    end tell

    Any ideas on how to automate the selection of "Save as PDF" or how to dictate the save location based on the current year?

    Your help is greatly appreciated!
  2. macrumors newbie


    Jul 25, 2011

    It looks like you're still waiting for an answer. Here's a partial one, as I have no idea how to script the Safari part. I haven't done any UI scripting.

    --get the current year 
    set ThisYear to year of (current date)
    --create a dialog to choose where to store the pdf
    set ThisChoice to button returned of (display alert "Where should this be stored?." buttons {"cancel", "business", "personal"} default button "personal")
    if ThisChoice is "personal" then
    	set TheFolder to "HD:users:username:docs:personal_records:"
    else if ThisChoice is "business" then
    	set TheFolder to "HD:users:username:docs:business_records:"
    else if ThisChoice is "cancel" then
    	error -128
    end if
    --create 'year' folder if it does not already exist
    tell application "Finder"
    	if (exists folder ThisYear of folder TheFolder) is false then
    		make new folder at TheFolder with properties {name:ThisYear}
    	end if
    end tell
    --set the final folder path
    set FolderPath to TheFolder & ThisYear & ":" as string

    Hope that helps a little

  3. macrumors newbie

    Mar 28, 2013

Share This Page