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

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

Archive