Adding Keyboard Shortcuts For Inspecting iOS Apps and Web Pages in Safari #
Back in iOS 6 Apple added the ability to remotely inspect pages in mobile Safari and UIWebViews. While I'm very grateful for that capability, the fact that it's buried in a submenu in Safari's “Develop” menu means that I have to navigate a maze with a mouse every time I relaunch the app. I decided to investigate adding a way of triggering the inspector via a keyboard shortcut.
The target
My first thought was that I could add a keyboard shortcut via OS X's built-in support. After all, “mobile.html” is just another menu item. Something like:
If only it were so easy
Unfortunately, while that worked if I opened the “Develop” menu at least once, it didn't on a cold start of Safari. I'm guessing that the contents of the menu are generated dynamically (and lazily), and thus there isn't a “mobile.html” item initially for the keyboard shortcut system to hook into.
Inspired by a similar BBEdit script, I then decided to experiment with AppleScript and the System Events UI automation framework. After cursing at AppleScript for a while (can't wait for JavaScript for Automation), I ended up with:
tell application "Safari" to activate tell application "System Events" to ¬ click menu item "mobile.html" of menu ¬ "iPhone Simulator" of menu item "iPhone Simulator" of menu ¬ "Develop" of menu bar item "Develop" of menu bar 1 of process "Safari"
That seemed to work reliably, now it was just a matter of binding it to a keyboard shortcut. There apps like FastScripts that provide this capability, but to make the script more portable, I wanted a way that didn't depend on third-party software. It turned out that Automator can be used to do this, albeit in a somewhat convoluted fashion:
- Launch Automator
- Create a new “Service” workflow
- Add a “Run AppleScript” action¹
- Change the setting at the top of the window to “Service receives no input in any application“
- Replace the
(* Your script goes here *)
placeholder with the script above (your workflow should end up looking like this) - Save the service as “Inspect Simulator”
I wanted to attach a keyboard shortcut to this service when either Safari or the simulator were running, but not in other apps. I therefore then went to the “App Shortcuts” keyboard preferences pane (pictured above) and added shortcuts for that menu item in both apps (to add shortcuts for the simulator, you need to select the “Other…” option in the menu and select it from /Applications/
).
One final gotcha is that the first time the script is run in either app, you will get a “The action 'Run AppleScript' encountered an error.” dialog. Immediately behind that dialog is another, saying “'Safari.app' would like to control this computer using accessibility features.” You'll need to open the Security & Privacy preferences pane and enable Safari (and the simulator's) accessibility permissions.
- Not be confused with the “Execute AppleScript” action, which is a Remote Desktop one — I did that and was puzzled by the “no computers” error message for a good while.
6 Comments
Setting the actual shortcut was the hardest part, because you can do it on Services menu on the Keyboard pane, but depends on the keys, it just wont work...like using hte Option. I'm not sure, but in the end, it works! Woof!
Some added tricks for people to play with
(Also Apple changed the menu naming...)
on run {input, parameters}
--build the phonegap app
tell application "Terminal"
if not (exists window 1) then reopen
activate
do script "cd PATH_TO_FILE" in window 1
do script "cordova emulate ios --target=' iPhone-6, 10.0'" in window 1
end tell
delay 30 --takes a while to build the app
--this displays the safari app's console
tell application "Safari" to activate
tell application "System Events" to ¬
click menu item "index.html" of menu ¬
"Simulator" of menu item "Simulator" of menu ¬
"Develop" of menu bar item "Develop" of menu bar 1 of process "Safari"
--from http://blog.persistent.info/2014/06/adding-keyboard-shortcuts-for.html
-- return input
tell application "Simulator" to activate
tell application "System Events"
set visible of application process "Terminal" to false
end tell
end run
Post a Comment