Skip to content

Commit 71e3297

Browse files
committed
Improve AppleScript errors
1 parent bbf3bb4 commit 71e3297

File tree

2 files changed

+39
-29
lines changed

2 files changed

+39
-29
lines changed

CotEditor/Sources/Scripting Support/TextSelection.swift

+32-24
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ private enum OSAUnicodeNormalizationType: FourCharCode {
106106

107107
// MARK: AppleScript Accessors
108108

109-
/// String of the selection (Unicode text).
109+
/// The string of the selection (Unicode text).
110110
@objc var contents: Any? {
111111

112112
get {
@@ -135,7 +135,7 @@ private enum OSAUnicodeNormalizationType: FourCharCode {
135135
}
136136

137137

138-
/// Character range (location and length) of the selection.
138+
/// The character range (location and length) of the selection.
139139
@objc var range: [Int]? {
140140

141141
get {
@@ -163,7 +163,7 @@ private enum OSAUnicodeNormalizationType: FourCharCode {
163163
}
164164

165165

166-
/// Line range (location and length) of the selection (list type).
166+
/// The line range (location and length) of the selection (list type).
167167
@objc var lineRange: [Int]? {
168168

169169
get {
@@ -197,7 +197,7 @@ private enum OSAUnicodeNormalizationType: FourCharCode {
197197

198198
// MARK: AppleScript Handlers
199199

200-
/// Shift the selection to right.
200+
/// Shifts the selection to right.
201201
@objc func handleShiftRight(_ command: NSScriptCommand) {
202202

203203
self.textView?.shiftRight(command)
@@ -265,17 +265,19 @@ private enum OSAUnicodeNormalizationType: FourCharCode {
265265

266266
guard
267267
let argument = command.evaluatedArguments?["caseType"] as? UInt32,
268-
let type = OSACaseType(rawValue: argument),
269-
let textView = self.textView
270-
else { return }
268+
let type = OSACaseType(rawValue: argument)
269+
else {
270+
command.scriptErrorNumber = OSAParameterMismatch
271+
return
272+
}
271273

272274
switch type {
273275
case .lowercase:
274-
textView.lowercaseWord(command)
276+
self.textView?.lowercaseWord(command)
275277
case .uppercase:
276-
textView.uppercaseWord(command)
278+
self.textView?.uppercaseWord(command)
277279
case .capitalized:
278-
textView.capitalizeWord(command)
280+
self.textView?.capitalizeWord(command)
279281
}
280282
}
281283

@@ -285,15 +287,17 @@ private enum OSAUnicodeNormalizationType: FourCharCode {
285287

286288
guard
287289
let argument = command.evaluatedArguments?["widthType"] as? UInt32,
288-
let type = OSAWidthType(rawValue: argument),
289-
let textView = self.textView
290-
else { return }
290+
let type = OSAWidthType(rawValue: argument)
291+
else {
292+
command.scriptErrorNumber = OSAParameterMismatch
293+
return
294+
}
291295

292296
switch type {
293297
case .half:
294-
textView.exchangeHalfwidthRoman(command)
298+
self.textView?.exchangeHalfwidthRoman(command)
295299
case .full:
296-
textView.exchangeFullwidthRoman(command)
300+
self.textView?.exchangeFullwidthRoman(command)
297301
}
298302
}
299303

@@ -303,15 +307,17 @@ private enum OSAUnicodeNormalizationType: FourCharCode {
303307

304308
guard
305309
let argument = command.evaluatedArguments?["kanaType"] as? UInt32,
306-
let type = OSAKanaType(rawValue: argument),
307-
let textView = self.textView
308-
else { return }
310+
let type = OSAKanaType(rawValue: argument)
311+
else {
312+
command.scriptErrorNumber = OSAParameterMismatch
313+
return
314+
}
309315

310316
switch type {
311317
case .hiragana:
312-
textView.exchangeHiragana(command)
318+
self.textView?.exchangeHiragana(command)
313319
case .katakana:
314-
textView.exchangeKatakana(command)
320+
self.textView?.exchangeKatakana(command)
315321
}
316322
}
317323

@@ -342,11 +348,13 @@ private enum OSAUnicodeNormalizationType: FourCharCode {
342348

343349
guard
344350
let argument = command.evaluatedArguments?["unfType"] as? UInt32,
345-
let type = OSAUnicodeNormalizationType(rawValue: argument),
346-
let textView = self.textView
347-
else { return }
351+
let type = OSAUnicodeNormalizationType(rawValue: argument)
352+
else {
353+
command.scriptErrorNumber = OSAParameterMismatch
354+
return
355+
}
348356

349-
textView.normalizeUnicode(form: type.form)
357+
self.textView?.normalizeUnicode(form: type.form)
350358
}
351359

352360

CotEditor/Sources/Scripting Support/WriteToConsoleCommand.swift

+7-5
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
//
99
// ---------------------------------------------------------------------------
1010
//
11-
// © 2017-2024 1024jp
11+
// © 2017-2025 1024jp
1212
//
1313
// Licensed under the Apache License, Version 2.0 (the "License");
1414
// you may not use this file except in compliance with the License.
@@ -29,11 +29,13 @@ final class WriteToConsoleCommand: NSScriptCommand {
2929

3030
override func performDefaultImplementation() -> Any? {
3131

32-
guard
33-
let message = self.directParameter as? String,
34-
let arguments = self.evaluatedArguments
35-
else { return false }
32+
guard let message = self.directParameter as? String else {
33+
self.scriptErrorNumber = OSAMissingParameter
34+
self.scriptErrorOffendingObjectDescriptor = NSAppleEventDescriptor(string: "message")
35+
return false
36+
}
3637

38+
let arguments = self.evaluatedArguments ?? [:]
3739
let title = (arguments["title"] as? Bool) ?? true
3840
let timestamp = (arguments["timestamp"] as? Bool) ?? true
3941

0 commit comments

Comments
 (0)