Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Script for opening web inspector window for remote debugging iOS web apps (including phonegap and other webview apps). This version of the script will suspend and wait for 30 seconds for Safari to establish a connection with the device (or simulator) and open the web inspector window as soon as technically possible.
#!/usr/bin/osascript
# Name of the device as visible in Safari->Develop menu
set deviceName to "iPhone Simulator"
# Number of seconds to wait for the simulator window to show up
set maxWait to 30
# ---------------------------------------
# You shouldn't modify anything below here
set hasClicked to false
set x to 0
tell application "Safari"
activate
repeat until hasClicked or x > (maxWait * 10)
try
tell application "System Events"
click menu item "index.html" of menu deviceName of menu item deviceName of menu "Develop" of menu bar item $item "Develop" of menu bar 1 of application process "Safari"
end tell
set hasClicked to true
on error foo
delay 0.1
set x to x + 1
end try
end repeat
if hasClicked = false then
display dialog "Unable to connect to iOS simulator - make sure that it's working" buttons {"OK"} default button 1
else
try
tell application "System Events"
click button 1 of window "Top Sites" of application process "Safari"
end tell
end try
return
end if
end tell
Owner

Thinkscape commented Jan 21, 2014

I've also noticed that it's not currently possible to invoke the script as .cordova/hooks/after_run, because the way phonegap cli invokes hook scripts. The sub-process does not have access to the GUI so it's not able to find or work on safari window.

The easiest workaround is to run:

chmod a+x ./safari-webinspector.applescript

(./safari-webinspector.applescript & ); phonegap run ios

This will wait for safari to connect to iOS (i.e. simulator) and display webinspector window as soon as possible.

mftaher commented Mar 2, 2014

getting this error:

./safari-webinspector.applescript:632:633: script error: Expected expression, property or key form, etc. but found unknown token. (-2741)

raztus commented Apr 8, 2014

@mftaher Same error here. To fix it I removed the $item from the click menu command:

click menu item "index.html" of menu deviceName of menu item deviceName of menu "Develop" of menu bar item "Develop" of menu bar 1 of application process "Safari"

udfalkso commented Nov 5, 2014

Thanks for this!

Can't get this to work in Xcode 6.1.1
I have changed deviceName to "iOS Simulator" and applied the change suggested by @raztus. Run build but it just hangs without running the app in the Sim. Any ideas?

jfrumar commented Jan 10, 2015

OK this is awesome - thanks for sharing!

I had to make the following changes to modernize it for Yosemite:

  • Remove $item on the click menu command as mentioned above
  • Change to set deviceName to "iOS Simulator"
  • Change to click button 1 of window "Favorites" of application process "Safari"

You must also quit the Simulator between sessions (otherwise the script triggers prematurely and ends).

For cordova (assuming you keep it in the scripts folder and have chmod +x scripts/open-webinspector.applescript):

(scripts/open-webinspector.applescript &); cordova emulate ios

This is awesome, been looking for something like this for a while.

So just to summarize all the edits for Yosemite, here's the updated script.

Thanks to @Thinkscape, @jfrumar and @raztus 😄

#!/usr/bin/osascript

# Name of the device as visible in Safari->Develop menu
set deviceName to "iOS Simulator"

# Number of seconds to wait for the simulator window to show up
set maxWait to 30

# ---------------------------------------
# You shouldn't modify anything below here
set hasClicked to false
set x to 0
tell application "Safari"
    activate
    repeat until hasClicked or x > (maxWait * 10)
        try
            tell application "System Events"
                click menu item "index.html" of menu deviceName of menu item deviceName of menu "Develop" of menu bar item "Develop" of menu bar 1 of application process "Safari"
            end tell
            set hasClicked to true
        on error foo
            delay 0.1
            set x to x + 1
        end try
    end repeat
    if hasClicked = false then
        display dialog "Unable to connect to iOS simulator - make sure that it's working" buttons {"OK"} default button 1
    else
        try
            tell application "System Events"
                click button 1 of window "Favorites" of application process "Safari"
            end tell
        end try
        return
    end if
end tell

Thanks everyone!

I made a fork of this to both compile and run the debugger, and also made instructions on how to assign it to a shortcut key with Automator.
https://gist.github.com/uniphonic/fc4e679884aa83787c26

Here is my version of opening Safari Web Inspector - https://gist.github.com/14ea36ce3bc9cc5632b78252fe35ec76

i have error when executing it:

/Users/peter>./safari-webinspector.applescript
./safari-webinspector.applescript:611:612: script error: 預期的是™Ì•‹¶°°Bƒ›© ©Œ¡‰≠»ßŒ¶°µ•,但找到的是•º™æ™∫•N∏π。 (-2741)

now, my safari "iOS simulator" is completely missing

It must works for debugging of real iPad webview without emulator? I see Unable to connect to iOS simulator

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