annotate ScriptingBridge_iOSSimulatorQuit.lua @ 7:8956367af822

Added ScriptingBridge_iOSSimulatorQuit.lua for Part 4
author Eric Wing <ewing@anscamobile.com>
date Thu, 11 Aug 2011 15:44:58 -0700
parents
children
rev   line source
7
8956367af822 Added ScriptingBridge_iOSSimulatorQuit.lua for Part 4
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
1 #!/Library/Frameworks/LuaCocoa.framework/Versions/Current/Tools/luacocoa
8956367af822 Added ScriptingBridge_iOSSimulatorQuit.lua for Part 4
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
2
8956367af822 Added ScriptingBridge_iOSSimulatorQuit.lua for Part 4
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
3 LuaCocoa.import("ScriptingBridge")
8956367af822 Added ScriptingBridge_iOSSimulatorQuit.lua for Part 4
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
4 LuaCocoa.import("Foundation")
8956367af822 Added ScriptingBridge_iOSSimulatorQuit.lua for Part 4
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
5
8956367af822 Added ScriptingBridge_iOSSimulatorQuit.lua for Part 4
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
6 -- Not finding through scripting bridge, so adding here
8956367af822 Added ScriptingBridge_iOSSimulatorQuit.lua for Part 4
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
7 kAEDefaultTimeout = -1
8956367af822 Added ScriptingBridge_iOSSimulatorQuit.lua for Part 4
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
8 kNoTimeOut = -2
8956367af822 Added ScriptingBridge_iOSSimulatorQuit.lua for Part 4
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
9
8956367af822 Added ScriptingBridge_iOSSimulatorQuit.lua for Part 4
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
10 local next_skin = arg[1] or nil
8956367af822 Added ScriptingBridge_iOSSimulatorQuit.lua for Part 4
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
11
8956367af822 Added ScriptingBridge_iOSSimulatorQuit.lua for Part 4
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
12 -- The simulator does not have a scripting dictionary.
8956367af822 Added ScriptingBridge_iOSSimulatorQuit.lua for Part 4
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
13 -- We must manipulate it through system events.
8956367af822 Added ScriptingBridge_iOSSimulatorQuit.lua for Part 4
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
14 --local iOSSimulator = SBApplication:applicationWithBundleIdentifier_("com.apple.iphonesimulator")
8956367af822 Added ScriptingBridge_iOSSimulatorQuit.lua for Part 4
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
15 local system_events = SBApplication:applicationWithBundleIdentifier_("com.apple.systemevents")
8956367af822 Added ScriptingBridge_iOSSimulatorQuit.lua for Part 4
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
16 system_events:setTimeout_(kNoTimeOut)
8956367af822 Added ScriptingBridge_iOSSimulatorQuit.lua for Part 4
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
17
8956367af822 Added ScriptingBridge_iOSSimulatorQuit.lua for Part 4
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
18 --local iOSSimulator = systemEvents:processes():byName_("iPhone Simulator")
8956367af822 Added ScriptingBridge_iOSSimulatorQuit.lua for Part 4
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
19 local processes = system_events:processes()
8956367af822 Added ScriptingBridge_iOSSimulatorQuit.lua for Part 4
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
20 local ios_sim_process = nil
8956367af822 Added ScriptingBridge_iOSSimulatorQuit.lua for Part 4
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
21 for i=1, #processes do
8956367af822 Added ScriptingBridge_iOSSimulatorQuit.lua for Part 4
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
22 -- print("processes[".. tostring(i) .. "]" .. processes[i]:name())
8956367af822 Added ScriptingBridge_iOSSimulatorQuit.lua for Part 4
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
23 if tostring(processes[i]:name()) == "iPhone Simulator" then
8956367af822 Added ScriptingBridge_iOSSimulatorQuit.lua for Part 4
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
24 ios_sim_process = processes[i]
8956367af822 Added ScriptingBridge_iOSSimulatorQuit.lua for Part 4
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
25 end
8956367af822 Added ScriptingBridge_iOSSimulatorQuit.lua for Part 4
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
26 end
8956367af822 Added ScriptingBridge_iOSSimulatorQuit.lua for Part 4
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
27
8956367af822 Added ScriptingBridge_iOSSimulatorQuit.lua for Part 4
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
28 if not ios_sim_process then
8956367af822 Added ScriptingBridge_iOSSimulatorQuit.lua for Part 4
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
29 print("Could not find iPhone Simulator process running. Aborting...")
8956367af822 Added ScriptingBridge_iOSSimulatorQuit.lua for Part 4
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
30 os.exit(1)
8956367af822 Added ScriptingBridge_iOSSimulatorQuit.lua for Part 4
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
31 end
8956367af822 Added ScriptingBridge_iOSSimulatorQuit.lua for Part 4
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
32
8956367af822 Added ScriptingBridge_iOSSimulatorQuit.lua for Part 4
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
33 -- Bring the app to the foreground
8956367af822 Added ScriptingBridge_iOSSimulatorQuit.lua for Part 4
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
34 ios_sim_process:setFrontmost_(true)
8956367af822 Added ScriptingBridge_iOSSimulatorQuit.lua for Part 4
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
35
8956367af822 Added ScriptingBridge_iOSSimulatorQuit.lua for Part 4
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
36 --local menus = ios_sim_process:menuBars()[1]:menus()
8956367af822 Added ScriptingBridge_iOSSimulatorQuit.lua for Part 4
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
37 local menu_items = ios_sim_process:menuBars()[1]:menus()[2]:menuItems()
8956367af822 Added ScriptingBridge_iOSSimulatorQuit.lua for Part 4
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
38 local reset_option = nil
8956367af822 Added ScriptingBridge_iOSSimulatorQuit.lua for Part 4
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
39 local quit_option = nil
8956367af822 Added ScriptingBridge_iOSSimulatorQuit.lua for Part 4
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
40 --print(menu_items)
8956367af822 Added ScriptingBridge_iOSSimulatorQuit.lua for Part 4
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
41 for i=1, #menu_items do
8956367af822 Added ScriptingBridge_iOSSimulatorQuit.lua for Part 4
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
42 -- NSLog("%@", menu_items[i]:title())
8956367af822 Added ScriptingBridge_iOSSimulatorQuit.lua for Part 4
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
43 -- Note the ellipses is not 3 dots but Alt-Semicolon
8956367af822 Added ScriptingBridge_iOSSimulatorQuit.lua for Part 4
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
44 if tostring(menu_items[i]:title()) == "Reset Content and Settingsā€¦" then
8956367af822 Added ScriptingBridge_iOSSimulatorQuit.lua for Part 4
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
45 reset_option = menu_items[i]
8956367af822 Added ScriptingBridge_iOSSimulatorQuit.lua for Part 4
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
46 elseif tostring(menu_items[i]:title()) == "Quit iOS Simulator" then
8956367af822 Added ScriptingBridge_iOSSimulatorQuit.lua for Part 4
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
47 quit_option = menu_items[i]
8956367af822 Added ScriptingBridge_iOSSimulatorQuit.lua for Part 4
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
48 end
8956367af822 Added ScriptingBridge_iOSSimulatorQuit.lua for Part 4
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
49 end
8956367af822 Added ScriptingBridge_iOSSimulatorQuit.lua for Part 4
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
50 if not reset_option then
8956367af822 Added ScriptingBridge_iOSSimulatorQuit.lua for Part 4
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
51 print("Warning: Could not find 'Reset Content and Settingsā€¦', setting to last known index position")
8956367af822 Added ScriptingBridge_iOSSimulatorQuit.lua for Part 4
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
52 reset_option = menu_items[3]
8956367af822 Added ScriptingBridge_iOSSimulatorQuit.lua for Part 4
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
53 end
8956367af822 Added ScriptingBridge_iOSSimulatorQuit.lua for Part 4
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
54 if not quit_option then
8956367af822 Added ScriptingBridge_iOSSimulatorQuit.lua for Part 4
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
55 print("Warning: Could not find 'Quit iOS Simulator', setting to last known index position")
8956367af822 Added ScriptingBridge_iOSSimulatorQuit.lua for Part 4
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
56 quit_option = menu_items[7]
8956367af822 Added ScriptingBridge_iOSSimulatorQuit.lua for Part 4
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
57 end
8956367af822 Added ScriptingBridge_iOSSimulatorQuit.lua for Part 4
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
58
8956367af822 Added ScriptingBridge_iOSSimulatorQuit.lua for Part 4
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
59 local ret_val = reset_option:clickAt_(reset_option:position())
8956367af822 Added ScriptingBridge_iOSSimulatorQuit.lua for Part 4
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
60
8956367af822 Added ScriptingBridge_iOSSimulatorQuit.lua for Part 4
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
61 -- A confirmation dialog appears and we must hit the correct button.
8956367af822 Added ScriptingBridge_iOSSimulatorQuit.lua for Part 4
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
62 -- The sleep helps make sure the alert has enough time to show up.
8956367af822 Added ScriptingBridge_iOSSimulatorQuit.lua for Part 4
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
63 os.execute("sleep 1")
8956367af822 Added ScriptingBridge_iOSSimulatorQuit.lua for Part 4
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
64 --print(ret_val)
8956367af822 Added ScriptingBridge_iOSSimulatorQuit.lua for Part 4
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
65
8956367af822 Added ScriptingBridge_iOSSimulatorQuit.lua for Part 4
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
66 -- This requires that keyboard navigation for buttons is on in Accessibility
8956367af822 Added ScriptingBridge_iOSSimulatorQuit.lua for Part 4
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
67 system_events:keystroke_using_(" ", 0)
8956367af822 Added ScriptingBridge_iOSSimulatorQuit.lua for Part 4
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
68
8956367af822 Added ScriptingBridge_iOSSimulatorQuit.lua for Part 4
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
69
8956367af822 Added ScriptingBridge_iOSSimulatorQuit.lua for Part 4
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
70 if next_skin == "iphone" then
8956367af822 Added ScriptingBridge_iOSSimulatorQuit.lua for Part 4
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
71 next_skin_index = 2
8956367af822 Added ScriptingBridge_iOSSimulatorQuit.lua for Part 4
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
72 elseif next_skin == "ipad" then
8956367af822 Added ScriptingBridge_iOSSimulatorQuit.lua for Part 4
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
73 next_skin_index = 1
8956367af822 Added ScriptingBridge_iOSSimulatorQuit.lua for Part 4
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
74 elseif next_skin == "iphone4" then
8956367af822 Added ScriptingBridge_iOSSimulatorQuit.lua for Part 4
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
75 next_skin_index = 3
8956367af822 Added ScriptingBridge_iOSSimulatorQuit.lua for Part 4
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
76 end
8956367af822 Added ScriptingBridge_iOSSimulatorQuit.lua for Part 4
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
77
8956367af822 Added ScriptingBridge_iOSSimulatorQuit.lua for Part 4
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
78 if next_skin_index then
8956367af822 Added ScriptingBridge_iOSSimulatorQuit.lua for Part 4
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
79
8956367af822 Added ScriptingBridge_iOSSimulatorQuit.lua for Part 4
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
80 --menu_items = ios_sim_process:menuBars()[1]:menus()[5]:menuItems()[1]:menuItems()
8956367af822 Added ScriptingBridge_iOSSimulatorQuit.lua for Part 4
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
81 -- Will take us to "Devices"
8956367af822 Added ScriptingBridge_iOSSimulatorQuit.lua for Part 4
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
82 menu_items = ios_sim_process:menuBars()[1]:menus()[5]:menuItems()
8956367af822 Added ScriptingBridge_iOSSimulatorQuit.lua for Part 4
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
83 local iphone_retina = nil
8956367af822 Added ScriptingBridge_iOSSimulatorQuit.lua for Part 4
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
84 --[[
8956367af822 Added ScriptingBridge_iOSSimulatorQuit.lua for Part 4
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
85 for i=1, #menu_items do
8956367af822 Added ScriptingBridge_iOSSimulatorQuit.lua for Part 4
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
86 NSLog("%@, %@", menu_items[i]:title(), menu_items[i])
8956367af822 Added ScriptingBridge_iOSSimulatorQuit.lua for Part 4
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
87 end
8956367af822 Added ScriptingBridge_iOSSimulatorQuit.lua for Part 4
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
88 --]]
8956367af822 Added ScriptingBridge_iOSSimulatorQuit.lua for Part 4
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
89
8956367af822 Added ScriptingBridge_iOSSimulatorQuit.lua for Part 4
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
90 -- Can't figure out how to tell if checked
8956367af822 Added ScriptingBridge_iOSSimulatorQuit.lua for Part 4
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
91 --[==[
8956367af822 Added ScriptingBridge_iOSSimulatorQuit.lua for Part 4
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
92 local sub_menu_items = ios_sim_process:menuBars()[1]:menus()[5]:menuItems()[1]:menus()[1]:menuItems()
8956367af822 Added ScriptingBridge_iOSSimulatorQuit.lua for Part 4
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
93 for i=1, #sub_menu_items do
8956367af822 Added ScriptingBridge_iOSSimulatorQuit.lua for Part 4
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
94 NSLog("%@, %@", sub_menu_items[i]:title(), sub_menu_items[i]:value())
8956367af822 Added ScriptingBridge_iOSSimulatorQuit.lua for Part 4
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
95 end
8956367af822 Added ScriptingBridge_iOSSimulatorQuit.lua for Part 4
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
96 --]==]
8956367af822 Added ScriptingBridge_iOSSimulatorQuit.lua for Part 4
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
97
8956367af822 Added ScriptingBridge_iOSSimulatorQuit.lua for Part 4
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
98 -- Setting the simulator to iPhone 4 mode via menu Menu->Hardware->Devices->iPhone (Retina)
8956367af822 Added ScriptingBridge_iOSSimulatorQuit.lua for Part 4
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
99 -- This is because our launch tool doesn't have a way to set between iPhone/iPhone4,
8956367af822 Added ScriptingBridge_iOSSimulatorQuit.lua for Part 4
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
100 -- so we leave it as the last user preference.
8956367af822 Added ScriptingBridge_iOSSimulatorQuit.lua for Part 4
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
101 iphone_retina = menu_items[1]:menus()[1]:menuItems()[next_skin_index]
8956367af822 Added ScriptingBridge_iOSSimulatorQuit.lua for Part 4
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
102 iphone_retina:clickAt_(iphone_retina:position())
8956367af822 Added ScriptingBridge_iOSSimulatorQuit.lua for Part 4
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
103 end
8956367af822 Added ScriptingBridge_iOSSimulatorQuit.lua for Part 4
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
104
8956367af822 Added ScriptingBridge_iOSSimulatorQuit.lua for Part 4
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
105
8956367af822 Added ScriptingBridge_iOSSimulatorQuit.lua for Part 4
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
106 -- Now quit the simulator via the menu option.
8956367af822 Added ScriptingBridge_iOSSimulatorQuit.lua for Part 4
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
107 quit_option:clickAt_(quit_option:position())
8956367af822 Added ScriptingBridge_iOSSimulatorQuit.lua for Part 4
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
108
8956367af822 Added ScriptingBridge_iOSSimulatorQuit.lua for Part 4
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
109 os.exit(0)
8956367af822 Added ScriptingBridge_iOSSimulatorQuit.lua for Part 4
Eric Wing <ewing@anscamobile.com>
parents:
diff changeset
110