Detecting a portable Mac

Mac OSX specific forum
mrbungle
Enthusiast
Enthusiast
Posts: 188
Joined: Wed Dec 30, 2020 3:18 am

Detecting a portable Mac

Post by mrbungle »

I created this after playing with IOKit to build my own battery monitoring app. Considering that portable Macs far outsell desktop models (85% to 15%) and have additional hardware like TouchID and a battery, this might be useful.

Use cases:

1. Portable Macs have 13" to 16" displays but still have much less screen real estate than a desktop model, so you can use this code to make simple adjustments to program defaults like how you may display certain information in the menu bar or default to smaller windows and dialog boxes.

2. If you ever need to monitor the battery, you can smartly determine if your program is actually running on a portable Mac and handle cases where the program is not running on a portable machine gracefully.

3. Since 2020, all portable Mac's support TouchID authentication, so you can use this as a quick way to assume the Mac has TouchID suppport (though you still should check if TouchID can be initialized).

4. Power use is much more critical for portable Macs, so knowing if your program is running on a portable, you can expose certain functions to give users more control.

It's pretty simple: It reads from the Mac's system dictionary for the key that reveals the current model is using a battery.

Code: Select all


ImportC "-framework IOKit"
  IOServiceMatching.i(name.p-ascii)
  IOServiceGetMatchingService.i(masterPort.i, matchingDictionary.i)
  IOObjectRelease.i(object.i)
EndImport

Procedure.b IsPortableMac()
  Protected batteryService.i
  Protected result.b = #False
  
  batteryService = IOServiceGetMatchingService(0, IOServiceMatching("AppleSmartBattery"))
  
  If batteryService
    result = #True
    IOObjectRelease(batteryService)
  EndIf
  
  ProcedureReturn result
EndProcedure

; --- Test ---
If IsPortableMac()
	Debug "Portable Mac (Battery Detected)"
Else
	Debug "Desktop Mac"
EndIf
User avatar
Piero
Addict
Addict
Posts: 1214
Joined: Sat Apr 29, 2023 6:04 pm
Location: Italy

Re: Detecting a portable Mac

Post by Piero »

Thanks for the useful info, and I hope you didn't miss that you can easily, ENORMOUSLY increase the "screen space" on (any, "new") mac with utilities like BetterDisplay (free for "basic" stuff)…

PS: be sure to activate System Settings > Accessibility > Zoom before…
Post Reply