Automating Adobe InDesign CC 2015


Placing Email Content In InDesign

Friday, July 08, 2016

Placing Mail Content in InDesign

A recent post to the Adobe InDesign Scripting forum asked about a script to place email content into an InDesign document. Of course, this got me to thinking about the Macintosh users out there who haven't discovered the power of AppleScript.

If you are a Macintosh user and you are new to the idea of using scripts for automation. Or, perhaps you don't have that little black AppleScript scroll logo sitting at the top of your computer in the menu bar. For whatever the reason you haven't worked with AppleScript, this blog is for you.

Prerequisite: you need to have the AppleScript Editor installed. If you don't, you will find it in the Applications folder for your Macintosh. (You may need to look in the Utilities folder to find the application.)

Click on the application to launch it and be sure to add it to your Dock.

Now that you have the AppleScript Editor running in the foreground, open its Preferences panel (from the AppleScript Editor menu or using the quick key combination Command+,).

In the General panel for the Preferences Panel you will see options for having the Script menu added to the menu bar with scripts provided.

Preferences Panel for AppleScript Editor...Adding Script Menu and Scripts

Enable your preferences, and be prepared to have some eye-opening automation available at your finger tips. So let's explore.

With the AppleScript icon in the menu bar, click on it to open a list of the scripts added. In the list is a folder of scripts for each of the following:

  • ColorSync
  • Folder Actions
  • Font Book
  • Mail Scripts
  • Script Editor Scripts
  • UI Element Scripts
  • VoiceOver

Additionally, you can add your own folders for your favorites to this impressive list.

For now, we will look at a sampling. Highlight the listing for the Mail Scripts folder.

The Mail Scripts Folder

Inside the Mail Scripts folder you have scripts to automate your Mail application including one that allows you to get Usenet style "kill" file functionality in Mail. This script is named Remove Messages from Sender or Thread.

If you don't find a script that does exactly what you are wanting, take a look at scripts having titles indicating they may provide clues or code for writing your own script.

To open a script from the script list, Option + click on the name of the script in the list.
Or, open the Scripts Folder and Double Click on the script. In either event, the script will open in AppleScript Editor.

As an example: the person on the InDesign forum was looking for a script that would take the contents of an email and place it into an InDesign document.

A script to automate this functionality presents a number of tasks that need to be resolved:
Task 1: Define the target email: Get a reference to a selected email or get the email reference by name of sender, date sent, or subject.
Task 2: Make sure InDesign is open with insertion point selected (at location where text from email is to be inserted).
Task 3: Extract content from email and insert it at insertion point location.

Get Selected Email

The easiest solution to the first task above would be to get the reference to the email by selection. In browsing through the scripts provided, you will find several that target a selected email. For instance, from the Summarize Message script we read:

tell application "Mail" 
     set selectedMessages to selection
     set selectionCount to (count of selectedMessages)
     ...

For the purpose of our sample script, we could decide to bail the user out of the script with an on error trap if there is no selection in Mail. Using some of the code from the Summarize Message script, we come up with:

try
   tell application "Mail"
     set selectedMessages to selection
     set selectionCount to (count of selectedMessages)
     if selectionCount is 0 then 
      error ("Requires Mail message to be selected)
     end if
     -- if there is a selection the script can continue
     set messageRef to item 1 of selectedMessages
   end tell
on error errMsg
   activate
   display alert "Error: " & errMsg
end try

Get InDesign Insertion Point Reference

The second task for our script is to determine if there is an insertion point active in an InDesign document. The code for this could be similar to that for our first task above:

   set errMsg to "Requires insertion point reference in active document")
   tell application "Adobe InDesign CC 2015"
      set selectedList to selection
      set selectedCount to count of selectedList
      if selectedCount is 0 then
         error  errMsg
      end if 
      if class of item 1 of selectedList is not "insertion point" then
         error errMsg
      end if
      set insertionRef to item 1 of selectedList
   end tell

Extract Mail Message

The clue to how to write the code to extract the content from the Mail message also came from the Summarize Message script. Near the bottom of the script we read:

   set summary to summarize (content of theMessage as string)

With this bit of code, we can write the following.This will require access to the messageRef variable from Get Selected Email code above

   tell application "Mail"
      set messageContent to (content of messageRef as string)
   end tell 

