Automating the User Interface

More information about using AppleScript in the iWork for Mac apps is available on third-party websites such as Mac OS X Automation and iWork Automation. To learn how to do a Mail Merge using AppleScript in Pages 5.5 and later or Numbers 3.5 and later, visit the Mail Merge section of the iWork Automation site. Mac Lock Keychain Applescript. But if it's unlocked and I run the script, then the login keychain will lock. Five Mac Apps Worth Checking Out - August 2020. But AppleScript is also well suited for the desktop. You can use it to set your system to boot up with certain apps open in a particular way, right down to the size, location and content of each.

Unfortunately, not every Mac app has scripting support, and those that do may not always have scripting support for every task you want to automate. You can often work around such limitations, however, by writing a user interface script, commonly called a UI or GUI script. A user interface script simulates user interaction, such as mouse clicks and keystrokes, allowing the script to select menu items, push buttons, enter text into text fields, and more.

Enabling User Interface Scripting

User interface scripting relies upon the OS X accessibility frameworks that provide alternative methods of querying and controlling the interfaces of apps and the system. By default, accessibility control of apps is disabled. For security and privacy reasons, the user must manually enable it on an app-by-app (including script apps) basis.

  1. Launch System Preferences and click Security & Privacy.

  2. Click Accessibility.

  3. Choose an app and click Open.

When running an app that requires accessibility control for the first time, the system prompts you to enable it. See Figure 37-1.

Attempting to run an app that has not been given permission to use accessibility features results in an error. See Figure 37-2.

Note

To run a user interface script in Script Editor, you must enable accessibility for Script Editor.

Admin credentials are required to perform enable user interface scripting.

Targeting an App

User interface scripting terminology is found in the Processes Suite of the System Events scripting dictionary. This suite includes terminology for interacting with most types of user interface elements, including windows, buttons, checkboxes, menus, radio buttons, text fields, and more. In System Events, the process class represents a running app. Listing 37-1 shows how to target an app using this class.

APPLESCRIPT

Listing 37-1AppleScript: Targeting an app for user interface scripting
  1. tell application 'System Events'
  2. tell process 'Safari'
  3. -- Perform user interface scripting tasks
  4. end tell
  5. end tell

To control the user interface of an app, you must first inspect the app and determine its element hierarchy. This can be done by querying the app. For example, Listing 37-2 asks Safari for a list of menus in the menu bar.

APPLESCRIPT

Listing 37-2AppleScript: Querying an app for user interface element information
  1. tell application 'System Events'
  2. tell process 'Safari'
  3. name of every menu of menu bar 1
  4. end tell
  5. end tell
  6. --> Result: {'Apple', 'Safari', 'File', 'Edit', 'View', 'History', 'Bookmarks', 'Develop', 'Window', 'Help'}

Accessibility Inspector (Figure 37-3) makes it even easier to identify user interface element information. This app is included with Xcode. To use it, open Xcode and select Xcode > Open Developer Tool > Accessibility Inspector.

Once you know how an element fits into an interface, you target it within that hierarchy. For example, button X of window Y of process Z.

Clicking a Button

Use the click command to click a button. Listing 37-3 clicks a button in the Safari toolbar to toggle the sidebar between open and closed.

APPLESCRIPT

Listing 37-3AppleScript: Clicking a button
  1. tell application 'System Events'
  2. tell process 'Safari'
  3. tell toolbar of window 1
  4. click (first button where its accessibility description = 'Sidebar')
  5. end tell
  6. end tell
  7. end tell
  8. --> Result: {button 1 of toolbar 1 of window 'AppleScript: Graphic User Interface (GUI) Scripting' of application process 'Safari' of application 'System Events'}

Choosing a Menu Item

Menu items can have a fairly deep hierarchy within the interface of an app. A menu item generally resides within a menu, which resides within a menu bar. In scripting, they must be addressed as such. Listing 37-4 selects the Pin Tab menu item in the Window menu of Safari. /louder-volume-app-mac.html.

