Sending email with attachment using AppleScript

D

David

Hi I have simple applescript

activate application "Microsoft Entourage"
set filepath to "Macintosh HD:Users:test:del2pdf"
tell application "Microsoft Entourage"
set newMessage to make new draft window
make new attachment at newMessage with properties {file:filepath}
end tell

It works just fine when I call it directly from the script editor.
However I need to execute this script from my application , therefore
I use Carbon framework (see code snippet at the end)
In this case some strange thing happens : sometimes new email message
is created with attachment but attached file has been deleted and when
I click send I got error indicating non-existent attachment file.
Wht;s even more strange that sometimes file is not deleted and sending
is successful.
I double checked my code and I do NOT delete attachment file it
feels like Entourage deletes it before sending email.
Can anybody help with this issue ? Any ideas?

Thanks
David





OSStatus LowRunAppleScript(const void* text, long textLength,
AEDesc *resultData) {
ComponentInstance theComponent;
AEDesc scriptTextDesc;
OSStatus err;
OSAID scriptID, resultID;

/* set up locals to a known state */
theComponent = NULL;
AECreateDesc(typeNull, NULL, 0, &scriptTextDesc);
scriptID = kOSANullScript;
resultID = kOSANullScript;

/* open the scripting component */
theComponent = OpenDefaultComponent(kOSAComponentType,
typeAppleScript);
if (theComponent == NULL) { err = paramErr; goto bail; }

/* put the script text into an aedesc */
err = AECreateDesc(typeChar, text, textLength, &scriptTextDesc);
if (err != noErr) goto bail;

/* compile the script */
err = OSACompile(theComponent, &scriptTextDesc,
kOSAModeNull, &scriptID);
if (err != noErr) goto bail;

/* run the script */
err = OSAExecute(theComponent, scriptID, kOSANullScript,
kOSAModeNull, &resultID);

/* collect the results - if any */
if (resultData != NULL) {
AECreateDesc(typeNull, NULL, 0, resultData);
if (err == errOSAScriptError) {
OSAScriptError(theComponent, kOSAErrorMessage,
typeChar, resultData);
} else if (err == noErr && resultID != kOSANullScript) {
OSADisplay(theComponent, resultID, typeChar,
kOSAModeNull, resultData);
}
}


bail:
AEDisposeDesc(&scriptTextDesc);
if (scriptID != kOSANullScript) OSADispose(theComponent,
scriptID);
if (resultID != kOSANullScript) OSADispose(theComponent,
resultID);
if (theComponent != NULL) CloseComponent(theComponent);
return err;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top