how to get list of available ios simulators?
Asked Answered
P

2

16

i want to list available ios simulators as in below format ( iOS version should take from JSON key )

{
  "name": "iPhone 5s (11.4)",
  "udid": "FB6B3F7F-E6CD-4F64-8EC4-6C07C86A25C8"
}
{
  "name": "iPhone 6 Plus (11.4)",
  "udid": "768AE0B3-F77A-4DF4-8198-74CB2D598D19"
}
...
{
  "name": "iPad Air (11.4)",
  "udid": "396C0E3A-7DFD-41BD-9E05-28A27E6BB4AB"
}
{
  "name": "iPad Air 2 (11.4)",
  "udid": "1AD4AC1A-B45B-4869-9778-B5572D4CE031"
}
...
{
  "name": "iPhone 8 (13.2)",
  "udid": "71E02A7B-BCBB-45D8-8638-E5381B420406"
}
{
  "name": "iPhone 8 Plus (13.2)",
  "udid": "D3333C55-2D0A-4999-B6D2-450C637C9248"
}
...
{
  "name": "iPad (7th generation) (13.2)",
  "udid": "0543121E-86DC-4BC9-933F-60AA31D219CD"
}
{
  "name": "iPad Pro (11-inch) (13.2)",
  "udid": "1979F272-E938-45AA-9717-A5228AF6EBF1"
}
...

i tried with something like this

xcrun simctl list --json devices available | jq '.devices | with_entries(select(.key|contains("com.apple.CoreSimulator.SimRuntime.iOS")))'

but i was unable to implement rest of the jq manipulations to achieve expected format.

output for xcrun simctl list --json devices available as below

