Skip to content

Instantly share code, notes, and snippets.

@toioski
Last active June 5, 2023 01:30
Show Gist options
  • Star 11 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save toioski/44abee301719d090808727d46c0567bc to your computer and use it in GitHub Desktop.
Save toioski/44abee301719d090808727d46c0567bc to your computer and use it in GitHub Desktop.
Automatically Open Safari Dev Tools for iOS Simulator
-- `menu_click`, by Jacob Rus, September 2006.
-- Ref: https://stackoverflow.com/questions/14669542/automatically-open-the-safari-debugger-when-the-iphone-simulator-is-launched
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
-- Find Simulator entry
global simulatorValue
activate application "Safari"
tell application "System Events"
tell process "Safari"
set menuItemList to name of every menu item of menu "Develop" of menu bar item "Develop" of menu bar 1
repeat with menuItem in menuItemList
if menuItem contains "Simulator" then
set simulatorValue to menuItem
end if
end repeat
end tell
end tell
log simulatorValue
-- Find webview entry inside Simulator
global menuValue
activate application "Safari"
tell application "System Events"
tell process "Safari"
set menuItemList to name of every menu item of menu simulatorValue of menu item simulatorValue of menu "Develop" of menu bar item "Develop" of menu bar 1
repeat with menuItem in menuItemList
if menuItem contains "www" then
set menuValue to menuItem
end if
end repeat
end tell
end tell
menu_click({"Safari", "Develop", simulatorValue, menuValue})
@GanZhiXiong
Copy link

This script is really well written👍👍👍

How to execute the script after Xcode installs the App on the phone?

@tyirvine
Copy link

Might wanna add or menuItem contains ".com" to line 62

@feel5ny
Copy link

feel5ny commented Dec 14, 2022

Thank you. It was annoying every time I opened WebView Inspector, but it really helped me a lot.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment