Thought I'd post this for anyone who runs into the same need. Someone on reddit gave me this solution (which I tested and it works great). Note that this time you need an ellipsis after "Settings", not three periods (weird).
This is an AppleScript that can be invoked from the command line to reset the Simulator:
tell application "iPhone Simulator"
activate
end tell
tell application "System Events"
tell process "iPhone Simulator"
tell menu bar 1
tell menu bar item "iOs Simulator"
tell menu "iOs Simulator"
click menu item "Reset Content and Settings…"
end tell
end tell
end tell
tell window 1
click button "Reset"
end tell
end tell
end tell
The keyboard short-cut solution is not relevant anymore and unfortunately @Cameron solution didn't work for me either (I tried to debug it with no luck)
Here is what works for me:
#!/bin/bash
# `menu_click`, by Jacob Rus, September 2006
#
# Accepts a list of form: `{"Finder", "View", "Arrange By", "Date"}`
# Execute the specified menu item. In this case, assuming the Finder
# is the active application, arranging the frontmost folder by date.
osascript <<SCRIPT
on menu_click(mList)
local appName, topMenu, r
-- Validate our input
if mList's length < 3 then error "Menu list is not long enough"
-- Set these variables for clarity and brevity later on
set {appName, topMenu} to (items 1 through 2 of mList)
set r to (items 3 through (mList's length) of mList)
-- This overly-long line calls the menu_recurse function with
-- two arguments: r, and a reference to the top-level menu
tell application "System Events" to my menu_click_recurse(r, ((process appName)'s ¬
(menu bar 1)'s (menu bar item topMenu)'s (menu topMenu)))
end menu_click
on menu_click_recurse(mList, parentObject)
local f, r
-- `f` = first item, `r` = rest of items
set f to item 1 of mList
if mList's length > 1 then set r to (items 2 through (mList's length) of mList)
-- either actually click the menu item, or recurse again
tell application "System Events"
if mList's length is 1 then
click parentObject's menu item f
else
my menu_click_recurse(r, (parentObject's (menu item f)'s (menu f)))
end if
end tell
end menu_click_recurse
application "iPhone Simulator" activate
menu_click({"iPhone Simulator", "iOS Simulator", "Reset Content and Settings…"})
tell application "System Events"
tell process "iPhone Simulator"
tell window 1
click button "Reset"
end tell
end tell
end tell
SCRIPT
I want to add something to Cameron Brown's answer. To make sure the correct version is resetted (e.g, iPad, version 6.1), I start the iOS Simulator via ios-sim:
I wrote a script that will reset the contents & settings of all versions and devices for the iOS Simulator. It grabs the device names and version numbers from the menu, so it will include any new devices or iOS versions that Apple releases simulators for.
It's easy to run manually or use in a build-script. I would suggest adding it as a Pre-Action Run Script before the build.
It's based heavily on Stian's script above, but doesn't become stale with new iOS versions, and eliminates the dialog box (better for automation build scripts and working from the command-line).
target names and Simulator app name seem to have changed a bit to xCode6 / iOS8.
Here is un updated version of Cameron Brown's osascript for xCode6 / iOS8:
tell application "iPhone Simulator"
activate
end tell
tell application "System Events"
tell process "iPhone Simulator"
tell menu bar 1
tell menu bar item "iOs Simulator"
tell menu "iOs Simulator"
click menu item "Reset Content and Settings…"
end tell
end tell
end tell
tell window 1
click button "Reset"
end tell
end tell
end tell
COPY-PASTE ANSWER - note: will reset the contents and settings of all available simulators.
Thanks @Alpine for the inspiration and knowledge. If you run this in your command line you should be able to reset all the available sims. This works with Xcode 6.
# Get the sim list with the UUIDs
OUTPUT="$(xcrun simctl list)"
# Parse out the UUIDs and saves them to file
echo $OUTPUT | awk -F "[()]" '{ for (i=2; i<NF; i+=2) print $i }' | grep '^[-A-Z0-9]*$' > output.txt
# Iterate through file and reset sim
for UUID in `awk '{ print $1 }' output.txt`
do
xcrun simctl erase $UUID
done
We use the following python script to reset the simulator on our build server.
#!/usr/bin/env python
import subprocess
import re
import os
def uninstall_app():
print 'Running %s' % __file__
# Get (maybe read from argv) your bundle identifier e.g. com.mysite.app_name
try:
bundle_identifier = '$' + os.environ['BUNDLE_IDENTIFIER']
except KeyError, e:
print 'Environment variable %s not found. ' % e
print 'Environment: ', os.environ
exit(1)
print 'Uninstalling app with Bundle identifier: ', bundle_identifier
# We call xcrun, and strip the device GUIDs from the output
process = subprocess.Popen(['xcrun', 'simctl', 'list'], stdout=subprocess.PIPE)
# Read first line
line = process.stdout.readline()
while True:
# Assume first match inside parenthesis is what we want
m = re.search('\((.*?)\)', line)
if not (m is None):
# The regex found something,
# group(1) will throw away the surrounding parenthesis
device_GUID = m.group(1)
# Brutely call uninstall on all listed devices. We know some of these will fail and output an error, but, well..
subprocess.call(['xcrun', 'simctl', 'uninstall', device_GUID, bundle_identifier])
# Read next line
line = process.stdout.readline()
# Stop condition
if line == '':
break
if __name__ == '__main__':
uninstall_app()
It assumse your app's bundle identifier is set as an environment variable, e.g.
export BUNDLE_IDENTIFIER=com.example.app_name
maybe you'd want to pass the bundle identifier in another way.
Building on most of the answers above, I am using Keyboard Maestro and made a little Macro to reset the currently running Simulator and restarting it.
It turns the focus back to Xcode after resetting and restarting, so I can hit Command+R again right away to re-run the app, which I find very convenient.
The content of the ruby script is:
#!/usr/bin/env ruby
list = `xcrun simctl list`.split("\n")
list.each do |line|
if line =~ /\(Booted\)$/
device = line.match(/([^(]*)\s+\(([^)]*)\)\s+\(([^)]*)\).*/)[1]
uuid = line.match(/([^(]*)\s+\(([^)]*)\)\s+\(([^)]*)\).*/)[2]
status = line.match(/([^(]*)\s+\(([^)]*)\)\s+\(([^)]*)\).*/)[3]
puts uuid
break
end
end
Here's a Rakefile task to reset a targeted simulator. This works with Xcode 7 since the Xcode 7 command line tools broke the xcrun simctl uninstall command. I have a little custom runC method since I like to see the actual terminal command as well as its output.
desc "Resets the iPhone Simulator state"
task :reset_simulator => [] do
deviceDestinationName = 'iPhone 6' #for efficiency since we only target one device for our unit tests
puts "...starting simulator reset"
runC('killall "iOS Simulator"')
runC('killall "Simulator"')
runC('xcrun simctl list > deviceList.txt')
lines = File.open('deviceList.txt').readlines
lines.each do |line|
lineStripped = line.strip
if (lineStripped=~/[a-zA-Z0-9]{8}-[a-zA-Z0-9]{4}-[a-zA-Z0-9]{4}-[a-zA-Z0-9]{4}-[a-zA-Z0-9]{12}/)
if (lineStripped !~ /unavailable/ && lineStripped.include?("#{deviceDestinationName} ("))
puts "Inspecting simulator: #{lineStripped} by making sure it is shut down, then erasing it."
needsShutdown = !lineStripped.include?('Shutdown')
aDeviceId = lineStripped[/[a-zA-Z0-9]{8}-[a-zA-Z0-9]{4}-[a-zA-Z0-9]{4}-[a-zA-Z0-9]{4}-[a-zA-Z0-9]{12}/]
if (needsShutdown)
runC("xcrun simctl shutdown #{aDeviceId}")
end
runC("xcrun simctl erase #{aDeviceId}")
#does not work to just uninstall the app with Xcode 7, do just rely on resetting the device above
#`xcrun simctl uninstall #{aDeviceId} com.animoto.AppServiceClientTester`
end
end
end
runC('rm deviceList.txt')
end
#Runs a command and prints out both the command that will be run and the results
def runC(command)
puts '$ ' + command
puts `#{command}`
end
I have found this very helpful tool called "SimulatorManager":
http://tue-savvy.github.io
It will reset all you simulators by means of a menu bar widget (not sure if that's what its called) but on top of that it will give you quick access to all your application data. I really can't live without it anymore.
Spread the word!
In the Application picker, select "Other..." to open the app picker dialog.
In this dialog you're unable to "Show Package Contents" to explore a .app, so you'll need to use Go to Folder via Cmd-Shift-G. (First open the application drop down and select Other)