Skip to content

Commit dbb2f16

Browse files
authored
Implemented changable icons for AppleScriptTouchBarItem (#275)
AppleScriptTouchBarItem now allow to specify any number of icons which can be changed from the script. You cannot change icon from touch event. To change icon, you need to return array from your script with 2 values - title and icn name. More info in readme
1 parent 3864591 commit dbb2f16

File tree

4 files changed

+59
-6
lines changed

4 files changed

+59
-6
lines changed

MTMR/AppleScriptTouchBarItem.swift

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@ class AppleScriptTouchBarItem: CustomButtonTouchBarItem {
44
private var script: NSAppleScript!
55
private let interval: TimeInterval
66
private var forceHideConstraint: NSLayoutConstraint!
7+
private let alternativeImages: [String: SourceProtocol]
78

8-
init?(identifier: NSTouchBarItem.Identifier, source: SourceProtocol, interval: TimeInterval) {
9+
init?(identifier: NSTouchBarItem.Identifier, source: SourceProtocol, interval: TimeInterval, alternativeImages: [String: SourceProtocol]) {
910
self.interval = interval
11+
self.alternativeImages = alternativeImages
1012
super.init(identifier: identifier, title: "")
1113
forceHideConstraint = view.widthAnchor.constraint(equalToConstant: 0)
1214
title = "scheduled"
@@ -57,13 +59,35 @@ class AppleScriptTouchBarItem: CustomButtonTouchBarItem {
5759
}
5860
}
5961

62+
func updateIcon(iconLabel: String) {
63+
if alternativeImages[iconLabel] != nil {
64+
DispatchQueue.main.async {
65+
self.image = self.alternativeImages[iconLabel]!.image
66+
}
67+
} else {
68+
print("Cannot find icon with label \"\(iconLabel)\"")
69+
}
70+
}
71+
6072
func execute() -> String {
6173
var error: NSDictionary?
6274
let output = script.executeAndReturnError(&error)
6375
if let error = error {
6476
print(error)
6577
return "error"
6678
}
79+
if output.descriptorType == typeAEList {
80+
let arr = Array(1...output.numberOfItems).compactMap({ output.atIndex($0)!.stringValue ?? "" })
81+
82+
if arr.count <= 0 {
83+
return ""
84+
} else if arr.count == 1 {
85+
return arr[0]
86+
} else {
87+
updateIcon(iconLabel: arr[1])
88+
return arr[0]
89+
}
90+
}
6791
return output.stringValue ?? ""
6892
}
6993
}

MTMR/ItemsParsing.swift

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ class SupportedTypesHolder {
208208

209209
enum ItemType: Decodable {
210210
case staticButton(title: String)
211-
case appleScriptTitledButton(source: SourceProtocol, refreshInterval: Double)
211+
case appleScriptTitledButton(source: SourceProtocol, refreshInterval: Double, alternativeImages: [String: SourceProtocol])
212212
case shellScriptTitledButton(source: SourceProtocol, refreshInterval: Double)
213213
case timeButton(formatTemplate: String, timeZone: String?, locale: String?)
214214
case battery
@@ -251,6 +251,7 @@ enum ItemType: Decodable {
251251
case autoResize
252252
case filter
253253
case disableMarquee
254+
case alternativeImages
254255
}
255256

256257
enum ItemTypeRaw: String, Decodable {
@@ -282,7 +283,8 @@ enum ItemType: Decodable {
282283
case .appleScriptTitledButton:
283284
let source = try container.decode(Source.self, forKey: .source)
284285
let interval = try container.decodeIfPresent(Double.self, forKey: .refreshInterval) ?? 1800.0
285-
self = .appleScriptTitledButton(source: source, refreshInterval: interval)
286+
let alternativeImages = try container.decodeIfPresent([String: Source].self, forKey: .alternativeImages) ?? [:]
287+
self = .appleScriptTitledButton(source: source, refreshInterval: interval, alternativeImages: alternativeImages)
286288

287289
case .shellScriptTitledButton:
288290
let source = try container.decode(Source.self, forKey: .source)

MTMR/TouchBarController.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -240,8 +240,8 @@ class TouchBarController: NSObject, NSTouchBarDelegate {
240240
switch item.type {
241241
case let .staticButton(title: title):
242242
barItem = CustomButtonTouchBarItem(identifier: identifier, title: title)
243-
case let .appleScriptTitledButton(source: source, refreshInterval: interval):
244-
barItem = AppleScriptTouchBarItem(identifier: identifier, source: source, interval: interval)
243+
case let .appleScriptTitledButton(source: source, refreshInterval: interval, alternativeImages: alternativeImages):
244+
barItem = AppleScriptTouchBarItem(identifier: identifier, source: source, interval: interval, alternativeImages: alternativeImages)
245245
case let .shellScriptTitledButton(source: source, refreshInterval: interval):
246246
barItem = ShellScriptTouchBarItem(identifier: identifier, source: source, interval: interval)
247247
case let .timeButton(formatTemplate: template, timeZone: timeZone, locale: locale):

README.md

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,10 +133,37 @@ The pre-installed configuration contains less or more than you'll probably want,
133133
"inline": "tell application \"Finder\"\rif not (exists window 1) then\rmake new Finder window\rset target of front window to path to home folder as string\rend if\ractivate\rend tell",
134134
// or
135135
"base64": "StringInbase64"
136-
}
136+
},
137137
}
138138
```
139139

140+
> Note: appleScriptTitledButton can change its icon. To do it, you need to do the following things:
141+
1. Declarate dictionary of icons in `alternativeImages` field
142+
2. Make you script return array of two values - `{"TITLE", "IMAGE_LABEL"}`
143+
3. Make sure that your `IMAGE_LABEL` is declared in `alternativeImages` field
144+
145+
Example:
146+
```js
147+
{
148+
"type": "appleScriptTitledButton",
149+
"source": {
150+
"inline": "if (random number from 1 to 2) = 1 then\n\tset val to {\"title\", \"play\"}\nelse\n\tset val to {\"title\", \"pause\"}\nend if\nreturn val"
151+
},
152+
"refreshInterval": 1,
153+
"image": {
154+
"base64": "iVBORw0KGgoAAAANSUhEUgA..."
155+
},
156+
"alternativeImages": {
157+
"play": {
158+
"base64": "iVBORw0KGgoAAAANSUhEUgAAAAAA..."
159+
},
160+
"pause": {
161+
"base64": "iVBORw0KGgoAAAANSUhEUgAAAIAA..."
162+
}
163+
}
164+
},
165+
```
166+
140167
#### `shellScriptTitledButton`
141168
> Note: script may return also colors using escape sequences (read more here https://misc.flogisoft.com/bash/tip_colors_and_formatting)
142169
> Only "16 Colors" mode supported atm. If background color returned, button will pick it up as own background color.

0 commit comments

Comments
 (0)