KMMailHelper.m 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. //
  2. // KMMailHelper.m
  3. // PDF Reader
  4. //
  5. // Created by HuFeng on 13-3-13.
  6. // Copyright (c) 2013年 zhangjie. All rights reserved.
  7. //
  8. #import "KMMailHelper.h"
  9. @implementation KMMailHelper
  10. + (void)sendFileWithPaths:(NSArray *)paths {
  11. NSString *totalFileString = nil;
  12. for (int i = 0; i < [paths count]; i++) {
  13. NSString* filePath = [[paths objectAtIndex:i] lastPathComponent];
  14. if (i == 0) {
  15. totalFileString = [NSString stringWithFormat:@"%@",filePath];
  16. }else{
  17. totalFileString = [totalFileString stringByAppendingFormat:@" %@",filePath];
  18. }
  19. }
  20. NSString *emailString = [NSString stringWithFormat:@"\
  21. tell application \"Mail\"\n\
  22. set newMessage to make new outgoing message with properties {subject:\"%@\" & return} \n\
  23. tell newMessage\n\
  24. set visible to true\n\
  25. tell content\n\
  26. ",totalFileString];
  27. //add attachments to script
  28. for (NSString *file in paths) {
  29. emailString = [emailString stringByAppendingFormat:@"make new attachment with properties {file name:\"%@\"} at before the first paragraph\n", file];
  30. }
  31. NSDictionary *errorDictionary;
  32. emailString = [emailString stringByAppendingFormat:@"end tell\n activate\n end tell\n end tell"];
  33. NSAppleScript *emailScript = [[NSAppleScript alloc] initWithSource:emailString];
  34. NSAppleEventDescriptor *eventDescriptor = [emailScript executeAndReturnError:&errorDictionary];
  35. if (!eventDescriptor)
  36. {
  37. NSURL *url = [NSURL URLWithString:@"https://www.pdfreaderpro.com/contact"];
  38. if(![[NSWorkspace sharedWorkspace] openURL:url])
  39. NSLog(@"Failed to open url: %@",[url description]);
  40. }
  41. }
  42. + (void)newEmailWithContacts:(NSString*)contact andSubjects:(NSString*)subjects
  43. {
  44. NSString *emailString = [NSString stringWithFormat:@"\
  45. tell application \"Mail\"\n\
  46. set newMessage to make new outgoing message with properties {subject:\"%@\" & return} \n\
  47. tell newMessage\n\
  48. set visible to true\n\
  49. make new to recipient at end of to recipients with properties {address:\"%@\"}\n\
  50. tell content\n\
  51. ",subjects,contact];
  52. NSDictionary *errorDictionary;
  53. emailString = [emailString stringByAppendingFormat:@"end tell\n activate\n end tell\n end tell"];
  54. NSAppleScript *emailScript = [[NSAppleScript alloc] initWithSource:emailString];
  55. NSAppleEventDescriptor *eventDescriptor = [emailScript executeAndReturnError:&errorDictionary];
  56. if (!eventDescriptor)
  57. {
  58. NSURL *url = [NSURL URLWithString:@"https://www.pdfreaderpro.com/contact"];
  59. if(![[NSWorkspace sharedWorkspace] openURL:url])
  60. NSLog(@"Failed to open url: %@",[url description]);
  61. }
  62. }
  63. @end