Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Automatically open the Safari Debugger when the iPhone Simulator is launched

The iOS web debugger in Safari is the bee's knees, but it closes every time the Simulator is restarted. Not only is it annoying to re-open it from the menu after every build, but it makes it tricky to debug any behavior that happens during startup.

Is there a way to set up a trigger in Xcode to automatically open the Safari debugger after every build, or perhaps a way to build a shell script or Automator action to do a build and immediately open the debugger?

like image 544
Luke Dennis Avatar asked Feb 03 '13 04:02

Luke Dennis


2 Answers

This is a partial solution. This opens the debug window of Safari with one click which is a lot better but not automatic.

Open Script Editor on your mac (Command + Space Bar and type in Script Editor)

Paste in this code:

-- `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.

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

menu_click({"Safari", "Develop", "Simulator", "index.html"})

Once the simulator has opened, click run on your script (you might need to allow the script editor in the settings the first time).

(Optional) You can save your the scripts as an app so that you don't have to have the script editor open.

like image 127
Dev01 Avatar answered Nov 20 '22 17:11

Dev01


There is question that should be marked a duplicate that describes using setTimeout() to give you enough time to switch windows over to Safari and set a breakpoint.

Something like this, where startMyApp is the bootstrap function of your app:

setTimeout(function () {
  startMyApp();
}, 20000);

It is super ghetto, but does work. I've submitted a feature request via http://www.apple.com/feedback/safari.html too to close the loop.

like image 4
Rob Barreca Avatar answered Nov 20 '22 17:11

Rob Barreca