macsync/@packages/shared/Sources/MacSyncShared/Util/AppleScriptEscape.swift

20 lines
1 KiB
Swift

import Foundation
/// String escaping for values interpolated into AppleScript source. Every
/// module that sends via AppleScript (iMessage, iMail, future iNote /
/// iReminder fallbacks) previously re-implemented the same backslash + quote
/// replacement. Extracted once so a missed escape can be fixed in one place.
public enum AppleScriptEscape {
/// Escape a value for embedding inside a double-quoted AppleScript
/// literal: `"\(escaped)"`. Handles backslashes and double quotes.
/// Newlines in the source are preserved as the literal characters
/// `\\n` because AppleScript treats raw newlines inside `""` literals
/// as a syntax error when they originate from interpolation.
public static func quote(_ value: String) -> String {
value
.replacingOccurrences(of: "\\", with: "\\\\")
.replacingOccurrences(of: "\"", with: "\\\"")
.replacingOccurrences(of: "\n", with: "\\n")
.replacingOccurrences(of: "\r", with: "\\r")
}
}