2011-12-06 11 views
3

Mail.appを開き、添付する件名とファイルを指定したいと思います。私は単独でも両方でもできません。添付ファイルと件名を持つ外部アプリケーションからMail.appを開く

件名を設定するには、mailto:文字列とNSWorkspaceのopenURLを作成するだけです。私はMac用のiOSのMFMailComposeViewControllerと同等の認識していないよ

[[NSWorkspace sharedWorkspace] openFile:resolvedPath withApplication:@"Mail"]; 

を使用することができます添付ファイルを設定するには

。私の選択肢は何ですか?

答えて

-1

編集:このリンク元のコードは削除されているので、代わりにこの回答の一部をコピーしました。

AppleScript(またはApple Events)を使用してMail.appを駆動できます。これには、渡すことができる添付ファイルのサイズに制限がなく、巨大なURLを構築することを避けるという利点があります。

- (NSAppleScript *)script 
{ 
    if (script) 
    return script; 

    NSDictionary *errorInfo = nil; 

    /* This AppleScript code is here to communicate with Mail.app */ 
    NSString *ourScript = 
    @"on build_message(sendr, recip, subj, ccrec, bccrec, msgbody," 
    @"     attachfiles)\n" 
    @" tell application \"Mail\"\n" 
    @" set mailversion to version as string\n" 
    @" set msg to make new outgoing message at beginning of" 
    @"  outgoing messages\n" 
    @" tell msg\n" 
    @"  set the subject to subj\n" 
    @"  set the content to msgbody\n" 
    @"  if (sendr is not equal to \"\") then\n" 
    @"  set sender to sendr\n" 
    @"  end if\n" 
    @"  repeat with rec in recip\n" 
    @"  make new to recipient at end of to recipients with properties {" 
    @"   name: |name| of rec, address: |address| of rec }\n" 
    @"  end repeat\n" 
    @"  repeat with rec in ccrec\n" 
    @"  make new to recipient at end of cc recipients with properties {" 
    @"   name: |name| of rec, address: |address| of rec }\n" 
    @"  end repeat\n" 
    @"  repeat with rec in bccrec\n" 
    @"  make new to recipient at end of bcc recipients with properties {" 
    @"   name: |name| of rec, address: |address| of rec }\n" 
    @"  end repeat\n" 
    @"  tell content\n" 
    @"  repeat with attch in attachfiles\n" 
    @"   make new attachment with properties { file name: attch } at " 
    @"   after the last paragraph\n" 
    @"  end repeat\n" 
    @"  end tell\n" 
    @" end tell\n" 
    @" return msg\n" 
    @" end tell\n" 
    @"end build_message\n" 
    @"\n" 
    @"on deliver_message(sendr, recip, subj, ccrec, bccrec, msgbody," 
    @"     attachfiles)\n" 
    @" set msg to build_message(sendr, recip, subj, ccrec, bccrec, msgbody," 
    @"       attachfiles)\n" 
    @" tell application \"Mail\"\n" 
    @" send msg\n" 
    @" end tell\n" 
    @"end deliver_message\n" 
    @"\n" 
    @"on construct_message(sendr, recip, subj, ccrec, bccrec, msgbody," 
    @"      attachfiles)\n" 
    @" set msg to build_message(sendr, recip, subj, ccrec, bccrec, msgbody," 
    @"       attachfiles)\n" 
    @" tell application \"Mail\"\n" 
    @" tell msg\n" 
    @"  set visible to true\n" 
    @"  activate\n" 
    @" end tell\n" 
    @" end tell\n" 
    @"end construct_message\n"; 

    script = [[NSAppleScript alloc] initWithSource:ourScript]; 

    if (![script compileAndReturnError:&errorInfo]) { 
    NSLog (@"Unable to compile script: %@", errorInfo); 
    [script release]; 
    script = nil; 
    } 

    return script; 
} 

が、あなたは上記のように、この

- (NSAppleEventDescriptor *)descriptorForThisProcess 
{ 
    ProcessSerialNumber thePSN = { 0, kCurrentProcess }; 

    return [NSAppleEventDescriptor descriptorWithDescriptorType:typeProcessSerialNumber 
          bytes:&thePSN 
           length:sizeof (thePSN)]; 
} 

