Device Class
In some scenerios, it is beneficial for an Omni Automation script to derive information about the device running the script. For example, a plug-in that creates new views might need to adjust its actions based upon whether the device running the script is an iPad or an iPhone. The Device class can be used to derive information about the device and operating system on which the script is executed.
Class Properties
current (Device r/o) • The device the current application is running on.
Instance Properties
iOS (Boolean r/o) • A convenience property whose value is true on iPhone and iPad devices.
iPad (Boolean r/o) • A convenience property whose value is true only on iPad devices, but not on iPhone devices.
mac (Boolean r/o) • A convenience property whose value is true only on Mac devices.
visionPro (Boolean r/o) • A convenience that returns true only on Apple Vision Pro devices.
operatingSystemVersion (Version r/o) • The current operation system version running on the device.
type (Device.Type or null r/o) • The general type of the current device.
Device.Type Class
Class properties for the Device.Type class:
iPad (Device.Type r/o) • An iPad.
iPhone (Device.Type r/o) • An iPhone.
mac (Device.Type r/o) • A Macintosh computer.
visionPro (Device.Type r/o) • An Apple Vision Pro.
all (Array of Device.Type r/o) • An array of all items of this enumeration.
Examples
Here are some examples of the use of the Device class:
Current Device
Device.current
Sample Results:
- Mac: [object Device] {iOS: false, iPad: false, mac: true, operatingSystemVersion: [object Version: 14.4.1], type: [object DeviceType: mac], visionPro: false}
- iPad: [object Device] {iOS: true, iPad: true, mac: false, operatingSystemVersion: [object Version: 14.2], type: [object DeviceType: iPad], visionPro: false}
- iPhone: [object Device] {iOS: true, iPad: false, mac: false, operatingSystemVersion: [object Version: 14.4], type: [object DeviceType: iPhone], visionPro: false}
A script that checks device type:
Device Type
currentDeviceType = Device.current.type
if (currentDeviceType === DeviceType.iPhone){
console.log("This device is an iPhone.")
} else if (currentDeviceType === DeviceType.iPad){
console.log("This device is an iPad.")
} else if (currentDeviceType === DeviceType.mac){
console.log("This device is a Mac.")
} else if (currentDeviceType === DeviceType.visionPro){
console.log("This device is an Apple Vision Pro.")
} else {
console.log("This is an unknown type of device.")
}
Determining current device using JavaScript switch statement:
Device Type (Switch Statement)
switch(Device.current.type){
case DeviceType.iPhone:
console.log("This device is an iPhone.")
break;
case DeviceType.iPad:
console.log("This device is an iPad.")
break;
case DeviceType.mac:
console.log("This device is a Mac.")
break;
case DeviceType.visionPro:
console.log("This device is an Apple Vision Pro.")
break;
default:
console.log("This is an unknown type of device.")
}
A script that checks operating system:
Operating System Version
Device.current.operatingSystemVersion
//--> [object Version: 14.4.1] {versionString: "14.4.1"}
Here is a script that checks to see if the current device is a Mac running macOS 11 (Big Sur) or newer:
Device/OS Version Check (macOS Big Sur or newer)
if (Device.current.mac){
currentVersion = Device.current.operatingSystemVersion
requiredVersion = new Version("11")
result = currentVersion.atLeast(requiredVersion)
if (result === false){
title = "INCOMPATIBLE OS"
msg = "This script requires macOS 11.0 or newer. "
msg = msg.concat("The current operating system version is: " + currentVersion.versionString)
new Alert(title, msg).show()
}
} else {
title = "INCOMPATIBLE DEVICE"
msg = "This plug-in requires a device running macOS 11.0 or newer."
new Alert(title, msg).show()
}
A script that sets the keyboard to be displayed for the specified text input field in a plug-in form viewed on an iPhone:
Set Keyboard Type for Device (iPhone)
if (Device.current.type === DeviceType.iPhone){
textInputField.keyboardType = KeyboardType.PhonePad
}