Skip to content

Instantly share code, notes, and snippets.

@wearhere
Created February 10, 2012 10:52
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save wearhere/1788744 to your computer and use it in GitHub Desktop.
Save wearhere/1788744 to your computer and use it in GitHub Desktop.
Toggle Between Script and Editor Log Views in UIAutomation Instrument
(*
Running this script will cause Instruments to become active
and switch from the Script view (where you edit your UIAutomation script)
to the Editor Log view (where you see the logs of executing those scripts)
or vice versa.
*)
-- JW: This block only needs to be executed once, and can then be removed.
-- I don't know if leaving it in might cause a performance hit;
-- the comment (copied from UI Browser) makes it seem like not.
on enabledGUIScripting(switch)
-- Call this handler and pass 'true' in the switch parameter to enable GUI Scripting before your script executes any GUI Scripting commands, or pass 'false' to disable GUI Scripting. You need not test the 'UI elements enabled' setting before calling this handler, because authorization is required only if 'UI elements enabled' will be changed. Returns the final setting of 'UI elements enabled', even if unchanged.
tell application "System Events"
activate -- brings System Events authentication dialog to front
set UI elements enabled to switch
return UI elements enabled
end tell
end enabledGUIScripting
enabledGUIScripting(true)
tell application "Instruments" to activate
tell application "System Events"
tell process "Instruments"
-- Open Script/Editor Log menu
-- Trace and action identified by using UI Browser
tell splitter group 1 of splitter group 1 of window 1
set navMenu to static text 2 of list 1
set currentValue to navMenu's value
tell navMenu to perform action "AXShowMenu"
end tell
end tell
-- I couldn't figure out how to identify the menu item to directly press it
-- so we press it using keystrokes instead
-- From bottom, menu reads "Script", "Editor Log"
-- so press up once always, and a second time if we need to switch to "Editor Log"
key code 126
if (currentValue is "Script") then
key code 126
end if
-- press enter
key code 36
end tell
@wearhere
Copy link
Author

Editing scripts in UIAutomation is a pretty painful experience. It's pretty clear that Apple intended for people to do little more than hit Record and maybe change some of the drop-downs afterward. There are no keyboard shortcuts for anything related to scripting, which is unfortunate because creating a script is a pretty fiddly process, and every time you run it – via a button – Instruments will change the view to the "Editor Log", and then you have to change it back – via a menu – to the script.

Via the magic of GUI scripting and with the help of the brilliant UI Browser to examine Instruments' interface, I've created this Applescript to programmatically push Instruments' buttons. Save it as an application, run it and Instruments will switch from Script->Editor Log or v.v.

Apps like BetterTouchTool will let you execute scripts like these via global keyboard shortcuts or global or app-specific gestures. I use three-finger swipe up/down to run the toggling script (as an analogue of the switch header<->source command in Xcode). It'd be pretty easy to script the Run/Record/Stop buttons too, but I didn't want to burn global keyboard shortcuts on those and didn't think that gestures would feel intuitive.

@asalom
Copy link

asalom commented Apr 17, 2015

At the Status section where it says: Script is stopped. Double click that and you'll switch between Script and Editor log views

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