« Why I feel bad for Nancy Pfotenhauer | Main | Jerry Yang's new song »

Election Day stupid AppleScript tricks

Since I voted early, and I had to do some work in Pages that would end up being emailed, I decided to correct an annoyance of Pages and Keynote, namely, the inability to send the current document to an email program as an attachment. I mean, Word and PPT have this, why not Keynote & Pages. (Numbers isn't scriptable, so I don't care about that application until it is.)

Turned out to be relatively simple, even getting it to work with both Mail and Entourage. Normally, Mail's broke-ass scripting makes me give up. This time, it worked. But then it's simple enough. I'll post the source below, and a link to the zipfile is here.

They're both compiled data-fork scripts, so they work best in the Mac OS X script menu. Just open up the script folders for Pages and Keynote, drop in the appropriate scripts, and off you go. The scripts check for two possible errors:

  1. You don't actually have a document open. In that case, you get fussed at, and the script exits. Open a document ya goof.

  2. The document is open, but has never been saved. In that case, it doesn't really exist on the filesystem, so there's no way to get a path to it. Save your work, what, you want to lose it all?
Some caveats:

Source for the Pages versions –

Entourage:

tell application "Pages"
     set theWindow to every window whose index = 1
     try
          set theDoc to document of item 1 of theWindow
          try
               set theDocPath to path of theDoc as POSIX file
               tell application "Microsoft Entourage"
                    activate
                    set theMessage to make new draft window with properties {send attachments to cc recipients:true}
                    make new attachment at theMessage with properties {file:theDocPath}
               end tell
          on error
               display dialog "Make sure you save your document before trying to email it"
          end try
     on error
          display dialog "Make sure you have a Pages file open, or there's nothing to send"
     end try
end tell

Mail:

tell application "Pages"
     set theWindow to every window whose index = 1
     try
          set theDoc to document of item 1 of theWindow
          try
               set theDocPath to path of theDoc
               tell application "Mail"
                    activate
                    set theMessage to make new outgoing message with properties {visible:true}
                    make new attachment at end of theMessage with properties {file name:theDocPath}
               end tell
          on error
               display dialog "Make sure you save your document before trying to email it"
          end try
     on error
          display dialog "Make sure you have a Pages file open, or there's nothing to send"
     end try
end tell

The source for both Entourage and Mail is simple. The first line grabs the window with an index of 1. If there are no windows open, you get an empty list. I could check for that, but then I have to use really different logic for Keynote, (which always has a window, even if there are no visible windows open), and that would be annoying.

We then have the first try block, which is to set theDoc to the document of item 1 of the list of windows we got in the previous line. If that errors out, then we get a dialog, and the script ends. If it works, we dump into another try block, and get the path of the documentation. For Entourage, we coerce it to a POSIX path, for Mail, we leave it as text. (The difference is so each application can find the file when we set up the attachment.) If this errors out, it means we have a document, but it's never been saved, and so has no path. Display appropriate dialog and end the script.

If it works, then it's pretty straightforward. Bring Entourage to the front, make a new draft window and set it to send the attachment to cc recipients, then attach the Pages file.

The Mail version is the same, except for the syntax of the attachment and the new message.

Source for the Keynote Version –

Entourage:

set theDoc to {}
tell application "Keynote"
     set theWindow to every window whose index = 1
     try
          set theName to name of item 1 of theWindow
          if theName is not "" then
               set theDoc to slideshow of item 1 of theWindow
               try
                    set theDocPath to path of theDoc as POSIX file
                    tell application "Microsoft Entourage"
                         activate
                         set theMessage to make new draft window with properties {send attachments to cc recipients:true}
                         make new attachment at theMessage with properties {file:theDocPath}
                    end tell
               on error
                    display dialog "Make sure you save your presentation before trying to email it"
               end try
          else
               display dialog "Make sure you have a Keynote file open, or there's nothing to send"
          end if
     on error
          display dialog "Make sure you have a Keynote file open, or there's nothing to send"
     end try
end tell

Mail:

set theDoc to {}
tell application "Keynote"
     set theWindow to every window whose index = 1
     try
          set theName to name of item 1 of theWindow
          if theName is not "" then
               set theDoc to slideshow of item 1 of theWindow
               try
                    set theDocPath to path of theDoc
                    tell application "Mail"
                         activate
                         set theMessage to make new outgoing message with properties {visible:true}
                         make new attachment at end of theMessage with properties {file name:theDocPath}
                    end tell
               on error
                    display dialog "Make sure you save your presentation before trying to email it"
               end try
          else
               display dialog "Make sure you have a Keynote file open, or there's nothing to send"
          end if
     on error
          display dialog "Make sure you have a Keynote file open, or there's nothing to send"
     end try
end tell

There are a few differences over the Pages versions, all Keynote-related. Since there is always a window, I end up getting a lot of nulls for the slideshow, (aka "document") of the window. So, because I like things simple, I just check for the name. If it's "", then it's one of Keynote's invisible windows, dialogs pop, and the script ends. If it works, then we get the path. If there's no path, the document has never been saved, we get the appropriate dialogs, and the script quits.

The Entourage and Mail bits are the same here, so I'll not repeat them.

As always, change them how you wish, but if you're going to use my source to start, my ONLY licensing requirement is that you keep my name in a comment as the original author.

Enjoy.

Categories:     Applescript, Entourage X Scripts, Mac Matters, Mac OS X Scripts, Mail Scripts, Other
Posted by John C. Welch at 17:14 | Permalink



Comments

Warning for Notes users: The commenting system uses HTML.
I know this will be scary for some of you, especially Notes fans. However, open standards, rah-rah.
If you want to use less-than or greater-than signs, or other similar charachters that HTML reserves,
you'll simply have to learn to do it the HTML way. Luckily, HTML is kind of popular, no matter what
your re-educators have told you, and you can easily find help on the intertubes.
digital.forest Where Internet solutions grow

There, a PayPal Button.

 
Use this code for your Macworld tickets!
Family
The Artwork of Melissa Findley
Diane Francis @ the National Post Eric Francis @ the Calgary Sun

Apple Amazon Links
Apple Mac OS X Server 10.5 [Unlimited]

Apple Mac OS X Server 10.5 [10-Client]

Apple Mac OS X 10.5 Leopard

Apple Mac OS X 10.5 Leopard [5-User Family Pack]

Amazon Book Links
Legacy of Ashes: The History of the CIA

The Donnas: Bitchin'

Wizards at War (The Young Wizards, Book 8)

The Demon's Sermon on the Martial Arts

The Collected Stories of Arthur C. Clarke

JavaScript and Ajax for the Web, Sixth Edition

Awakening Warrior: Revolution in the Ethics of Warfare

FOB Links

Mac Web Writers

Techie Links

Review Victims