From e9c69f646172eb31fc202e51f5de361d1cc4a933 Mon Sep 17 00:00:00 2001 From: Serg Date: Sat, 12 May 2018 11:11:53 +0700 Subject: [PATCH] experimental: run applescript on separate serial queue --- MTMR/AppleScriptTouchBarItem.swift | 11 +++++++++-- MTMR/TouchBarController.swift | 10 ++++++---- 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/MTMR/AppleScriptTouchBarItem.swift b/MTMR/AppleScriptTouchBarItem.swift index 9659a9b..8b23a6a 100644 --- a/MTMR/AppleScriptTouchBarItem.swift +++ b/MTMR/AppleScriptTouchBarItem.swift @@ -15,7 +15,7 @@ class AppleScriptTouchBarItem: CustomButtonTouchBarItem { } self.script = script button.bezelColor = .clear - DispatchQueue.main.async { + DispatchQueue.appleScriptQueue.async { var error: NSDictionary? guard script.compileAndReturnError(&error) else { #if DEBUG @@ -42,8 +42,11 @@ class AppleScriptTouchBarItem: CustomButtonTouchBarItem { DispatchQueue.main.async { self.title = scriptResult self.forceHideConstraint.isActive = scriptResult == "" + #if DEBUG + print("did set new script result title \(scriptResult)") + #endif } - DispatchQueue.main.asyncAfter(deadline: .now() + self.interval) { [weak self] in + DispatchQueue.appleScriptQueue.asyncAfter(deadline: .now() + self.interval) { [weak self] in self?.refreshAndSchedule() } } @@ -66,3 +69,7 @@ extension SourceProtocol { return NSAppleScript(source: source) } } + +extension DispatchQueue { + static let appleScriptQueue = DispatchQueue(label: "mtmr.applescript") +} diff --git a/MTMR/TouchBarController.swift b/MTMR/TouchBarController.swift index a396add..fb6ec07 100644 --- a/MTMR/TouchBarController.swift +++ b/MTMR/TouchBarController.swift @@ -262,10 +262,12 @@ class TouchBarController: NSObject, NSTouchBarDelegate { return {} } return { - var error: NSDictionary? - appleScript.executeAndReturnError(&error) - if let error = error { - print("error \(error) when handling \(item) ") + DispatchQueue.appleScriptQueue.async { + var error: NSDictionary? + appleScript.executeAndReturnError(&error) + if let error = error { + print("error \(error) when handling \(item) ") + } } } case .shellScript(executable: let executable, parameters: let parameters):