- (BOOL)doHandler:(NSString *)handler 
     forMessage:(NSAttributedString *)messageBody 
     headers:(NSDictionary *)messageHeaders 
{ 
    NSMutableString *body = [NSMutableString string]; 
    NSDictionary *errorInfo = nil; 
    NSAppleEventDescriptor *target = [self descriptorForThisProcess]; 
    NSAppleEventDescriptor *event 
    = [NSAppleEventDescriptor appleEventWithEventClass:'ascr' 
          eventID:kASSubroutineEvent 
         targetDescriptor:target 
          returnID:kAutoGenerateReturnID 
        transactionID:kAnyTransactionID]; 
    NSAppleEventDescriptor *params = [NSAppleEventDescriptor listDescriptor]; 
    NSAppleEventDescriptor *files = [NSAppleEventDescriptor listDescriptor]; 
    NSString *tmpdir = NSTemporaryDirectory(); 
    NSString *attachtmp = nil; 
    NSFileManager *fileManager = [NSFileManager defaultManager]; 
    NSString *sendr = @"", *subj = @""; 
    NSAppleEventDescriptor *recip 
    = [self recipientListFromString:[messageHeaders objectForKey:@"To"]]; 
    NSAppleEventDescriptor *ccrec 
    = [self recipientListFromString:[messageHeaders objectForKey:@"Cc"]]; 
    NSAppleEventDescriptor *bccrec 
    = [self recipientListFromString:[messageHeaders objectForKey:@"Bcc"]]; 
    unsigned numFiles = 0; 
    NSUInteger length = [messageBody length]; 
    NSUInteger pos = 0; 
    NSRange range; 

    if ([messageHeaders objectForKey:@"Subject"]) 
    subj = [messageHeaders objectForKey:@"Subject"]; 
    if ([messageHeaders objectForKey:@"Sender"]) 
    sendr = [messageHeaders objectForKey:@"Sender"]; 

    /* Find all the attachments and replace them with placeholder text */ 
    while (pos < length) { 
    NSDictionary *attributes = [messageBody attributesAtIndex:pos 
          effectiveRange:&range]; 
    NSTextAttachment *attachment 
     = [attributes objectForKey:NSAttachmentAttributeName]; 

    if (attachment && !attachtmp) { 
     /* Create a temporary directory to hold the attachments (we have to do this 
    because we can't get the full path from the NSFileWrapper) */ 
     do { 
    attachtmp = [tmpdir stringByAppendingPathComponent: 
        [NSString stringWithFormat:@"csmail-%08lx", 
        random()]]; 
     } while (![fileManager createDirectoryAtPath:attachtmp 
         withIntermediateDirectories:NO 
             attributes:nil 
              error:NULL]); 
    } 

    if (attachment) { 
     NSFileWrapper *fileWrapper = [attachment fileWrapper]; 
     NSString *filename = [attachtmp stringByAppendingPathComponent: 
       [fileWrapper preferredFilename]]; 
     NSURL *url = [NSURL fileURLWithPath:filename isDirectory:NO]; 

     [fileWrapper writeToURL:url 
         options:NSFileWrapperWritingAtomic 
      originalContentsURL:nil 
         error:NULL]; 

     [body appendFormat:@"<%@>\n", [fileWrapper preferredFilename]]; 

     [files insertDescriptor: 
    [NSAppleEventDescriptor descriptorWithString:filename] 
       atIndex:++numFiles]; 

     pos = range.location + range.length; 

     continue; 
    } 

    [body appendString:[[messageBody string] substringWithRange:range]]; 

    pos = range.location + range.length; 
    } 

    /* Replace any CF/LF pairs with just LF; also replace CRs with LFs */ 
    [body replaceOccurrencesOfString:@"\r\n" withString:@"\n" 
    options:NSLiteralSearch range:NSMakeRange (0, [body length])]; 
    [body replaceOccurrencesOfString:@"\r" withString:@"\n" 
    options:NSLiteralSearch range:NSMakeRange (0, [body length])]; 

    [event setParamDescriptor: 
    [NSAppleEventDescriptor descriptorWithString:handler] 
     forKeyword:keyASSubroutineName]; 
    [params insertDescriptor: 
    [NSAppleEventDescriptor descriptorWithString:sendr] 
      atIndex:1]; 
    [params insertDescriptor:recip atIndex:2]; 
    [params insertDescriptor: 
    [NSAppleEventDescriptor descriptorWithString:subj] 
      atIndex:3]; 
    [params insertDescriptor:ccrec atIndex:4]; 
    [params insertDescriptor:bccrec atIndex:5]; 
    [params insertDescriptor: 
    [NSAppleEventDescriptor descriptorWithString:body] 
      atIndex:6]; 
    [params insertDescriptor:files atIndex:7]; 

    [event setParamDescriptor:params forKeyword:keyDirectObject]; 

    if (![[self script] executeAppleEvent:event error:&errorInfo]) { 
    NSLog (@"Unable to communicate with Mail.app. Error was %@.\n", 
     errorInfo); 
    return NO; 
    } 

    return YES; 
} 

- (BOOL)deliverMessage:(NSAttributedString *)messageBody 
      headers:(NSDictionary *)messageHeaders 
{ 
    return [self doHandler:@"deliver_message" 
      forMessage:messageBody 
     headers:messageHeaders]; 
} 

- (BOOL)constructMessage:(NSAttributedString *)messageBody 
     headers:(NSDictionary *)messageHeaders 
{ 
    return [self doHandler:@"construct_message" 
      forMessage:messageBody 
     headers:messageHeaders]; 
} 

よう-constructMessage:headers:方法を、コードを使用して、スクリプト内の関数をトリガすることができます私たちのソフトウェアで

、我々は、適切なAppleScriptをコンパイルするには、以下の使用します-deliverMessage:headers:メソッドはを作成し、の電子メールを1ヒットで送信しますが、Mailには既にすべての情報が入力された新しい電子メールが開かれます。

0

これは、AppleScriptを使って簡単に行うことができます。「面白い」の部分は、AppleScriptをアプリケーションから呼び出す方法です。 http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/ScriptingBridgeConcepts/Introduction/Introduction.html

AppleScriptの素晴らしい点は、スクリプトエディタを使用して外部アプリケーションを制御してから、アプリケーションでコードを作成する方法を見つけることができることです。

10
NSString* subject = @"mail subject"; 
NSString* body = @"mail body"; 
NSString* to = @"[email protected]"; 

NSString *encodedSubject = [NSString stringWithFormat:@"SUBJECT=%@", [subject stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]; 
NSString *encodedBody = [NSString stringWithFormat:@"BODY=%@", [body stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]; 
NSString *encodedTo = [to stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; 
NSString *encodedURLString = [NSString stringWithFormat:@"mailto:%@?%@&%@", encodedTo, encodedSubject, encodedBody]; 
NSURL *mailtoURL = [NSURL URLWithString:encodedURLString]; 

[[NSWorkspace sharedWorkspace] openURL:mailtoURL]; 
+1

-1ファイルを添付しません。 –

関連する問題