1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586 |
- //
- // KMMailHelper.swift
- // PDF Reader Pro
- //
- // Created by wanjun on 2023/10/7.
- //
- import Cocoa
- @objcMembers class KMMailHelper: NSObject {
- static func sendFile(withPaths paths: [String]) {
- var totalFileString: String?
- for (index, path) in paths.enumerated() {
- let filePath = URL(fileURLWithPath: path).lastPathComponent
- if index == 0 {
- totalFileString = filePath
- } else {
- totalFileString?.append(" \(filePath)")
- }
- }
-
- guard let totalFileString = totalFileString else {
- return
- }
-
- let emailString = """
- tell application "Mail"
- set newMessage to make new outgoing message with properties {subject:"\(totalFileString)" & return}
- tell newMessage
- set visible to true
- tell content
- """
-
- var attachmentScript = ""
- for file in paths {
- attachmentScript.append("make new attachment with properties {file name:\"\(file)\"} at before the first paragraph\n")
- }
-
- // let errorDictionary: AutoreleasingUnsafeMutablePointer<NSDictionary?>? = nil
- // let emailScript = NSAppleScript(source: emailString + attachmentScript + "end tell\nactivate\nend tell\nend tell")
- // let eventDescriptor = emailScript?.executeAndReturnError(errorDictionary)
- //
- // if eventDescriptor == nil {
- // if let url = URL(string: "https://www.pdfreaderpro.com/contact"), !NSWorkspace.shared.open(url) {
- // NSLog("Failed to open url: \(url)")
- // }
- // }
- let ws = NSWorkspace.shared
- let service = NSSharingService(named: .composeEmail)
- service?.subject = totalFileString
- var urls: [URL] = []
- for file in paths {
- urls.append(.init(fileURLWithPath: file))
- }
- service?.perform(withItems: urls)
- }
-
- @objc static func newEmail(withContacts contact: String, andSubjects subjects: String) {
- let emailString = """
- tell application "Mail"
- set newMessage to make new outgoing message with properties {subject:"\(subjects)" & return}
- tell newMessage
- set visible to true
- make new to recipient at end of to recipients with properties {address:"\(contact)"}
- tell content
- """
- // let errorDictionary: AutoreleasingUnsafeMutablePointer<NSDictionary?>? = nil
- // let emailScript = NSAppleScript(source: emailString + "end tell\nactivate\nend tell\nend tell")
- // let eventDescriptor = emailScript?.executeAndReturnError(errorDictionary)
- //
- // if eventDescriptor == nil {
- // if let url = URL(string: "https://www.pdfreaderpro.com/contact"), !NSWorkspace.shared.open(url) {
- // NSLog("Failed to open url: \(url)")
- // }
- // }
- let workspace = NSWorkspace.shared
- let service = NSSharingService(named: NSSharingService.Name.composeEmail)
-
- service?.recipients = [contact]
- service?.subject = subjects//emailString
- // service?.messageBody = emailString
-
- service?.perform(withItems: [""])
- }
- }
|