Skip to content

Instantly share code, notes, and snippets.

@macshome
Last active February 13, 2025 16:01
Show Gist options
  • Save macshome/1eb974187224da7d6ceae56dc9fb1c9c to your computer and use it in GitHub Desktop.
Save macshome/1eb974187224da7d6ceae56dc9fb1c9c to your computer and use it in GitHub Desktop.
A playground to explore the properties of a ProcessInfo object with Swift
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`
@macshome
Copy link
Author

Sample output:
(The process name is empty when running in a playground.)

Process Info:
Process Name: 
Process Identifier: 25784
Process UUID: DF23F2DD-F165-4D4D-81A3-BA81B7A79E74-25784-0000014E7BE5278B
Environment: ["TMPDIR": "/var/folders/kf/q3kq08s500j46l9fw2knpx1m0000gp/T/", "LOGNAME": "josh.wisenbaker", "LOGGER_DEPTH": "6", "SSH_AUTH_SOCK": "/private/tmp/com.apple.launchd.oMhHDe6A8J/Listeners", "__CF_USER_TEXT_ENCODING": "0x1F6:0x0:0x0", "XPC_FLAGS": "0x0", "XPC_SERVICE_NAME": "com.apple.dt.Xcode.PlaygroundStub-macosx", "DYLD_LIBRARY_PATH": "/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/Developer/Platforms/MacOSX.platform/Developer/Library/Frameworks:/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/Library/Frameworks:/Users/josh.wisenbaker/Library/Developer/Xcode/DerivedData/ProcessInfo-fgpxfjbxkktiztewrgjpofyilgri/Build/Intermediates.noindex/Playgrounds/ProcessInfo/Products/Debug:/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/usr/lib", "HOME": "/Users/josh.wisenbaker", "SHELL": "/bin/zsh", "PLAYGROUND_COMMUNICATION_SOCKET": "/var/folders/kf/q3kq08s500j46l9fw2knpx1m0000gp/T/com.apple.dt.Xcode.pg/s/24232-11", "PACKAGE_RESOURCE_BUNDLE_PATH": "/Users/josh.wisenbaker/Library/Developer/Xcode/DerivedData/ProcessInfo-fgpxfjbxkktiztewrgjpofyilgri/Build/Intermediates.noindex/Playgrounds/Products/Debug", "DYLD_FRAMEWORK_PATH": "/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/Developer/Platforms/MacOSX.platform/Developer/Library/Frameworks:/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/Library/Frameworks:/Users/josh.wisenbaker/Library/Developer/Xcode/DerivedData/ProcessInfo-fgpxfjbxkktiztewrgjpofyilgri/Build/Intermediates.noindex/Playgrounds/ProcessInfo/Products/Debug", "USER": "josh.wisenbaker", "PATH": "/usr/bin:/bin:/usr/sbin:/sbin"]
Arguments: ["/Applications/Xcode.app/Contents/SharedFrameworks/DVTPlaygroundStubMacServices.framework/Versions/A/XPCServices/com.apple.dt.Xcode.PlaygroundStub-macosx.xpc/Contents/MacOS/com.apple.dt.Xcode.PlaygroundStub-macosx"]
Is Mac Catalyst: false
Is iOS App On Mac: false

System Info:
System Uptime: 59858.23753004167 seconds
Physical Memory: 32 GB
Operating System Version: Version 15.3.1 (Build 24D70)
Active Processor Count: 10
Processor Count: 10
Thermal State: 0
Is Low Power Mode Enabled: false
Host Name: chonk-josh-wisenbaker.local

User Info:
User Name: josh.wisenbaker
Full User Name: Josh Wisenbaker

Operating System Struct:
Major Version: 15
Minor Version: 3
Patch Version: 1

Thermal State via switch:
Nominal

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