Ready to build a cool City Hall Clock app for your Mac? Great! We're going to create an app that sits in your menu bar, chimes every 15 minutes, and even counts out the hours. Let's break it down step by step, and I'll explain every part of the code so you can understand what's going on.
Our City Hall Clock app will:
First things first, let's set up our project:
mkdir CityHallClock cd CityHallClock
go mod init cityhallclock
go get github.com/getlantern/systray go get github.com/faiface/beep
Now, let's create our main.go file and go through each function:
package main import ( "bytes" "log" "os" "path/filepath" "time" "github.com/faiface/beep" "github.com/faiface/beep/mp3" "github.com/faiface/beep/speaker" "github.com/getlantern/systray" ) var ( audioBuffer *beep.Buffer ) func main() { initAudio() systray.Run(onReady, onExit) } // ... (other functions will go here)
Let's break down each function:
func main() { initAudio() systray.Run(onReady, onExit) }
This is where our app starts. It does two important things:
func initAudio() { execPath, err := os.Executable() if err != nil { log.Fatal(err) } resourcesPath := filepath.Join(filepath.Dir(execPath), "..", "Resources") chimeFile := filepath.Join(resourcesPath, "chime.mp3") f, err := os.Open(chimeFile) if err != nil { log.Fatal(err) } defer f.Close() streamer, format, err := mp3.Decode(f) if err != nil { log.Fatal(err) } defer streamer.Close() audioBuffer = beep.NewBuffer(format) audioBuffer.Append(streamer) err = speaker.Init(format.SampleRate, format.SampleRate.N(time.Second/10)) if err != nil { log.Fatal(err) } }
This function sets up our audio:
If anything goes wrong (like not finding the sound file), it'll log the error and quit.
func onReady() { systray.SetIcon(getIcon()) systray.SetTitle("City Hall Clock") systray.SetTooltip("City Hall Clock") mQuit := systray.AddMenuItem("Quit", "Quit the app") go func() { <-mQuit.ClickedCh systray.Quit() }() go runClock() }
This function sets up our menu bar icon:
func onExit() { // Cleanup tasks go here }
This function is called when the app is quitting. We're not doing anything here, but you could add cleanup tasks if needed.
func runClock() { ticker := time.NewTicker(time.Minute) defer ticker.Stop() for { select { case t := <-ticker.C: if t.Minute() == 0 || t.Minute() == 15 || t.Minute() == 30 || t.Minute() == 45 { go chime(t) } } } }
This is our clock's "heart":
func chime(t time.Time) { hour := t.Hour() minute := t.Minute() var chimeTimes int if minute == 0 { chimeTimes = hour % 12 if chimeTimes == 0 { chimeTimes = 12 } } else { chimeTimes = 1 } for i := 0; i < chimeTimes; i++ { streamer := audioBuffer.Streamer(0, audioBuffer.Len()) speaker.Play(streamer) time.Sleep(time.Duration(audioBuffer.Len()) * time.Second / time.Duration(audioBuffer.Format().SampleRate)) if i < chimeTimes-1 { time.Sleep(500 * time.Millisecond) // Wait between chimes } } }
This function plays our chimes:
func getIcon() []byte { execPath, err := os.Executable() if err != nil { log.Fatal(err) } iconPath := filepath.Join(filepath.Dir(execPath), "..", "Resources", "icon.png") // Read the icon file icon, err := os.ReadFile(iconPath) if err != nil { log.Fatal(err) } return icon }
This function gets our menu bar icon:
To make our app a proper macOS citizen, we need to create an application bundle. This involves creating an Info.plist file:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> <plist version="1.0"> <dict> <key>CFBundleExecutable</key> <string>CityHallClock</string> <key>CFBundleIconFile</key> <string>AppIcon</string> <key>CFBundleIdentifier</key> <string>com.yourcompany.cityhallclock</string> <key>CFBundleName</key> <string>City Hall Clock</string> <key>CFBundlePackageType</key> <string>APPL</string> <key>CFBundleShortVersionString</key> <string>1.0</string> <key>CFBundleVersion</key> <string>1</string> <key>LSMinimumSystemVersion</key> <string>10.12</string> <key>LSUIElement</key> <true/> <key>NSHighResolutionCapable</key> <true/> </dict> </plist>
Save this as Info.plist in your project directory.
We need two icons:
Let's create a build script (build.sh):
#!/bin/bash # Build the Go application go build -o CityHallClock # Create the app bundle structure mkdir -p CityHallClock.app/Contents/MacOS mkdir -p CityHallClock.app/Contents/Resources # Move the executable to the app bundle mv CityHallClock CityHallClock.app/Contents/MacOS/ # Copy the Info.plist cp Info.plist CityHallClock.app/Contents/ # Copy the chime sound to Resources cp chime.mp3 CityHallClock.app/Contents/Resources/ # Copy the menu bar icon cp icon.png CityHallClock.app/Contents/Resources/ # Copy the application icon cp AppIcon.icns CityHallClock.app/Contents/Resources/ echo "Application bundle created: CityHallClock.app"
Make it executable with chmod +x build.sh, then run it with ./build.sh.
And there you have it! You've built a fully functional City Hall Clock app for macOS. You've learned about:
Feel free to expand on this. Maybe add preferences for custom chimes or different chiming intervals. The sky's the limit!
You can find full source code here https://github.com/rezmoss/citychime
Happy coding, and enjoy your new clock!
The above is the detailed content of Building a City Hall Clock App for macOS: A Comprehensive Guide. For more information, please follow other related articles on the PHP Chinese website!