forked from raycast/script-commands
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdictionary-lookup.swift
executable file
·42 lines (32 loc) · 1.17 KB
/
dictionary-lookup.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#!/usr/bin/swift
// Required parameters:
// @raycast.schemaVersion 1
// @raycast.title Dictionary Lookup
// @raycast.mode compact
// Optional parameters:
// @raycast.icon 📖
// @raycast.packageName Writing
// @raycast.argument1 { "type": "text", "placeholder": "word or phrase" }
// Documentation:
// @raycast.description Directly use macOS Dictionary
// @raycast.author Alessandra Pereyra
// @raycast.authorURL https://github.com/alessandrapereyra
import Cocoa
import CoreServices.DictionaryServices
func translate(_ text: String) -> String? {
let nsstring = text as NSString
let cfrange = CFRange(location: 0, length: nsstring.length)
guard let definition = DCSCopyTextDefinition(nil, nsstring, cfrange) else {
return nil
}
var foundDefinitions = String(definition.takeRetainedValue()).components(separatedBy: "\n")
if foundDefinitions.count > 1 {
foundDefinitions.removeFirst()
foundDefinitions.removeFirst()
foundDefinitions.removeLast()
}
return foundDefinitions.joined(separator: " • ")
}
let text = CommandLine.arguments[1]
let definition = translate(text) ?? "No definition found for \"\(text)\""
print(definition)