Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > Applescript & Cocoa: Mail a pdf attachment

Applescript & Cocoa: Mail a pdf attachment
Thread Tools
Mac Enthusiast
Join Date: Sep 2000
Location: Vermont, USA
Status: Offline
Reply With Quote
Sep 2, 2003, 06:52 PM
 
OK, I almost have this figured out. I want my application to send an email using Applescript that has a pdf file as an attachment. I am providing the attachment path as a static path.
I always get an NSCannontCreateScriptCommandError (or something like that) for the
make new attachment... line of code. I don't know much about Applescript, so maybe this will be easy for someone?

The Applescript CODE from Script Editor:

tell application "Mail"

(* SPECIFY GENERAL CONTENT OF MESSAGE *)
set bodyvar to return & return & "Test body."
--set addrVar to "bogus@apple.com"
set addrNameVar to "Guinea Pig"

set myrecipient to "bogus@nowhere.com"

set theAttachment to "~/Documents/Test.pdf"

set subjectvar to "Email with Attachment!"

set mymail to (make new outgoing message at the beginning of outgoing messages with properties {subject:subjectvar, content:bodyvar})
tell mymail to make new to recipient at beginning of to recipients with properties {name:addrNameVar, address:myrecipient}

tell content
make new attachment with properties {file name:theAttachment} at before the first word of paragraph 0
end tell

set visible of mymail to true



end tell

Test.pdf is a real file in my Documents folder.

Thanks in advance!
     
Professional Poster
Join Date: Nov 2000
Location: Tasmania, Australia
Status: Offline
Reply With Quote
Sep 2, 2003, 09:22 PM
 
Just a guess (not having tried to use your code), but I would think that AppleScript would be unable to interpret the "~" in your file path. Certainly in ObjectiveC you have to expand the tilde to the full file path before using it (using [string expandTildeInPath] or something similar).

Try it with the fully qualified path name. The tilde character is usually used for the shell which then expands it, and in more recent times other programs have taken it on, but I doubt AppleScript would know what to do with it.
     
Mac Enthusiast
Join Date: Sep 2000
Location: Vermont, USA
Status: Offline
Reply With Quote
Sep 3, 2003, 12:29 AM
 
Thanks for the suggestion, but I still get the same error.

Any other suggestions folks?
     
Professional Poster
Join Date: Oct 2001
Location: London
Status: Offline
Reply With Quote
Sep 3, 2003, 04:02 AM
 
Two things:

1 - The user may not want to use mail.app

2 - Sometimes (in fact often) Cocoa apps do not support the "Make new x with y, z" syntax.

you may need to do:

make new X
make new Y at front of X

or

make new X
set properties of X to {foo: Z}
     
Mac Enthusiast
Join Date: Sep 2000
Location: Vermont, USA
Status: Offline
Reply With Quote
Sep 3, 2003, 06:17 PM
 
The original example code I posted above wouldn't even work in Script Editor. I am trying to first get it to work there, and then I will worry about getting to work in my Cocoa application.

Can anyone get the script above to work. I tried Diggory's suggestions, but still no luck.
     
Professional Poster
Join Date: Oct 2001
Location: London
Status: Offline
Reply With Quote
Sep 4, 2003, 06:57 AM
 
Have a look at:

/Library/Scripts/Mail\ Scripts/Create New\ Message.scpt


for an example of how to attach to new messages
     
Mac Enthusiast
Join Date: Sep 2000
Location: Vermont, USA
Status: Offline
Reply With Quote
Sep 4, 2003, 12:21 PM
 
Thanks Diggory, I figured it out be looking at that script!
     
   
Thread Tools
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Top
Privacy Policy
All times are GMT -5. The time now is 02:21 PM.
All contents of these forums © 1995-2011 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.7 © 2000-2011, Jelsoft Enterprises Ltd., Content Relevant URLs by vBSEO 3.3.2