APPLESCRIPT

Listing 37-4AppleScript: Choosing a menu item
  1. tell application 'System Events'
  2. tell process 'Safari'
  3. set frontmost to true
  4. click menu item 'Pin Tab' of menu 'Window' of menu bar 1
  5. end tell
  6. end tell
  7. --> Result: menu item 'Pin Tab' of menu 'Window' of menu bar item 'Window' of menu bar 1 of application process 'Safari' of application 'System Events'

Note

Scripting the user interface of an app can be tedious and repetitious. To streamline the process, consider creating handlers to perform common functions. For example, Listing 37-5 shows a handler that can be used to choose any menu item of any menu in any running app.

APPLESCRIPT

Listing 37-5AppleScript: A handler that chooses a menu item
  1. on chooseMenuItem(theAppName, theMenuName, theMenuItemName)
  2. try
  3. -- Bring the target app to the front
  4. tell application theAppName
  5. activate
  6. end tell
  7. -- Target the app
  8. tell application 'System Events'
  9. tell process theAppName
  10. -- Target the menu bar
  11. tell menu bar 1
  12. -- Target the menu by name
  13. tell menu bar item theMenuName
  14. tell menu theMenuName
  15. -- Click the menu item
  16. click menu item theMenuItemName
  17. end tell
  18. end tell
  19. end tell
  20. end tell
  21. end tell
  22. return true
  23. on error
  24. return false
  25. end try
  26. end chooseMenuItem

Listing 37-6 calls the handler in Listing 37-5 to select the Pin Tab menu item in the Window menu of Safari.

APPLESCRIPT

Listing 37-6AppleScript: Calling a handler to choose a menu item

Choosing a Submenu Item

Some menus contain other menus. In these cases, it may be necessary to select a menu item in a submenu of a menu. Listing 37-7 demonstrates how this would be done by selecting a submenu item in Safari.

APPLESCRIPT

Listing 37-7AppleScript: Selecting a submenu item
  1. tell application 'System Events'
  2. tell process 'Safari'
  3. set frontmost to true
  4. click menu item 'Email This Page' of menu of menu item 'Share' of menu 'File' of menu bar 1
  5. end tell
  6. end tell
  7. --> Result: {menu item 'Email This Page' of menu 'Share' of menu item 'Share' of menu 'File' of menu bar item 'File' of menu bar 1 of application process 'Safari' of application 'System Events'}

Copyright © 2018 Apple Inc. All rights reserved. Terms of Use Privacy Policy Updated: 2016-06-13

Apple Applescript

Developers have often relied upon AppleScript’s ability to control the user-interface, to provide an automation solution when no direct scripting support of an application or process was available. While this valued ability continues to be fully supported in Mavericks, the enhanced security focus of the new OS requires a few changes in how scripters access and apply the Accessibility frameworks.

In versions of OS X prior to Mavericks (v10.9), the ability to control the user-interface via AppleScript was determined by the state of a checkbox located in the Accessibility system preference pane. (⬇ see below)

(⬆ see above) The Accessibility system preference pane in OS X Mountain Lion (v10.8). Note the checkbox for enabling control of the user-interface via the accessibility frameworks.

If the checkbox titled “Enable access for assistive devices” (⬆ see above) was selected, you could run AppleScript scripts, applets, and droplets that would simulate a user’s actions, such as selecting a menu item, or pressing a dialog button.

In OS X Mavericks, security controls are more granular, requiring the individual addition of applications, or automation applets that script the accessibility frameworks, to an approval list, displayed in the Security & Privacy system preference pane. (⬇ see below)

App

(⬆ see above) The Accessibility Access List located in Security & Privacy system preference pane of OS X Mavericks (v10.9).

Applications (scripts, applets, etc.) are added to the list either through an integrated approval process, triggered when they first attempt to execute, or by an administrative user proactively dragging their icons into the list.