{
  "devices" : {
    "com.apple.CoreSimulator.SimRuntime.iOS-11-4" : [
      {
        "state" : "Shutdown",
        "isAvailable" : true,
        "name" : "iPhone 5s",
        "udid" : "FB6B3F7F-E6CD-4F64-8EC4-6C07C86A25C8"
      },
      {
        "state" : "Shutdown",
        "isAvailable" : true,
        "name" : "iPhone 6 Plus",
        "udid" : "768AE0B3-F77A-4DF4-8198-74CB2D598D19"
      },
      {
        "state" : "Shutdown",
        "isAvailable" : true,
        "name" : "iPhone 6",
        "udid" : "A02B7489-0D21-4317-AF01-94604C910ACB"
      },
      {
        "state" : "Shutdown",
        "isAvailable" : true,
        "name" : "iPhone 6s",
        "udid" : "8482548E-3A1A-4EE8-833A-C45B59F87061"
      },
      {
        "state" : "Booted",
        "isAvailable" : true,
        "name" : "iPhone 6s Plus",
        "udid" : "12D1E272-48EB-4C26-8F64-201C8C2375ED"
      },
      {
        "state" : "Shutdown",
        "isAvailable" : true,
        "name" : "iPhone SE",
        "udid" : "558C8C2A-C993-4728-8847-06B9353F234D"
      },
      {
        "state" : "Shutdown",
        "isAvailable" : true,
        "name" : "iPhone 7",
        "udid" : "D43EF2BC-7298-4900-B94F-4C72C09A1709"
      },
      {
        "state" : "Shutdown",
        "isAvailable" : true,
        "name" : "iPhone 7 Plus",
        "udid" : "3F6D7F10-84EA-49C0-ADFD-58F80125BE18"
      },
      {
        "state" : "Shutdown",
        "isAvailable" : true,
        "name" : "iPhone 8",
        "udid" : "04DD5864-D2BA-42D9-AC30-28C2E8FB553A"
      },
      {
        "state" : "Shutdown",
        "isAvailable" : true,
        "name" : "iPhone 8 Plus",
        "udid" : "E37DFF3B-2065-4116-9BA9-2A5DEB588E40"
      },
      {
        "state" : "Shutdown",
        "isAvailable" : true,
        "name" : "iPhone X",
        "udid" : "3A5FA651-64A9-4207-A8EA-9005907FB1DC"
      },
      {
        "state" : "Shutdown",
        "isAvailable" : true,
        "name" : "iPad Air",
        "udid" : "396C0E3A-7DFD-41BD-9E05-28A27E6BB4AB"
      },
      {
        "state" : "Shutdown",
        "isAvailable" : true,
        "name" : "iPad Air 2",
        "udid" : "1AD4AC1A-B45B-4869-9778-B5572D4CE031"
      },
      {
        "state" : "Shutdown",
        "isAvailable" : true,
        "name" : "iPad Pro (9.7-inch)",
        "udid" : "9050DF70-51B3-4D7D-8188-D8CE7309AC15"
      },
      {
        "state" : "Shutdown",
        "isAvailable" : true,
        "name" : "iPad Pro (12.9-inch)",
        "udid" : "551D5F5A-FC3E-4F7A-B19C-F6E05486D7F9"
      },
      {
        "state" : "Shutdown",
        "isAvailable" : true,
        "name" : "iPad (5th generation)",
        "udid" : "BBB67188-C866-4314-BD14-5969E89D327F"
      },
      {
        "state" : "Shutdown",
        "isAvailable" : true,
        "name" : "iPad Pro (12.9-inch) (2nd generation)",
        "udid" : "861D5117-DB9C-4175-A3E9-6A61BBD6EEC8"
      },
      {
        "state" : "Shutdown",
        "isAvailable" : true,
        "name" : "iPad Pro (10.5-inch)",
        "udid" : "6677E587-25F1-48B2-B13E-1F7D7BCAB8E8"
      }
    ],
    "com.apple.CoreSimulator.SimRuntime.watchOS-6-0" : [

    ],
    "com.apple.CoreSimulator.SimRuntime.iOS-13-1" : [

    ],
    "com.apple.CoreSimulator.SimRuntime.tvOS-13-0" : [

    ],
    "com.apple.CoreSimulator.SimRuntime.watchOS-6-1" : [
      {
        "state" : "Shutdown",
        "isAvailable" : true,
        "name" : "Apple Watch Series 4 - 40mm",
        "udid" : "D3D274E3-E139-4703-A8E5-344A68E847EB"
      },
      {
        "state" : "Shutdown",
        "isAvailable" : true,
        "name" : "Apple Watch Series 4 - 44mm",
        "udid" : "DFB29187-06DF-4741-A220-59AF0EB93CD5"
      },
      {
        "state" : "Shutdown",
        "isAvailable" : true,
        "name" : "Apple Watch Series 5 - 40mm",
        "udid" : "2C61EF40-69B0-4427-BF5F-D53871E38202"
      },
      {
        "state" : "Shutdown",
        "isAvailable" : true,
        "name" : "Apple Watch Series 5 - 44mm",
        "udid" : "BFF68412-CED6-4331-932E-D0E266DF0729"
      }
    ],
    "com.apple.CoreSimulator.SimRuntime.tvOS-13-2" : [
      {
        "state" : "Shutdown",
        "isAvailable" : true,
        "name" : "Apple TV",
        "udid" : "82076106-2C88-49E9-B294-4710C185FEA0"
      },
      {
        "state" : "Shutdown",
        "isAvailable" : true,
        "name" : "Apple TV 4K",
        "udid" : "9E125B78-669C-4AA2-A896-B2ED594B25D5"
      },
      {
        "state" : "Shutdown",
        "isAvailable" : true,
        "name" : "Apple TV 4K (at 1080p)",
        "udid" : "25491FB4-8C64-42A9-9DC4-91D1F2969CF0"
      }
    ],
    "com.apple.CoreSimulator.SimRuntime.iOS-13-2" : [
      {
        "state" : "Shutdown",
        "isAvailable" : true,
        "name" : "iPhone 8",
        "udid" : "71E02A7B-BCBB-45D8-8638-E5381B420406"
      },
      {
        "state" : "Shutdown",
        "isAvailable" : true,
        "name" : "iPhone 8 Plus",
        "udid" : "D3333C55-2D0A-4999-B6D2-450C637C9248"
      },
      {
        "state" : "Booted",
        "isAvailable" : true,
        "name" : "iPhone 11",
        "udid" : "A6B7C24D-FDDE-403D-AEE6-0ADA25D681F9"
      },
      {
        "state" : "Shutdown",
        "isAvailable" : true,
        "name" : "iPhone 11 Pro",
        "udid" : "E88DCE74-BF13-45A6-B0E8-2436175765CD"
      },
      {
        "state" : "Shutdown",
        "isAvailable" : true,
        "name" : "iPhone 11 Pro Max",
        "udid" : "925FE40D-3FBC-4E60-801C-A44DB3CBF866"
      },
      {
        "state" : "Shutdown",
        "isAvailable" : true,
        "name" : "iPad Pro (9.7-inch)",
        "udid" : "429D0104-D49D-4454-AA59-428F0A7B35BC"
      },
      {
        "state" : "Shutdown",
        "isAvailable" : true,
        "name" : "iPad (7th generation)",
        "udid" : "0543121E-86DC-4BC9-933F-60AA31D219CD"
      },
      {
        "state" : "Shutdown",
        "isAvailable" : true,
        "name" : "iPad Pro (11-inch)",
        "udid" : "1979F272-E938-45AA-9717-A5228AF6EBF1"
      },
      {
        "state" : "Shutdown",
        "isAvailable" : true,
        "name" : "iPad Pro (12.9-inch) (3rd generation)",
        "udid" : "EBA9C68B-0162-4FBB-B104-32BD5B1C3512"
      },
      {
        "state" : "Shutdown",
        "isAvailable" : true,
        "name" : "iPad Air (3rd generation)",
        "udid" : "8C26E739-659F-4631-A607-8C34C0DA2E5F"
      }
    ]
  }
}

