# include <Carbon/Carbon.h>
char sendMailScript_singAtt[] = "on send_email_message(subjectLine, messageText, fileAttachment)\n" " tell application \"Mail\"\n" " (* CREATE THE MESSAGE *)\n" " activate\n" " set newMessage to make new outgoing message at end of outgoing messages with properties {subject:subjectLine, content:messageText & return, visible:true}\n" " \n" " (* SPECIFY THE ATTACHMENT *)\n" " tell newMessage\n" " make new attachment with properties {file name:fileAttachment} at after last paragraph\n" " end tell\n" " (*set frontmost to true*)\n" " end tell\n" "end send_email_message"; char sendMailScript_doubAtt[] = "on send_email_message(subjectLine, messageText, fileAttachmentA, fileAttachmentB)\n" " tell application \"Mail\"\n" " (* CREATE THE MESSAGE *)\n" " activate\n" " set newMessage to make new outgoing message at end of outgoing messages with properties {subject:subjectLine, content:messageText & return, visible:true}\n" " \n" " (* SPECIFY THE ATTACHMENT *)\n" " tell newMessage\n" " make new attachment with properties {file name:fileAttachmentA} at after last paragraph\n" " make new attachment with properties {file name:fileAttachmentB} at after last paragraph\n" " end tell\n" " (*set frontmost to true*)\n" " end tell\n" "end send_email_message";
GetAliasHandleForFile(QString fname, AliasHandle * ahandle) { FSRef fref; if(FSPathMakeRef((const UInt8 *)fname.toLocal8Bit().data(), &fref, NULL) == noErr) if(FSNewAlias(0, &fref, ahandle) == noErr && (*ahandle)) return true; return false; }
OSStatus CreateEmailMessageEvent1(AppleEvent *theEvent, char* subjectLine, char* messageText, AliasHandle fileAttachment) { OSStatus err; ProcessSerialNumber PSN = {0, kCurrentProcess}; /* create the container list */ err = AEBuildAppleEvent( 'ascr', kASSubroutineEvent, typeProcessSerialNumber, (Ptr) &PSN, sizeof(PSN), kAutoGenerateReturnID, kAnyTransactionID, theEvent, NULL, "'----':[TEXT(@),TEXT(@),alis(@@)]," "'snam':TEXT(@)", subjectLine, messageText, fileAttachment, "send_email_message"); return err; } OSStatus CreateEmailMessageEvent2(AppleEvent *theEvent, char* subjectLine, char* messageText, AliasHandle fileAttachmentA, AliasHandle fileAttachmentB) { OSStatus err; ProcessSerialNumber PSN = {0, kCurrentProcess}; /* create the container list */ err = AEBuildAppleEvent( 'ascr', kASSubroutineEvent, typeProcessSerialNumber, (Ptr) &PSN, sizeof(PSN), kAutoGenerateReturnID, kAnyTransactionID, theEvent, NULL, "'----':[TEXT(@),TEXT(@),alis(@@),alis(@@)]," "'snam':TEXT(@)", subjectLine, messageText, fileAttachmentA, fileAttachmentB, "send_email_message"); return err; }
OSStatus ExecuteAppleScriptEvent(const void* text, long textLength, AppleEvent *theEvent, AEDesc *resultData) { ComponentInstance theComponent; AEDesc scriptTextDesc; OSStatus err; OSAID contextID, resultID; /* set up locals to a known state */ theComponent = NULL; AECreateDesc(typeNull, NULL, 0, &scriptTextDesc); contextID = kOSANullScript; resultID = kOSANullScript; /* open the scripting component */ theComponent = OpenDefaultComponent(kOSAComponentType, typeAppleScript); if(theComponent != NULL) { /* put the script text into a Apple event descriptor record */ err = AECreateDesc(typeChar, text, textLength, &scriptTextDesc); if(err == noErr) { /* compile the script into a new context. The flag 'kOSAModeCompileIntoContext' is used when compiling a script containing a handler into a context. */ err = OSACompile(theComponent, &scriptTextDesc, kOSAModeCompileIntoContext, &contextID); if(err == noErr) { /* run the script */ err = OSAExecuteEvent( theComponent, theEvent, contextID, 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, kOSAModeDisplayForHumans, resultData); } } } } else err = paramErr; AEDisposeDesc(&scriptTextDesc); if(contextID != kOSANullScript) OSADispose(theComponent, contextID); if(resultID != kOSANullScript) OSADispose(theComponent, resultID); if(theComponent != NULL) CloseComponent(theComponent); return err; }
void SendSingleFileToEmail(QString subj, QString body, QString attPath) { AliasHandle ahandle; AppleEvent theEvent; if(!GetAliasHandleForFile(attPath, &ahandle)) return; if(CreateEmailMessageEvent1(&theEvent, subj.toLocal8Bit().data(), body.toLocal8Bit().data(), ahandle) == noErr) ExecuteAppleScriptEvent(sendMailScript_singAtt, strlen(sendMailScript_singAtt), &theEvent, NULL); } void SendDoubleFileToEmail(QString subj, QString body, QString attPathA, QString attPathB) { AliasHandle ahandleA, ahandleB; AppleEvent theEvent; if(!GetAliasHandleForFile(attPathA, &ahandleA) || !GetAliasHandleForFile(attPathB, &ahandleB)) return; if(CreateEmailMessageEvent2(&theEvent, subj.toLocal8Bit().data(), body.toLocal8Bit().data(), ahandleA, ahandleB) == noErr) ExecuteAppleScriptEvent(sendMailScript_doubAtt, strlen(sendMailScript_doubAtt), &theEvent, NULL); }
Source: https://habr.com/ru/post/104633/
All Articles