ricardopereira
ricardopereira

Reputation: 11683

How to enable/disable “Show as run destination” for all simulators

Is it possible to toggle the “Show as run destination” flag for multiple iOS simulators instead of changing it one by one in the "Device and Simulators" window? Is there a command line with that purpose?

enter image description here

Upvotes: 9

Views: 2044

Answers (1)

ricardopereira
ricardopereira

Reputation: 11683

I decided to find it by myself using fswatch. BTW, it's really useful for this kind of situations. By monitoring the changes of file while toggling the "Show as run destination" flag, I found out that Xcode was changing the ~/Library/Preferences/com.apple.dt.Xcode.plist file 🙌

After some analysis, I noticed the key that I needed to change to achieve what I had in mind. The key is DVTIgnoredDevices and it holds an array of simulators. So, each simulator UUID in that list will be ignored in Xcode.

Now, I can change the DVTIgnoredDevices key using defaults command line tool specifying the needed value type:

-array Allows the user to specify an array as the value for the given preference key:

defaults write somedomain preferenceKey -array element1 element2 element3

The specified array overwrites the value of the key if the key was present at the time of the write. If the key was not present, it is created with the new value.

Example:

defaults write com.apple.dt.Xcode DVTIgnoredDevices '(
  "80E16DBC-2FE5-48AC-8A44-1F5DEFA00EA7",
  "B8C4D5FF-8F1A-4895-BD16-CCAFECD71098"
)'

After setting the DVTIgnoredDevices key, you need to clean the DerivedData folder and restart Xcode. To clean the DerivedData folder, please see this answer or just run the shortcut shift+alt+cmd+k (that's what I usually do).

Tested on Xcode Version 9.4 (9F1027a).

UPDATE:

I usually like to have just a couple of Simulators in the list, so I decided to do a script using instruments -s devices and add all the current Simulators to the DVTIgnoredDevices key. Then I chose which simulator(s) will be shown 😊

Xcode-hide-all-iPhone-simulators.sh

simulatorsIdentifiers=$(instruments -s devices |
  grep -o "iPhone .* (.*) \[.*\]" | #only iPhone
  grep -o "\[.*\]" | #only UUID
  sed "s/^\[\(.*\)\]$/\1/" | #remove square brackets
  sed 's/^/"/;$!s/$/"/;$s/$/"/' | #add quotes
  sed '$!s/$/,/' #add comma to separate each element
)

arrayOfSimulatorsIdentifiers=($(echo "$simulatorsIdentifiers" | tr ',' '\n'))

# Add simulators to DVTIgnoredDevices
echo "${#arrayOfSimulatorsIdentifiers[@]}"
for index in "${!arrayOfSimulatorsIdentifiers[@]}"
do
    echo "$index Adding: ${arrayOfSimulatorsIdentifiers[index]}"
done

defaults write com.apple.dt.Xcode DVTIgnoredDevices -array ${arrayOfSimulatorsIdentifiers[@]}

Gist file

Upvotes: 16

Related Questions