Background
Starting from macOS Catalina and iOS 13, Apple offers the ability to
use an iPad as a secondary screen for your MacBook. This is very handy ! Problem ? You need to navigate through the menu and a lot of extra clicks are needed. Can we use some clever tips to automate that ? I think so.
The Steps
The functionality is called Sidecar and you can enable it in the configuration panel, Display, (+) sign and choose your iPad in the drop down list.
It requires 4 clicks and many mouse movements. You will probably say "so what ?". Well, when you have your hands on the keyboard this might be annoying. So I wanted to investigate a way to do the same from the command line.
AppleScript
Apple allows the user to create any script to automate whatever tasks with the system. It is handy and simulate all activities done by a human via scripts. I came across a
Reddit article touching base that topic. I decided to give it a go.
Here is the script I manage to create :
tell application "System Settings"
activate
reveal pane id "com.apple.Displays-Settings.extension"
end tell
-- Wait for the Displays pane to load
delay 3 -- Adjust delay if necessary based on your system speed
tell application "System Events"
try
tell application process "System Settings"
-- Ensure Displays pane is loaded
if not (exists window 1) then
error "System Settings window not found."
end if
-- Wait for the correct group hierarchy to load
set timeoutSeconds to 10
set startTime to current date
repeat
if (exists group 1 of group 2 of splitter group 1 of group 1 of window 1) then
exit repeat
end if
if ((current date) - startTime > timeoutSeconds) then
error "Displays pane not fully loaded."
end if
delay 0.5
end repeat
-- Locate and interact with the `+` dropdown menu
tell group 1 of group 2 of splitter group 1 of group 1 of window 1
if not (exists pop up button 1) then
error "Pop-up button not found in Displays pane."
end if
click pop up button 1
delay 2 -- Wait for the dropdown menu to appear
-- Ensure the dropdown menu is fully loaded
set timeoutSeconds to 5
set startTime to current date
repeat
if (exists menu 1 of pop up button 1) then
exit repeat
end if
if ((current date) - startTime > timeoutSeconds) then
error "Dropdown menu did not appear."
end if
delay 0.5
end repeat
-- Select "iPad de Frédéric" from the dropdown menu
tell menu 1 of pop up button 1
set found to false
repeat with aMenuItem in menu items
if name of aMenuItem contains "Mirror or Extend to" then
set found to true
end if
if name of aMenuItem contains "iPad de Frédéric" and found then
click aMenuItem
set found to true
exit repeat
end if
end repeat
if not found then
error "iPad de Frédéric not found in the dropdown menu."
end if
end tell
end tell
end tell
-- Close System Settings
tell application "System Settings" to close window 1
on error errMsg
-- Log error for debugging
log "Error: " & errMsg
end try
end tell
⚠️ Note : the script above only works with macOS language sets to English. Indeed, it is reading the menu label and if your language is set to something else, the script won't find the text label and will obviously fail. Also, adjust the iPad name to match yours.
This script works when I execute it from the AppleScript UI. What's next ? There is a magic command from the CLI called osascript :
OSASCRIPT(1) General Commands Manual OSASCRIPT(1)
NAME
osascript – execute OSA scripts (AppleScript, JavaScript, etc.)
SYNOPSIS
osascript [-l language] [-i] [-s flags] [-e statement | programfile] [argument ...]
DESCRIPTION
osascript executes the given OSA script, which may be plain text or a compiled script (.scpt) created by Script Editor or osacompile(1). By default, osascript treats plain text as AppleScript, but
you can change this using the -l option. To get a list of the OSA languages installed on your system, use osalang(1).
When issued from the CLI passing the Apple script file as an argument :
❯ osascript Sidecar-iPad.scpt
It actually connect (or disconnect) the iPad as a Sidecar extended screen !
Ok, we have achieve what I'm calling the bare minimal regarding the automation we are looking for. The only downside is : we have to type few commands. It already a nice achievement, but we can do more !
I created a little bash script to simplify it even more :
❯ cat ipad.sh
#!/bin/bash
echo "Connecting iPad..."
osascript $HOME/MyProjects/AppleScript/Sidecar-iPad.scpt
echo "Done."
echo
We know what commands can trigger the Sidecar connectivity, but it would be awesome to create a keyboard shortcut. MacOS allows creation of custom keyboard shortcuts, but at this point I could not make it work. As soon as I have more details, I'll share them as always.
I hope this helps !
Comments
Post a Comment
Thank you for your message, it has been sent to the moderator for review...