├── Connect Sidecar - Big Sur.applescript ├── Connect Sidecar - Catalina.applescript ├── Connect Sidecar - Monterey.applescript ├── LICENSE └── README.md /Connect Sidecar - Big Sur.applescript: -------------------------------------------------------------------------------- 1 | use AppleScript version "2.4" -- Yosemite (10.10) or later 2 | use scripting additions 3 | 4 | set deviceName to "My iPad" -- Change this to the name of your iPad. 5 | 6 | tell application "System Events" 7 | tell its application process "ControlCenter" 8 | -- Click the Control Center menu. 9 | click menu bar item "Control Center" of menu bar 1 10 | 11 | -- Give the window time to draw. 12 | delay 1 13 | 14 | -- Get all of the checkboxes in the Control Center menu. 15 | set ccCheckboxes to title of (every checkbox of group 1 of group 1 of window "Control Center") 16 | 17 | if ccCheckboxes contains "Connect to Sidecar" then 18 | -- If one of the checkboxes is named "Connect to Sidecar," click that checkbox. 19 | set sidecarToggle to checkbox "Connect to Sidecar" of group 1 of group 1 of window "Control Center" 20 | click sidecarToggle 21 | 22 | -- This opens a secondary window that contains the button to actually connect to Sidecar. Give the window time to draw. 23 | delay 1 24 | 25 | -- Get all of the checkboxes in the secondary menu. 26 | set displayCheckboxes to title of (every checkbox of group 1 of group 1 of window "Control Center") 27 | 28 | if displayCheckboxes contains deviceName then 29 | -- If one of the checkboxes has the same name as the iPad (`deviceName`), click that checkbox to connect to Sidecar. 30 | set deviceToggle to checkbox deviceName of group 1 of group 1 of window "Control Center" 31 | click deviceToggle 32 | 33 | -- Click the Control Center menu to close the secondary menu and return to the main menu. 34 | click menu bar item "Control Center" of menu bar 1 35 | 36 | -- Click the Control Center menu again to close the main menu. 37 | click menu bar item "Control Center" of menu bar 1 38 | end if 39 | else 40 | repeat with cb in ccCheckboxes 41 | if cb contains "Disconnect" then 42 | -- If one of the checkboxes has "Disconnect" in its name, Sidecar is already connected. 43 | -- Click the checkbox to disconnect Sidecar. 44 | set sidecarToggle to ((checkbox 1 of group 1 of group 1 of window "Control Center") whose title contains "Disconnect") 45 | click sidecarToggle 46 | 47 | -- Click the Control Center menu again to close the main menu. 48 | click menu bar item "Control Center" of menu bar 1 49 | end if 50 | end repeat 51 | end if 52 | end tell 53 | end tell -------------------------------------------------------------------------------- /Connect Sidecar - Catalina.applescript: -------------------------------------------------------------------------------- 1 | use AppleScript version "2.4" -- Yosemite (10.10) or later 2 | use scripting additions 3 | 4 | set deviceName to "My iPad" -- Change this to the name of your iPad. 5 | 6 | tell application "System Events" 7 | tell process "SystemUIServer" 8 | click (menu bar item 1 of menu bar 1 whose description contains "Displays") 9 | set displaymenu to menu 1 of result 10 | if ((menu item 1 where its name starts with deviceName) of displaymenu) exists then 11 | -- Not connected, so click the name of the device to connect. 12 | click ((menu item 1 where its name starts with deviceName) of displaymenu) 13 | else 14 | -- Connected, so click "Disconnect" to disconnect. 15 | click ((menu item 1 where its name starts with "Disconnect") of displaymenu) 16 | end if 17 | end tell 18 | end tell -------------------------------------------------------------------------------- /Connect Sidecar - Monterey.applescript: -------------------------------------------------------------------------------- 1 | use AppleScript version "2.4" -- Yosemite (10.10) or later 2 | use scripting additions 3 | 4 | set deviceName to "My iPad" -- Change this to the name of your iPad. 5 | 6 | tell application "System Events" 7 | tell its application process "ControlCenter" 8 | -- Click the Control Center menu. 9 | click menu bar item "Control Center" of menu bar 1 10 | 11 | -- Give the window time to draw. 12 | delay 1 13 | 14 | -- Get all of the checkboxes in the Control Center menu. 15 | set ccCheckboxes to name of (every checkbox of window "Control Center") 16 | 17 | if ccCheckboxes contains "Connect to Sidecar" then 18 | -- If one of the checkboxes is named "Connect to Sidecar," click that checkbox. 19 | set sidecarToggle to checkbox "Connect to Sidecar" of window "Control Center" 20 | click sidecarToggle 21 | 22 | -- This opens a secondary window that contains the button to actually connect to Sidecar. Give the window time to draw. 23 | delay 1 24 | 25 | -- In masOS Monterey, the Sidecar device toggle (checkbox) is inside of a scroll area. 26 | -- Rather than assume that it's in scroll area 1, get all of the scroll areas, loop through them, and find the device toggle. 27 | set scrollAreas to (every scroll area of window "Control Center") 28 | set saCounter to 1 29 | set displayCheckboxes to "" 30 | 31 | repeat with sa in scrollAreas 32 | set displayCheckboxes to name of (every checkbox of sa) 33 | 34 | if displayCheckboxes contains deviceName then 35 | -- Device toggle found. 36 | exit repeat 37 | end if 38 | 39 | -- We didn't find the device toggle. Try the next scroll area. 40 | set saCounter to saCounter + 1 41 | end repeat 42 | 43 | if displayCheckboxes contains deviceName then 44 | -- If we found the a checkbox with the iPad's name, `saCounter` tells us which scroll area contains the Sidecar toggle. 45 | set deviceToggle to checkbox deviceName of scroll area saCounter of window "Control Center" 46 | 47 | -- Click the toggle to connect Sidecar. 48 | click deviceToggle 49 | 50 | -- Click the Control Center menu to close the secondary menu and return to the main menu. 51 | click menu bar item "Control Center" of menu bar 1 52 | 53 | -- Click the Control Center menu again to close the main menu. 54 | click menu bar item "Control Center" of menu bar 1 55 | else 56 | -- Sidecar is available, but no devices with deviceName were found. 57 | display dialog "The device " & deviceName & " can't be found. Please verify the name of your iPad and update the `deviceName` variable if necessary." 58 | end if 59 | else 60 | -- A checkbox named "Connect to Sidecar" wasn't found. 61 | set isConnected to false 62 | repeat with cb in ccCheckboxes 63 | -- Loop through the checkboxes and determine if Sidecar is already connected. 64 | if cb contains "Disconnect" then 65 | -- If one of the checkboxes has "Disconnect" in its name, Sidecar is already connected. 66 | -- Break out of the loop. 67 | set isConnected to true 68 | exit repeat 69 | end if 70 | end repeat 71 | 72 | if isConnected is equal to true then 73 | -- Click the checkbox to disconnect Sidecar. 74 | set sidecarToggle to ((checkbox 1 of window "Control Center") whose name contains "Disconnect") 75 | click sidecarToggle 76 | 77 | -- Click the Control Center menu again to close the main menu. 78 | click menu bar item "Control Center" of menu bar 1 79 | else 80 | -- Sidecar isn't connected, and no devices are available to connect to. Show an error message. 81 | display dialog "No Sidecar devices are in range." 82 | end if 83 | end if 84 | end tell 85 | end tell -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2021 Geoff Taylor 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | **Note: I'm no longer updating this script, so I've archived the repository. I don't use it anymore, and it's too difficult to keep up with the changes to Control Center in each macOS release. I'm glad so many of you found it useful.** 2 | 3 | # Connect Sidecar 4 | Connect Sidecar is a script that connects your Mac to [Sidecar](https://support.apple.com/en-us/HT210380). If your Mac is already connected to Sidecar, the script will disconnect it. 5 | 6 | The script is written in AppleScript. It uses [UI scripting](https://developer.apple.com/library/archive/documentation/LanguagesUtilities/Conceptual/MacAutomationScriptingGuide/AutomatetheUserInterface.html) to click the Sidecar button in the correct menu (*Displays* in Catalina or *Control Center* in Bug Sur). The primary advantage of the script is that you can assign a keyboard shortcut to it (for example, using [Keyboard Maestro](https://www.keyboardmaestro.com/main/) or [FastScripts](https://redsweater.com/fastscripts/)). You can then connect or disconnect using the keyboard shortcut rather than clicking the menu item. 7 | 8 | ## Setup 9 | Download the latest zip file from [this page](https://github.com/geofftaylor/connect-sidecar/releases/latest). Extract the files from the archive. Select the `.applescript` file that is appropriate for your version of macOS (Monterey, Big Sur or Catalina). 10 | 11 | Open the script in Script Editor. (Script Editor is located in `/Applications/Utilities`.) Go to this line: 12 | 13 | `set deviceName to "My iPad" -- Change this to the name of your iPad.` 14 | 15 | Change `"My iPad"` to the name of your iPad, make sure it's enclosed in quotation marks, and save the file. (You can find your iPad's name under **Settings** > **General** > **About** > **Name**.) 16 | 17 | Save the script in your user scripts folder (`/Users//Library/Scripts`) or another location of your choosing. 18 | 19 | ## Usage 20 | There are many ways you could use the script; here are a few options. 21 | 22 | ### Script Menu 23 | If you saved the script in your user scripts folder (`/Users//Library/Scripts`), it will be available in the [Script Menu](https://support.apple.com/guide/script-editor/access-scripts-using-the-script-menu-scpedt27975/mac). 24 | 25 | ### Keyboard Maestro 26 | Create a macro with an *Execute AppleScript* action. Set the action to *Execute script file* and enter the path to the script in the *Script file* box. Then assign a keyboard shortcut. 27 | 28 | ### FastScripts 29 | If you saved the script in your user scripts folder (`/Users//Library/Scripts`), it will be available in the FastScripts menu, and you can assign a keyboard shortcut in FastScript's preferences. 30 | 31 | ## Notes 32 | * This script will probably break with each major version of macOS. It may break with minor versions as well. 33 | * I'll attempt to update the script when new macOS versions are released, but it may take some time. --------------------------------------------------------------------------------