The Finished Script

   try
      set messageText to getSelectedEmail()
      if messageText is not "" then
         placeMessageText(messageText)
      else
        error "No message content returned from selected email"
      end if
   on error errStr
      activate
      display alert "Error: " & errStr
   end try
   --HANDLERS--
   on getSelectedEmail()
      tell application "Mail"
         set selectedMessages to selection
         set selectionCount to (count of selectedMessages)
         if selectionCount is 0 then
            error ("Requires Mail message to be selected")
         end if
         -- if there is an email selected the script can continue
         set messageRef to item 1 of selectedMessages
         return content of messageRef
      end tell
   end getSelectedEmail

   on placeMessageText(theText)
      set errStr to "Requires insertion point selection in active document"
      tell application "Adobe InDesign CC 2015"
         set selectedList to selection
         set selectedCount to length of selectedList
         if selectedCount is 0 then
            error errStr
         end if
         if class of item 1 of selectedList is not insertion point then
            error errStr
         end if
         set insertionRef to item 1 of selectedList
         set contents of insertionRef to theText
      end tell
   end placeMessageText 

Notice how we combined tasks 1 and 3 into a single handler since they were both targeted to the same application. We also changed task 2 to not only get a reference to the selected insertion point, but expanded it to have the insertion point place the text if the value of the variable messageText was not empty.

On Your Own

    Think of ways the script above could be expanded:
  • Set styling for incoming text (applied style for insertion point)
  • Filter incoming text (remove empty paragraphs, etc.)
  • Add date file was sent, sender, etc. to insertion point as part of placing the message content.

Onward and Upward

Looking at some of the code in the Mail Scripts folder actually gave me some ideas for some automation scripts. For instance: I have never set up Rule Actions in Mail to automatically delete spam. Now I know this is one thing I want to look into. Until then, I wrote a quick little script to allow me to delete all of the spam mails I get from one particular sender.

Delete Emails by Sender

   set msg to "Enter name of sender for mail deletion"
   set userResponse to display dialog (msg) default answer ""
   if text of userResponse is not "" then
	set senderName to text returned of userResponse
	tell application "Mail"
	   set selectedMessages to messages of mailbox "INBOX" of account "iCloud"
	   repeat with eachItem in selectedMessages
		set theMessage to eachItem
		set theSource to sender of theMessage
		if theSource contains senderName then
		   delete eachItem
		end if
	   end repeat
	end tell
	theSource
   end if

Do some exploring in the Scripts folder now that you have it on your Macintosh. I'm sure you will find some scripts there that you may want to take advantage of.

Trackback Link
http://www.yourscriptdoctor.com/BlogRetrieve.aspx?BlogID=18153&PostID=1465311&A=Trackback
Trackbacks
Post has no trackbacks.

Recent Posts


Tags

Repeat With Change Text Custom Dialog Border Panel PDF text item delimiters Image Placement Fixed Format ePub AppleScript Templates Templates lists fit Find by Font pasteboard handlers stylesheets InDesign dialogs Placing images if/then metacharacters Checkbox Control file paths selection text placement Numbering AppleScript dialogs BrdgeTalk Styling Text directory user interaction level enable redraw AppleScript import XML text concatenation save template Custom Buttons Character Styles parent page try/on error Excel PDF export preferences do script Create Document Enabling Groups styles XML XML structure folder active spread ePub for iPad text editbox Index of blogs and scripts for year 2016 Library Folder Embed Document PDF security images parent with timeout import text InDesign template transform Document stylesheets System Events Fixed Layout ePub Combobox placeable table styles CSS InDesign CC Create Text Frame doScript import HTML functions measurement editboxes Find by Color new document script templates Autoflow Adobe InDesign CC Find by Attribute Bullets transformation matrix Custom CSS Style Sheets repeat loops Table cells External Object Scripts CALS tables Radiobutton Group scripting Glossary path resize Automating animations Script Preferences paragraph styles Editbox Document Presets page items Nested Styles Choose file arrays ePub for Kindle Media Find Text Find Change entire path image metadata massage text cut and paste Publish Online say Apple apps records tutorial automated workflow GPS Export HTML wild cards Document from Template Caption Variable watermark Mail Adding Captions Read text file as list trimText Hyperlinks resolve measurements Metadata Choose from list script folder Preferences GPS metadata coordinates transform again Text Cleanup HTML Export find change options Sound Interactive Publications Video Export to PDF Tables automating InDesign dropdowns integer editboxes reframe automation XML tags object styles

Archive