The following is an example of how the new security protocols for accessing the Accessibility frameworks via AppleScript, are implemented.

In this example, an AppleScript applet titled GUI Scripting Applet contains AppleScript statements for controlling the user-interface of OS X. When the applet is launched, the following dialog will appear: (⬇ see below)

How To Run Applescript File

The error dialog (⬆ see above) indicates that access by the applet to the assistive frameworks has been denied. Click the OK button and a second dialog appears: (⬇ see below)

The second dialog (⬆ see above) announces that the applet you attempted to run, contains code that targets the accessibility frameworks, and that such access can be granted in the Security & Privacy system preference pane. The dialog provides button options to either:

  1. refuse access to the applet, by clicking the Deny button in the dialog; or…
  2. to begin the process of granting access for the applet, by clicking the Open System Preferences button.

Clicking the Open System Preferences button in the dialog (see above), will open the System Preferences application and display the Accessibility section in the Security & Privacypreference pane. Note that the applet will be already added to the access list, but the checkbox next to its name is not selected. (⬇ see below)

In order to add items to the access list, or to edit the status of items already in the access list, you must provide an administrative user name and password. Click the lock icon at the lower left of the System Preferences window (⬆ see above) to summon the system authentication dialog: (⬇ see below)

Enter an administrative unser name and password in the dialog, click the Unlock button, and the preference pane will be ready for editing: (⬇ see below)

To grant access by the applet to the accessibility frameworks, select the checkbox next to applet’s name: (⬇ see below)

And finally, click the lock icon again, to secure the preference pane: (⬇ see below)

In OS X Mavericks, AppleScript applications ('applets') that use Accessibility features may ask for the same information each time you use them, appearing not to remember the settings you previously entered.

This repetition occurs because, by default, applets that use Accessibility features in OS X Mavericks do not save their properties when run. Applets that save their properties modify their own contents in order to save that information. This self-modification makes the applet appear to OS X as a different app each time it is executed, thereby triggering the authorization process repeatedly.

/find-the-apps-running-on-mac.html. Show All Running Apps On Mac Using Force Quit Applications Manager. Another method to check all the Running apps and programs on your Mac is through the Force Quit applications manager on Mac. Click on the Apple icon in the top menu bar of your Mac and then click on Force Quit Application in the drop-down menu (See image below). In the CPU section, you see a list of apps running on your Mac. Right click the top row of categories. In the pop-up list that appears, select Kind. At the right end of the top row, a new. Sudo find / -iname.app This will display all the apps you have on your computer. From there, you can copy and paste the results into a text file to make it easy to start anew.

Unlocked

If you have an applet that requires both access to the Accessibility frameworks, and persistent property values, follow these instructions to sign the applet so that it works without requiring repeated re-authorization.

Important: Signing an applet using the following method introduces a security vulnerability that could allow malicious software to use Accessibility without user permission.

Download and install a property list file:

Applescript Run Application

  1. Click this link to download an archive of a special property list file (plist): ResourceRules-ignoring-Scripts.plist
  2. Unpack the archive, and place the downloaded property list file in the Preferences folder in your Library folder: (~/Library/Preferences)
    Tip: To access your Library folder, switch to the Finder, and hold down the Option key on your keyboard, and select Library from the Go menu.

Use the Terminal application to sign and register the applet:

  1. Launch the Terminal application
  2. Enter and execute the following command, substituting the correct actual path for both the property list file and the targeted AppleScript applet:
  3. codesign -s - --resource-rules=/Users/YourUserNameHere/Library/Preferences/ResourceRules-ignoring-Scripts.plist /path/to/applet.app

    NOTE: If you have your own signing identity, you may use that identity in place of “-” for the -s option.

(⬆ see above) The signing command string in a Terminal window. Note the use of the backslash character () to escape the space in the applet name. All spaces within paths must be escaped in the command string.

Applescript Run App When Unlocked Macbook Air

TOP CONTINUE

Coments are closed
Scroll to top