InDesign scripting : lesson 22

performing tedious, repetitive tasks — no matter how small — is monkey-work. automating those tasks makes you just that little bit less like a monkey. if you take a moment to think about it, you may find you’re wasting an awful lot of life on mundanities …

… such as creating and emailing proofs for clients. this script will export a pdf, attach it to an email, then add the recipient, subject line and basic message — all with just three double-clicks.

the first part of the script exports the pdf. exporting pdfs was covered way back in scripting lessons 08 thru 12, so refer back to them if you need an explanation of what’s going on here :

tell application "Adobe InDesign CS4"
  activate
  save active document
  set mgPdfPresets to name of every PDF export preset
  set mgPdfPreset to choose from list mgPdfPresets with prompt "How you want these done then?" with title "CHOOSE PDF EXPORT PRESET"
  if mgPdfPreset is false then
    display dialog "WHATEVER!" buttons "get stuffed" giving up after 1
    error number -128
  end if  
  
  set properties of PDF export preferences to properties of PDF export preset (item 1 of mgPdfPreset)
  set page range of PDF export preferences to all pages
  set mgFolder to file path of active document
  
  tell application "Finder"
    if (exists folder "PDF" of folder mgFolder) is false then
      make new folder at mgFolder with properties {name:"PDF"}
    end if
  end tell
  set mgDocName to name of active document
  set OrigDelims to text item delimiters of AppleScript
  set text item delimiters of AppleScript to {"."}
  set mgShortName to text item 1 of mgDocName
  set text item delimiters of AppleScript to OrigDelims
  
  set mgFilePath to mgFolder & "PDF" & ":" & mgShortName & ".pdf" as string
  tell active document
    export format PDF type to mgFilePath without showing options
  end tell
end tell

tell application "Finder"
  set mgFile to mgFilePath as alias
end tell

this uses the choose from list command which will present the user with a dialog listing their available export presets. it’ll look something like this :
screen grab of pdf export selection dialog

just for shits and giggles we’ve added a surly message for any user who dares to cancel that dialog instead of proceeding.

the last part of that script above gets a reference to the pdf as an alias — ready to attach to the email.

the next part of the script sets up the details for the email. this workflow has a small number of possible email recipients (just the four sales reps who deal directly with clients) — you might want to come up with a different solution if your list is longer :

set mgChoice to choose from list {"macGrunt", "macGroan", "macGrumble", "John"} with prompt "Who you want this sent to?"
if mgChoice is false then
  error number -128
end if
set mgChoice to item 1 of mgChoice
if mgChoice is "macGrunt" then
  set mgAddress to "macgrunt@macgrunt.com"
else if mgChoice is "macGroan" then
  set mgAddress to "macgroan@macgrunt.com"
else if mgChoice is "macGrumble" then
  set mgAddress to "macgrumble@macgrunt.com"
else if mgChoice is "John" then
  set mgAddress to "john@macgrunt.com"
end if

set mgContent to "Hi " & mgChoice & return & return & "Here's your groovy proof" & return

first the user is asked to select from another list that might look something like this :
screen grab of email recipient dialog
then, based on that choice, the email address is set. finally verbage for the email content is set.

the last part of the script sets up the actual email and will need to change depending on which email software you are using. for example, this is how you could do it for Mail :

tell application "Mail"
  activate
  set mgMessage to make new outgoing message with properties {visible:true, subject:mgShortName, content:mgContent}
  tell mgMessage    
    make new to recipient with properties {name:mgChoice, address:mgAddress}
    tell content of mgMessage
      make new attachment with properties {file name:mgFile} at after last paragraph
    end tell
  end tell
  save mgMessage
  set visible of mgMessage to true
end tell

you’ll notice that, for this workflow, we’re using the shortened filename as our subject line.

this is how it would go if you needed to do the same thing with Outlook :

tell application "Microsoft Outlook"
  activate
  set mgMessage to make new outgoing message with properties {subject:mgShortName, content:mgContent}
  make new recipient at mgMessage with properties {email address:{name:mgChoice, address:mgAddress}}
  make new attachment at mgMessage with properties {file:mgFile}
  open mgMessage
end tell

you should be able to use these as a starting point — just open the relevant scripting dictionary in script editor to work out the syntax specific to your email software. if it all becomes too hard — google is your friend.

well, that’s it. a relatively small time-saving, but just think how many times you repeat that simple monkey-task every week.

macgrunt icon

thoughtful and respectful comments welcome

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s