Your prescription for increased productivity and profitability
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.
..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:
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.
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.
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
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
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
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.
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.
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.