Skip to content

Commit

Permalink
Rewrite prefpaneHelper to support properly macOS 13+
Browse files Browse the repository at this point in the history
  • Loading branch information
jozews321 authored Feb 18, 2024
1 parent 88955f7 commit 25a3b1c
Showing 1 changed file with 28 additions and 38 deletions.
66 changes: 28 additions & 38 deletions YogaSMCUtils/ScriptHelper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ let prefpaneAS = """
activate
end tell
"""

let sleepAS = "tell application \"System Events\" to sleep"

let searchAS = "tell application \"System Events\" to keystroke space using {command down, option down}"
Expand Down Expand Up @@ -60,43 +61,32 @@ func scriptHelper(_ source: String, _ name: String, _ image: NSString? = nil) ->
return nil
}

func prefpaneHelper(_ identifier: String = "YogaSMCPane") {
guard let prefpane: SystemPreferencesApplication = SBApplication(bundleIdentifier: "com.apple.systempreferences"),
let paneArray = prefpane.panes?() else { return }

guard paneArray.count != 0 else {
#if DEBUG
showOSD("AppleEventAccess")
#endif
if scriptHelper(prefpaneAS, "Prefpane") == nil {
let alert = NSAlert()
alert.messageText = "Failed to open Preferences"
alert.informativeText = "Please install YogaSMCPane"
alert.alertStyle = .warning
alert.addButton(withTitle: "OK")
alert.runModal()
}
return
}

var target: SystemPreferencesPane?
for object in paneArray {
if let pane = object as? SystemPreferencesPane,
pane.name == identifier {
target = pane
break
func prefpaneHelper() {

let homeDir = NSHomeDirectory()
let path = "\(homeDir)/Library/PreferencePanes/"
let path2 = "/Library/PreferencePanes/"
let url = NSURL(fileURLWithPath: path)
let url2 = NSURL(fileURLWithPath: path2)

if let pathComponent = url.appendingPathComponent("YogaSMCPane.prefPane"), let pathComponent2 = url2.appendingPathComponent("YogaSMCPane.prefPane") {
let filePath = pathComponent.path
let filePath2 = pathComponent2.path
let fileManager = FileManager.default

if fileManager.fileExists(atPath: filePath ) || fileManager.fileExists(atPath: filePath2 ) {
NSWorkspace.shared.open(URL(string: "x-apple.systempreferences:com.apple.preference")!)
NSWorkspace.shared.open(URL(fileURLWithPath: "\(homeDir)/Library/PreferencePanes/YogaSMCPane.prefPane"))
NSWorkspace.shared.open(URL(fileURLWithPath: "/Library/PreferencePanes/YogaSMCPane.prefPane"))
return
} else {
let alert = NSAlert()
alert.messageText = "Failed to open Preferences/Settings"
alert.informativeText = "Please install YogaSMCPane"
alert.alertStyle = .warning
alert.addButton(withTitle: "OK")
alert.runModal()
}
}
}

if target == nil {
let alert = NSAlert()
alert.messageText = "Failed to open Preferences"
alert.informativeText = "Please install YogaSMCPane"
alert.alertStyle = .warning
alert.addButton(withTitle: "OK")
alert.runModal()
return
}
_ = target?.reveal?()
prefpane.activate()

}

0 comments on commit 25a3b1c

Please sign in to comment.