KMMailHelper.swift 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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. if eventDescriptor == nil {
  55. if let url = URL(string: "https://www.pdfreaderpro.com/contact"), !NSWorkspace.shared.open(url) {
  56. NSLog("Failed to open url: \(url)")
  57. }
  58. }
  59. }
  60. }