KMMailHelper.swift 2.6 KB

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