Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

brosenz

macrumors 6502
Original poster
Apr 26, 2011
342
90
I am looking for an online guide/training/video to get started creating a macOS Menu Bar app using SwiftUI to show the Battery Temperature, something simple, probably the only option I need is to be able to change the refresh rate, any help pointing me to the right direction will be appreciated, thank you
 

casperes1996

macrumors 604
Jan 26, 2014
7,485
5,649
Horsens, Denmark
With such a specific app in mind - is what you're looking for to learn the skills to make it or just the app itself. Cause I could alternatively point you to an app that already exists that can be made to do this for you; Stats (Free and open source).

If you want to learn how to make apps like that with some level of generality - what's your existing programming experience level? The best kind of resources are going to depend on how much/what you already know.

Does it have to be SwiftUI? To my knowledge it's still not possible to make menu bar items exclusively with SwiftUI; You'd need to wrap the SwiftUI View in an AppKit NSView or something like that, so it might be easier to stick with just AppKit/storyboard - You can check out how I did HexHelper on my GitHub for the general approach on making an app that lives in the menu bar - the code isn't the cleanest or greatest but for seeing how to make a menu bar app it should be fine. It's available on my GitHub: GitHub.com/casperes1996/HexHelper
The AppDelegate, Info.plist, storyboard and ViewController contain the most relevant parts to setting up a menu bar app. This is AppKit though not SwiftUI - but as mentioned pretty sure you can't do it only in SwiftUI

As for reading the sensor, one approach at least is to use SMCKit

Alternatively inspect the code used in Stats; I know they have two mechanisms, one being SMCKit but they also have some other sensor reading system they use, maybe just through sysctl or something, not sure if it can dump battery sensor readings
 
  • Like
Reactions: bogdanw

brosenz

macrumors 6502
Original poster
Apr 26, 2011
342
90
With such a specific app in mind - is what you're looking for to learn the skills to make it or just the app itself. Cause I could alternatively point you to an app that already exists that can be made to do this for you; Stats (Free and open source).

If you want to learn how to make apps like that with some level of generality - what's your existing programming experience level? The best kind of resources are going to depend on how much/what you already know.

Does it have to be SwiftUI? To my knowledge it's still not possible to make menu bar items exclusively with SwiftUI; You'd need to wrap the SwiftUI View in an AppKit NSView or something like that, so it might be easier to stick with just AppKit/storyboard - You can check out how I did HexHelper on my GitHub for the general approach on making an app that lives in the menu bar - the code isn't the cleanest or greatest but for seeing how to make a menu bar app it should be fine. It's available on my GitHub: GitHub.com/casperes1996/HexHelper
The AppDelegate, Info.plist, storyboard and ViewController contain the most relevant parts to setting up a menu bar app. This is AppKit though not SwiftUI - but as mentioned pretty sure you can't do it only in SwiftUI

As for reading the sensor, one approach at least is to use SMCKit

Alternatively inspect the code used in Stats; I know they have two mechanisms, one being SMCKit but they also have some other sensor reading system they use, maybe just through sysctl or something, not sure if it can dump battery sensor readings
Thank you for the information
 

Mark FX

macrumors regular
Nov 18, 2011
159
17
West Sussex, UK
@brosenz

To create a menu bar app on MacOS, you do not have to use SwiftUI, storyboards, or xib files.
These types of app's are created with the "NSStatusItem" class, and added to the systems "NSStatusBar".

Although you can use a storyboard or a xib file if you want to display windows, or a custom menu in an "NSPopover" for example, but Its not strictly required, as the icon or title and the menu for the status item can be done in code alone.

I recently showed someone how to do it in AppleScript, and can be seen in this forum posting here.
https://macscripter.net/viewtopic.php?id=48760

Something can be done just as simple as the AppleScript example in ObjectiveC, or Swift, without storyboards or xib files.
If you want to see the simplest example in Swift then let me know, and I'll post some code, and instructions.
Although you will have to accept my apologies for a slow reply at this moment in time.

Regards Mark
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.