SKAttachmentEmailer.m 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. //
  2. // SKAttachmentEmailer.m
  3. // Skim
  4. //
  5. // Created by Christiaan Hofman on 11/4/12.
  6. /*
  7. This software is Copyright (c) 2012-2018
  8. Christiaan Hofman. All rights reserved.
  9. Redistribution and use in source and binary forms, with or without
  10. modification, are permitted provided that the following conditions
  11. are met:
  12. - Redistributions of source code must retain the above copyright
  13. notice, this list of conditions and the following disclaimer.
  14. - Redistributions in binary form must reproduce the above copyright
  15. notice, this list of conditions and the following disclaimer in
  16. the documentation and/or other materials provided with the
  17. distribution.
  18. - Neither the name of Christiaan Hofman nor the names of any
  19. contributors may be used to endorse or promote products derived
  20. from this software without specific prior written permission.
  21. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  22. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  23. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  24. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  25. OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  26. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  27. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  28. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  29. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  30. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  31. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  32. */
  33. #import "SKAttachmentEmailer.h"
  34. //#import "NSString_SKExtensions.h"
  35. #import "NSObject+OCExtensions.h"
  36. //#import "NSFileManager_SKExtensions.h"
  37. @implementation SKAttachmentEmailer
  38. @synthesize fileURL, subject;
  39. + (id)attachmentEmailerWithFileURL:(NSURL *)aURL subject:(NSString *)aSubject waitingForTask:(NSTask *)task {
  40. id attachmentEmailer = [[self alloc] init];
  41. [attachmentEmailer setFileURL:aURL];
  42. [attachmentEmailer setSubject:aSubject];
  43. [attachmentEmailer waitForTaskTermination:task];
  44. return attachmentEmailer;
  45. }
  46. - (void)dealloc {
  47. // SKDESTROY(fileURL);
  48. // SKDESTROY(subject);
  49. // [super dealloc];
  50. }
  51. - (void)emailAttachmentFile {
  52. NSString *scriptFormat = nil;
  53. NSString *mailAppID = (NSString *)CFBridgingRelease(LSCopyDefaultHandlerForURLScheme(CFSTR("mailto")));
  54. if ([@"com.microsoft.entourage" isCaseInsensitiveEqual:mailAppID]) {
  55. scriptFormat = @"tell application \"Microsoft Entourage\"\n"
  56. @"activate\n"
  57. @"set m to make new draft window with properties {subject:\"%@\", visible:true}\n"
  58. @"tell m\n"
  59. @"make new attachment with properties {file:POSIX file \"%@\"}\n"
  60. @"end tell\n"
  61. @"end tell\n";
  62. } else if ([@"com.microsoft.outlook" isCaseInsensitiveEqual:mailAppID]) {
  63. scriptFormat = @"tell application \"Microsoft Outlook\"\n"
  64. @"activate\n"
  65. @"set m to make new draft window with properties {subject:\"%@\", visible:true}\n"
  66. @"tell m\n"
  67. @"make new attachment with properties {file:POSIX file \"%@\"}\n"
  68. @"end tell\n"
  69. @"end tell\n";
  70. } else if ([@"com.barebones.mailsmith" isCaseInsensitiveEqual:mailAppID]) {
  71. scriptFormat = @"tell application \"Mailsmith\"\n"
  72. @"activate\n"
  73. @"set m to make new message window with properties {subject:\"%@\", visible:true}\n"
  74. @"tell m\n"
  75. @"make new enclosure with properties {file:POSIX file \"%@\"}\n"
  76. @"end tell\n"
  77. @"end tell\n";
  78. } else if ([@"com.mailplaneapp.Mailplane" isCaseInsensitiveEqual:mailAppID]) {
  79. scriptFormat = @"tell application \"Mailplane\"\n"
  80. @"activate\n"
  81. @"set m to make new outgoing message with properties {subject:\"%@\", visible:true}\n"
  82. @"tell m\n"
  83. @"make new mail attachment with properties {path:\"%@\"}\n"
  84. @"end tell\n"
  85. @"end tell\n";
  86. } else if ([@"com.postbox-inc.postboxexpress" isCaseInsensitiveEqual:mailAppID]) {
  87. scriptFormat = @"tell application \"PostboxExpress\"\n"
  88. @"activate\n"
  89. @"send message subject \"%@\" attachment \"%@\"\n"
  90. @"end tell\n";
  91. } else if ([@"com.postbox-inc.postbox" isCaseInsensitiveEqual:mailAppID]) {
  92. scriptFormat = @"tell application \"Postbox\"\n"
  93. @"activate\n"
  94. @"send message subject \"%@\" attachment \"%@\"\n"
  95. @"end tell\n";
  96. } else {
  97. scriptFormat = @"tell application \"Mail\"\n"
  98. @"activate\n"
  99. @"set m to make new outgoing message with properties {subject:\"%@\", visible:true}\n"
  100. @"tell content of m\n"
  101. @"make new attachment at after last character with properties {file name:\"%@\"}\n"
  102. @"end tell\n"
  103. @"end tell\n";
  104. }
  105. NSString *scriptString = [NSString stringWithFormat:scriptFormat, subject, [fileURL path]];
  106. NSAppleScript *script = [[NSAppleScript alloc] initWithSource:scriptString];
  107. NSDictionary *errorDict = nil;
  108. if ([script compileAndReturnError:&errorDict] == NO)
  109. NSLog(@"Error compiling mail to script: %@", errorDict);
  110. else if ([script executeAndReturnError:&errorDict] == NO)
  111. NSLog(@"Error running mail to script: %@", errorDict);
  112. }
  113. - (void)taskFinished:(NSNotification *)notification {
  114. if ([fileURL checkResourceIsReachableAndReturnError:NULL] && [[notification object] terminationStatus] == 0)
  115. [self emailAttachmentFile];
  116. else
  117. NSBeep();
  118. [[NSNotificationCenter defaultCenter] removeObserver:self];
  119. }
  120. - (void)waitForTaskTermination:(NSTask *)task {
  121. // [self retain];
  122. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(taskFinished:) name:NSTaskDidTerminateNotification object:task];
  123. }
  124. - (void)taskFailed {
  125. [[NSNotificationCenter defaultCenter] removeObserver:self];
  126. // [self autorelease];
  127. }
  128. @end