10 Replies Latest reply: Mar 18, 2013 9:40 AM by bforeste
bforeste Level 1 Level 1 (0 points)

Hi - very new to automator, but now very motivated to find a solution to this annoying problem.

 

I have to scan expense receipts each week and upload them 1 by 1 into a client time & expense system. I don't think Automator can help me with this one.

 

A second system requires all the expense receipts to be merged into a PDF and a 3rd copy emailed to an approver.

 

This is what I would like automator to do:

  1. From my Expenses folder, where all my individual PDF's are located
  2. I select all my distinct PDF's for a particular week
  3. Using an automator service, right click style
  4. Using 'Ask for Text', I would enter the "Week of"
  5. My selected PDF's are merged into 1 PDF, with the name = "Week of <VAR>"
  6. Mail starts (can't figure out how to use Gmail in a browser in Automator)
  7. Creates a message to the approver guy with the merged attachment from #5 above
  8. Sends the message
  9. Quits Mail

 

I can get the automator to run without the variables trick...but it really would save a lot of time if I could just key it in once (instead of #filename, #email subject, #body of the message).

 

I also tried looking up the UUID for the variable and inserting it into the email body and subject line - but it didn't take.

 

Can anyone help with this?

 

Many thanks

 

bforeste


MacBook Pro, Mac OS X (10.6.8)
  • VikingOSX Level 6 Level 6 (9,370 points)

    Well, if this cannot be done in Automator, one can open Preview.app in thumbnail view. To merge PDF, you simply drag and drop each scanned PDF from the Finder into the end of displayed thumbnails in Preview.app. Then you save the document. Preview supports text annotation to handle the “Week of ...” text.

     

    Now tap the Share button on the Preview toolbar and choose Mail. Your merged and annotated PDF is attached to a new Mail message that is then sent off to that third approver.

     

    Not exactly mash a button, get an answer simplicity as a script might provide. It might be a functional interim solution until you get all the kinks out of an Automator/AppleScript solution.

     

    - OS X 10.8.2

  • softwater Level 5 Level 5 (5,370 points)

    Can't help you with automator, but you can create an app with the applescript below that will do what you want.

     

    Procedure:

     

    1. Open the Applescript Editor by typing Apples in the spotlight search field and hitting 'return' on your keyboard.

     

    2. Copy the entire script below and paste it into the Editor window.

     

    3. Hit 'Command-K' on the keyboard and ensure there are no compiler errors. If there are, please look at the script and see if one part of it was highlighted. Report back what part of the script was highlighted and any error messages.

     

    If the compile didn't produce an error, then:

     

    4. Hit 'Command-S' on the keyboard, choose a snazzy name ("PDF merger" or something...) and a location to save it in (your Apps or Desktop folders).  Be sure to change the 'File Format:' to Application at the bottom of the Save screen before hitting 'Save'.

     

    5. The first time you run the app (you run it by double-clicking on it, like any other App), you'll be asked to put in the details manually. On subsequent runs, it will fill in the defaults for you. I haven't set up the default locations for looking and searching for the files as I'm not sure where they would be on your system. I can do that if you tell me the path from your 'home' or user directory.

     

    Also note, I've written the script so that it doesn't send the message until you've reviewed it. i.e., you might want to double check you've attached the correct file, or add a message. If you want it to send automatically without review change the following lines near the end of the script:

     

    change the line 'set visible to true' to 'set visible to false'

    change the line 'save' to 'send'

     

    property defaultFolder : ""

    property msgSubject : ""

    property theRecipient : ""

    property theWeeklyname : ""

    property outputFile : ""

     

    getInfo()

    mergePDFS()

    sendMail()

     

    on getInfo()

     

              display dialog "Type the recipient's email address: " default answer theRecipient

              set theRecipient to the text returned of the result

              delay 0.25

              display dialog "Set the subject of the message: " default answer msgSubject

              set msgSubject to the text returned of the result

    end getInfo

     

     

    on mergePDFS()

              display dialog "Please choose the files to merge…" default button "OK"

              set inputFiles to choose file of type "PDF" with multiple selections allowed without invisibles

              delay 0.25

              display dialog "Please choose a folder to save the merged PDF…" default button "OK"

              set outputFolder to choose folder

              delay 0.25

              display dialog "Type the name of the combined pdf (without the .pdf extension): " default answer theWeeklyname

              set theWeeklyname to text returned of the result

              set outputFile to (outputFolder as text) & theWeeklyname & "(" & (count of inputFiles) & ").pdf"

     

              set pdfFiles to ""

              repeat with p in inputFiles

                        set pdfFiles to pdfFiles & " " & quoted form of POSIX path of p

              end repeat

     

              do shell script "/System/Library/Automator/Combine\\ PDF\\ Pages.action/Contents/Resources/join.py " & "-o " & quoted form of POSIX path of outputFile & pdfFiles

              return outputFile as alias

    end mergePDFS

     

     

    on sendMail()

              tell application "Finder"

                        set theAttachment to outputFile as alias

              end tell

              tell application "Mail"

                        set newMessage to make new outgoing message with properties {subject:msgSubject, content:"" & return & return}

                        tell newMessage

     

                                  set visible to true

      make new to recipient at end of to recipients with properties {address:theRecipient}

                                  tell content

      make new attachment with properties {file name:theAttachment} at after the last paragraph

                                  end tell

      save

                        end tell

              end tell

    end sendMail

  • VikingOSX Level 6 Level 6 (9,370 points)

    I wonder if you want these “mail attachment” properties {name: theAttachment, MIME type: application/pdf} per the dictionary entry for "mail attachment?” Seems to me there would be benefits to having the correct MIME type set for the combined PDF attachment.

     

    Unsaid, but assumed is that the OP has already set the Send Windows friendly attachments flag in Mail Composition attachments window, as it does not appear to be available to AppleScript.

  • softwater Level 5 Level 5 (5,370 points)

    VikingOSX wrote:

     

    I wonder if you want these “mail attachment” properties {name: theAttachment, MIME type: application/pdf} per the dictionary entry for "mail attachment?”

     

    I think you might be confusing classes, Viking. 

     

    In the dictionary, compare the class 'Mail attachment' (which belongs to a received message and has the read only MIME property) with the class  'attachment' from the Text Suite. It's the latter I've used here, not the former.

     

    That said, the script is rough round the edges (no try blocks or error handlers, default dialgo boxes, etc — I was in a hurry... ), and I'm sure others can improve on it if the OP is interested.

  • VikingOSX Level 6 Level 6 (9,370 points)

    The script is a good initial attempt for “in a hurry,” and you did catch me class confused.

  • Ultrazero Level 1 Level 1 (145 points)

    I can get you close to what you want to do in Automator with a couple compromises.

     

    #1) While you can use python scripts for sending an email through gmail, its easier in a pinch to use the stock Automator actions provided and run it through Mail.app. I see you're doing that anyway.

     

    #2) To rename the PDF and set values in your mail message, I'm using the Month/Day/Year variables that come with Automator. Unfortunately I don't know how to set those values so it can target a specific day in a given week. (say you always want to target mondays so it reads "Week of Monday, 3/11/13).  What you get now is the day it is sent.

     

    #3) Running a Quit Mail action presents a problem because depending on your network connection or the size of the PDF you're trying to send, mail might not be done sending the message. If you really need Mail to quit, you can put a pause in the workflow right before the 'quit' action and guestimate the longest it'll take to send most of your messages, but it's not optimal.  Also, what if you have other mail messages that you're working on? I'd leave that action off.

     

     

    I'm on Mountain Lion and I can't test this on 10.6.8 at the moment so let us know if it works.

     

     

     

    send-pdfs.png

  • bforeste Level 1 Level 1 (0 points)

    Softwater - thanks for the help. This did indeed do what you laid out, but with so many prompts to go through (even after it auto-fills on the subsequent pass) I think my basic Automator is less manual. I'm sure I can script the entire body of the email message - as it's always the same...but I'm still more comfortable tweaking the visual layout as opposed to scripting.

     

    I may end up using this though - have to play around with Ultrazero's automator a bit first.

     

    Many thanks

     

    bforeste

  • bforeste Level 1 Level 1 (0 points)

    Ultrazero - I ran into issues executing this...not sure why. I laid it out exactly as you had it, when I execute - I get the following: "Automator could not convert the input data to the desired type".

     

    Also - I'm really trying to get the 'custom variable' thing to work, as I compile my receipts sometimes several days (or weeks) after I incur them. Today, I might prepare receipts for March 4th, for example.

     

    So - having the 'text input' [ENTER TRAVEL WEEK] converted into the Variable that is used to 1) name the merged file, and 2) insert into the Subject of the email...is ideal for me.

     

    Thanks for any suggestions - otherwise, I may use the script or my first Automator attempt.

     

    regards

     

    bforeste

  • Ultrazero Level 1 Level 1 (145 points)

    Do you know which action is causing the error? Run the workflow in automator to help diagnosis the problem.

  • bforeste Level 1 Level 1 (0 points)

    This won't run inside Automator & it would probably require another post / investigation thread to figure out 'how' to get this to run & debug it. So - I'm giving up.

     

    I'll use my simple automator that merges the PDF and plops it into the email. It's still saving me time. Maybe once I upgrade beyond 10.6.8 I'll have more capabilities to configure this properly. By that time, I probably won't have the need anymore...

     

    Thanks for the tips though.

     

    bforeste