Skip to content

Instantly share code, notes, and snippets.

@TramPamPam
Created February 11, 2021 11:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save TramPamPam/49b14caa554e91bbf9224546387eeccf to your computer and use it in GitHub Desktop.
Save TramPamPam/49b14caa554e91bbf9224546387eeccf to your computer and use it in GitHub Desktop.
AppleScript to switch clock appearance
set currentValue to ¬
(do shell script ¬
"defaults read com.apple.menuextra.clock 'IsAnalog'") ¬
as integer as boolean
do shell script ¬
"defaults write com.apple.menuextra.clock 'IsAnalog' -bool " & (not currentValue) & ";"
@TramPamPam
Copy link
Author

import Cocoa
import SwiftUI

@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {

    var statusBarItem: NSStatusItem!
    
    func applicationDidFinishLaunching(_ aNotification: Notification) {
        // Create the SwiftUI view that provides the window contents.
        let contentView = ContentView()

        // Create the status item
        self.statusBarItem = NSStatusBar.system.statusItem(withLength: CGFloat(NSStatusItem.variableLength))
        
        if let button = self.statusBarItem.button {
            button.image = NSImage(named: "switch")
            button.action = #selector(runSwitchClockScript)
        }
        
        NSApp.activate(ignoringOtherApps: true)
    }

    @objc func runSwitchClockScript() {
        if let dict = UserDefaults.standard.persistentDomain(forName: "com.apple.menuextra.clock") {

            print(dict)

        } else {
            print("NOPE")
        }

// *  For example: `shellOut(to: "mkdir", arguments: ["NewFolder"], at: "~/CurrentFolder")`
//        do {
////            let res = try shellOut(to: "defaults read com.apple.menuextra.clock 'IsAnalog'")
//            let res = try shellOut(to: "defaults read", arguments: ["com.apple.menuextra.clock", "\'IsAnalog\'"])
//            print("res \(res)")
//        } catch {
//            print(error)
//            return
//        }

        var error: NSDictionary?
        let source =
            """
        set currentValue to ¬
            (do shell script ¬
                "defaults read com.apple.menuextra.clock 'IsAnalog'") ¬
                as integer as boolean

        do shell script ¬
            "defaults write com.apple.menuextra.clock 'IsAnalog' -bool " & (not currentValue) & ";"

        """
        let source2 = "defaults read com.apple.systemuiserver menuExtras"
        let process = Process()
        process.launchPath = "/usr/bin/osascript"
        process.arguments = ["-e", source]

        if #available(macOS 10.13, *) {
            do {
                try process.run()
            } catch {
                print(error)
            }

        } else {
            process.launch()
        }
        process.waitUntilExit()

        return
        guard let URL = Bundle.main.url(forResource: "clock", withExtension: "scpt") else {
            print("no url")
            return
        }

        guard let script = NSAppleScript(contentsOf: URL, error: &error) else {
            print("no script \(String(describing: error))")
            return
        }

        let output = script.executeAndReturnError(
            &error)

        print("output: \(output)")

        if (error != nil) {
            print("error: \(String(describing: error))")
        }
    }

}


//        guard let script = NSAppleScript(source:
//                                            """
//set currentValue to ¬
//    (do shell script ¬
//        "defaults read com.apple.menuextra.clock 'IsAnalog'") ¬
//        as integer as boolean
//
//do shell script ¬
//    "defaults write com.apple.menuextra.clock 'IsAnalog' -bool " & (not currentValue) & ";"
//
//""") else {
//            return
//        }

@TramPamPam
Copy link
Author

switch@2x

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment