KMMailHelper.swift 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. //
  2. // KMMailHelper.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by wanjun on 2023/10/7.
  6. //
  7. import Cocoa
  8. @objcMembers class KMMailHelper: NSObject {
  9. static func sendFile(withPaths paths: [String]) {
  10. var totalFileString: String?
  11. for (index, path) in paths.enumerated() {
  12. let filePath = URL(fileURLWithPath: path).lastPathComponent
  13. if index == 0 {
  14. totalFileString = filePath
  15. } else {
  16. totalFileString?.append(" \(filePath)")
  17. }
  18. }
  19. guard let totalFileString = totalFileString else {
  20. return
  21. }
  22. let emailString = """
  23. tell application "Mail"
  24. set newMessage to make new outgoing message with properties {subject:"\(totalFileString)" & return}
  25. tell newMessage
  26. set visible to true
  27. tell content
  28. """
  29. var attachmentScript = ""
  30. for file in paths {
  31. attachmentScript.append("make new attachment with properties {file name:\"\(file)\"} at before the first paragraph\n")
  32. }
  33. let errorDictionary: AutoreleasingUnsafeMutablePointer<NSDictionary?>? = nil
  34. let emailScript = NSAppleScript(source: emailString + attachmentScript + "end tell\nactivate\nend tell\nend tell")
  35. let eventDescriptor = emailScript?.executeAndReturnError(errorDictionary)
  36. if eventDescriptor == nil {
  37. if let url = URL(string: "https://www.pdfreaderpro.com/contact"), !NSWorkspace.shared.open(url) {
  38. NSLog("Failed to open url: \(url)")
  39. }
  40. }
  41. }
  42. @objc static func newEmail(withContacts contact: String, andSubjects subjects: String) {
  43. let emailString = """
  44. tell application "Mail"
  45. set newMessage to make new outgoing message with properties {subject:"\(subjects)" & return}
  46. tell newMessage
  47. set visible to true
  48. make new to recipient at end of to recipients with properties {address:"\(contact)"}
  49. tell content
  50. """
  51. // let errorDictionary: AutoreleasingUnsafeMutablePointer<NSDictionary?>? = nil
  52. // let emailScript = NSAppleScript(source: emailString + "end tell\nactivate\nend tell\nend tell")
  53. // let eventDescriptor = emailScript?.executeAndReturnError(errorDictionary)
  54. //
  55. // if eventDescriptor == nil {
  56. // if let url = URL(string: "https://www.pdfreaderpro.com/contact"), !NSWorkspace.shared.open(url) {
  57. // NSLog("Failed to open url: \(url)")
  58. // }
  59. // }
  60. let workspace = NSWorkspace.shared
  61. let service = NSSharingService(named: NSSharingService.Name.composeEmail)
  62. service?.recipients = [contact]
  63. service?.subject = subjects//emailString
  64. // service?.messageBody = emailString
  65. service?.perform(withItems: [""])
  66. }
  67. }