Open mail client with new mail and attaching a file.

Hi Everyone,

Sometimes we need to open the email client (Mail in MAC OSX) with an automatic attachment after that user can write subject and body text. For instance, in a servoy application on clicking on some button if we need open a email client with automatic new email creation and need to attach a file on the fly then we can achieve this by using an apple script.

Following is the function to open an email client with automatic new email creation and attaching a file to it by using an apple script and it will work for the mail clients which are scriptable only (like mail in MAC OSX ). Here, you can also observe , how we can run the apple script in servoy.

function openEmailClientWithAttahment() {

   // Assign the apple script to global variable.
		globals.g_APPLE_SCRIPT = 'tell application "mail" \n'+
 									' set newMessage to (a reference to (make new outgoing message)) \n'+
	
										'tell newMessage \n'+
											  'make new recipient at beginning of to recipients ¬ \n'+
 												'with properties {address:"'+email+'"} \n'+
		
													'set the subject to "Type Subject Here" \n'+
 														'set the content to "\n \n \n \n \n \n \n \n \n" \n'+
		
 															'tell content \n'+
																'make new attachment ¬ \n'+
 																	'with properties {file name:"'+Path of an attachment file+'"} ¬ \n'+
																	 	'at after the last paragraph \n'+
										                                        'end tell \n'+
		
							                                                'set visible to true \n'+
							                        'end tell \n'+
						        'end tell';

		// Create a apple script file.								 			
		plugins.file.writeTXTFile('my_script',globals.g_APPLE_SCRIPT);

		// Execute the apple script.
		var res = application.executeProgram('osascript','my_script');
}

We can do the same for windows by using vb Script. Hope this will help someone :) .

Thanks
Chaitanya s