KMMailHelper.swift 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. //
  2. // KMMailHelper.swift
  3. // Cisdem PDFMaster
  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. //
  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. let ws = NSWorkspace.shared
  43. let service = NSSharingService(named: .composeEmail)
  44. service?.subject = totalFileString
  45. var urls: [URL] = []
  46. for file in paths {
  47. urls.append(.init(fileURLWithPath: file))
  48. }
  49. service?.perform(withItems: urls)
  50. }
  51. @objc static func newEmail(withContacts contact: String, andSubjects subjects: String) {
  52. let emailString = """
  53. tell application "Mail"
  54. set newMessage to make new outgoing message with properties {subject:"\(subjects)" & return}
  55. tell newMessage
  56. set visible to true
  57. make new to recipient at end of to recipients with properties {address:"\(contact)"}
  58. tell content
  59. """
  60. // let errorDictionary: AutoreleasingUnsafeMutablePointer<NSDictionary?>? = nil
  61. // let emailScript = NSAppleScript(source: emailString + "end tell\nactivate\nend tell\nend tell")
  62. // let eventDescriptor = emailScript?.executeAndReturnError(errorDictionary)
  63. //
  64. // if eventDescriptor == nil {
  65. // if let url = URL(string: "https://www.pdfreaderpro.com/contact"), !NSWorkspace.shared.open(url) {
  66. // NSLog("Failed to open url: \(url)")
  67. // }
  68. // }
  69. let workspace = NSWorkspace.shared
  70. let service = NSSharingService(named: NSSharingService.Name.composeEmail)
  71. service?.recipients = [contact]
  72. service?.subject = subjects//emailString
  73. // service?.messageBody = emailString
  74. service?.perform(withItems: [""])
  75. }
  76. }