how do i list available ios simulators as expected format ?

Paean answered 11/11, 2019 at 18:32 Comment(6)
What does the json of the xcrun call look like?Countermeasure
@kumara: You need to provide the output of xcrun simctl list --json devices available before someone can answer you. There aren't many examples of the command's output online, presumably because those who feel the need to run it can just do so.Sacramental
@SimonShine @ JeffMercado I added the output of xcrun simctl list --json devices availablePaean
thanks @Dmitry i tried with jtc this approach is cool. but it hasn't been added the iOS version in the device name. version is taken from the key. i need something from "com.apple.CoreSimulator.SimRuntime.iOS-11-4" : [ { "state" : "Shutdown", "isAvailable" : true, "name" : "iPhone 5s", "udid" : "FB6B3F7F-E6CD-4F64-8EC4-6C07C86A25C8" },... to { "name": "iPhone 5s (11.4)", "udid": "FB6B3F7F-E6CD-4F64-8EC4-6C07C86A25C8" },...Paean
@kumara, that is easy - just capture (via regex) the version into a regex group (which are namespaces too) and then use it during template interpolation, like this: xcrun ... | jtc -x'<^com\.apple\.CoreSimulator\.SimRuntime\.iOS-(\d+)-(\d+)>L:[0][-1]<name>l:<n>v[-1][udid]' -T'{"name":"{n} ({$1}.{$2})", "udid":"{}" }'Eastward
@Eastward wow! this works nicely. Many thanks for your time.Paean
C
5

The version info that you're trying to grab is part of the devices keys. So you'll want to extract that out and build out your results formatted as needed. You could use contains combined with substrings to perform the extraction but using capture/1 here could simplify the process.

Here's one way you could do it:

$ xcrun ... | jq '.devices | to_entries[]
  | (.key | capture("com\\.apple\\.CoreSimulator\\.SimRuntime\\.iOS-(?<version>.+)")) as {$version}
  | .value[] | {name: "\(.name) (\($version|sub("-"; ".")))", udid}'
Countermeasure answered 12/11, 2019 at 5:22 Comment(7)
What do the curly braces in ... as {$version} contribute to?Sacramental
@jeff nice! this works fine. I am so very thankful for your time.Paean
@SimonShine the result of using capture here returns an object with a "version" property with the captured group from the key. We're destructuring that object to save that version property directly to the $version variable.Countermeasure
Oohh!!! Amazing. Thanks, @JeffMercado. Am I correct to assume that destructuring is only syntactically available after as and ??/, or is it available elsewhere?Sacramental
@SimonShine Well, as and ?// as far as I'm aware are the only places. One could argue it could be done for function arguments as well, but I don't believe it's implemented there currently.Countermeasure
command not found: jqCorrosion
@Corrosion Try brew install jq. That fixed the "command not found" error for me.Alkmaar
T
3

A simpler version

xcrun simctl list devices --json | jq '.devices[] | .[] | .name'
Tommi answered 23/8, 2023 at 19:21 Comment(1)
add -r to the last jq call to (e.g. jq -r '.devices[] | .[] | .name' to remove quotesInvestiture

© 2022 - 2024 — McMap. All rights reserved.