Last active
February 13, 2025 16:01
-
-
Save macshome/1eb974187224da7d6ceae56dc9fb1c9c to your computer and use it in GitHub Desktop.
A playground to explore the properties of a ProcessInfo object with Swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
// ProcessInfo is a class that provides information about the current process. | |
// It provides an easy way to access information about the process, the system, and the user. | |
// Note that `processInfo` is a static property on the `ProcessInfo` class. | |
let info = ProcessInfo.processInfo | |
print("Process Info:") | |
print("Process Name: \(info.processName)") | |
print("Process Identifier: \(info.processIdentifier)") | |
print("Process UUID: \(info.globallyUniqueString)") | |
print("Environment: \(info.environment)") | |
print("Arguments: \(info.arguments)") | |
print("Is Mac Catalyst: \(info.isMacCatalystApp)") | |
print("Is iOS App On Mac: \(info.isiOSAppOnMac)") | |
// There are also properties that provide information about the system and the user that the task is running on/as. | |
print("\nSystem Info:") | |
print("System Uptime: \(info.systemUptime) seconds") | |
print("Physical Memory: \(info.physicalMemory / (1024 * 1024 * 1024)) GB") | |
print("Operating System Version: \(info.operatingSystemVersionString)") | |
print("Active Processor Count: \(info.activeProcessorCount)") | |
print("Processor Count: \(info.processorCount)") | |
print("Thermal State: \(info.thermalState.rawValue)") | |
print("Is Low Power Mode Enabled: \(info.isLowPowerModeEnabled)") | |
print("Host Name: \(info.hostName)") | |
print("\nUser Info:") | |
print("User Name: \(info.userName)") | |
print("Full User Name: \(info.fullUserName)") | |
// There is also an operating system struct that can be returned so you aren't munging strings. | |
let osStruct = info.operatingSystemVersion | |
print("\nOperating System Struct:") | |
print("Major Version: \(osStruct.majorVersion)") | |
print("Minor Version: \(osStruct.minorVersion)") | |
print("Patch Version: \(osStruct.patchVersion)") | |
// Thermal state is an enum that can be used to determine the thermal state of the device. | |
let thermalState = info.thermalState | |
print("\nThermal State via switch:") | |
switch thermalState { | |
case .nominal: | |
print("Nominal") | |
case .fair: | |
print("Fair") | |
case .serious: | |
print("Serious") | |
case .critical: | |
print("Critical") | |
} | |
// There are also notifications you can listen to for when the thermal state changes | |
// and for power state changes. | |
// For example: `thermalStateDidChangeNotification` and `NSProcessInfoPowerStateDidChangeNotification` |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Sample output:
(The process name is empty when running in a playground.)