• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Scripting the Touch Bar
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Scripting the Touch Bar


  • Subject: Scripting the Touch Bar
  • From: Bill Cheeseman <email@hidden>
  • Date: Fri, 21 Apr 2017 09:18:10 -0400

I have discovered that the MacBook Pro (Late 2016) Touch Bar in macOS Sierra 10.12.2 and later supports GUI Scripting using AppleScript. Apple's API Reference for the NSTouchBar class says "Because the Touch Bar is designed to work with AppKit, it is fully accessible." And wow, is it ever!

Using my UI Browser product <http://pfiddlesoft.com/uibrowser> to target several applications known to support the Touch Bar on my Touch Bar-equipped MacBook Pro, I discovered that one of the child UI elements of the root application UI element of every such application, alongside its window and menu bar elements, is a new UI element whose AXRole attribute is "AXFunctionRowTopLevelElement". This is the application area in the center of the Touch Bar. Its immediate AXChildren array contains all of the Touch Bar items (pop up buttons, groups, etc.) contained in the application area of the Touch Bar.

The attributes of each of these Touch Bar items include such information as the item's frame, position and size within the entire Touch Bar, the item's role (e.g., "AXPopUpButton") and role description (e.g., "touchbar popover"), its title (if any), and even its accessibility help description (e.g., "Tap to choose special characters and emojis"). It also includes a reference to its parent and children elements, so you can navigate up and down the Touch Bar's accessibility hierarchy at will.

Using these discoveries, I have written  AppleScript scripts for the Touch Bar using GUI Scripting supported by Apple's System Events application. Here's an example. It requires either a MacBook Pro (Late 2016) with Touch Bar, or Daniel Jalkut's Touché application, free download at <https://red-sweater.com/touche/>, and macOS Sierra 10.12.2 or later. Run the script from within Script Editor or Script Debugger, and don't forget to allow access for the script editor in the Security & Privacy pane of System Preferences.

-- Touch Bar GUI Scripting Demo
-- 1.0.0 Bill Cheeseman 2017-04-21

-- Usage: Run this script in Script Editor or Script Debugger to see its Touch Bar information.

property touchBarRole : "AXFunctionRowTopLevelElement"

tell application "System Events"
set frontApp to first application process whose frontmost is true
try
set touchBar to first UI element of frontApp whose role is touchBarRole
on error errMsg number errNum
display alert "No Touch Bar support" message "This application or the Mac running it does not support the Touch Bar, or accessibility has not been allowed for script runner in the Security & Privacy pane of System Preferences.

" & errMsg & space & errNum
return
end try
set touchBarItems to value of attribute "AXChildren" of touchBar
return properties of item 1 of touchBarItems
end tell

--> {minimum value:missing value, orientation:missing value, position:{80, 0}, class:pop up button, role description:"touchbar popover", accessibility description:"character picker", focused:false, title:"", size:{72, 30}, value:missing value, help:"Tap to choose special characters and emojis", enabled:true, maximum value:missing value, role:"AXPopUpButton", entire contents:{}, subrole:missing value, selected:missing value, name:missing value, description:"character picker"}

-- 

Bill Cheeseman - email@hidden

 _______________________________________________
Do not post admin requests to the list. They will be ignored.
AppleScript-Users mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
Archives: http://lists.apple.com/archives/applescript-users

This email sent to email@hidden

  • Prev by Date: Re: Using system log
  • Next by Date: "Wait for files to copy" and "Dispense disk items incrementally" actions - AS analogues and how to use?
  • Previous by thread: Re: Google Chrome Appears to be Preparing to Disallow JavaScript injection via Applescript
  • Next by thread: "Wait for files to copy" and "Dispense disk items incrementally" actions - AS analogues and how to use?
  • Index(es):
    • Date
    • Thread