From 6843d95755d7147cdae25f96c9b9844d2cdd2da3 Mon Sep 17 00:00:00 2001 From: Ioan Lucut Date: Tue, 6 Aug 2024 13:42:24 +0300 Subject: [PATCH] feat(dispaly-switch): added AppleScript for toggling display configurations Included README with usage instructions and prerequisites. --- displays-switch/README.md | 39 ++++++++++++++++++++++ displays-switch/bes-display-switch.scpt | 43 +++++++++++++++++++++++++ 2 files changed, 82 insertions(+) create mode 100644 displays-switch/README.md create mode 100644 displays-switch/bes-display-switch.scpt diff --git a/displays-switch/README.md b/displays-switch/README.md new file mode 100644 index 0000000..3a0f109 --- /dev/null +++ b/displays-switch/README.md @@ -0,0 +1,39 @@ +# Display Configuration Toggle Script + +This AppleScript allows you to toggle between two different display configurations on macOS. It is designed to be used +with the `displayplacer` command-line tool. + +## Prerequisites + +- **`displayplacer`**: Ensure you have `displayplacer` installed. You can install it via Homebrew with the following + command: + + ```bash + brew install displayplacer + ``` + +## Script Overview + +### The script provides two display configurations: + + 1. Initial Configuration (example): + • Display 1: id:37D8832A-2D66-02CA-B9F7-8F30A301B230 at 2056x1329 resolution, 120Hz, 8-bit color depth, enabled, scaling on, origin at (0,0). + • Display 2: id:9261071D-C9FC-4476-9F8D-BC7B52CF4071 at 2560x1440 resolution, 60Hz, 8-bit color depth, enabled, scaling on, origin at (2056,0). + 2. Alternate Configuration (example): + • Display 1: id:9261071D-C9FC-4476-9F8D-BC7B52CF4071 at 2880x1620 resolution, 60Hz, 8-bit color depth, enabled, scaling on, origin at (0,0). + +### Using the Script + + 1. Open Script Editor: + • Open the Script Editor application on macOS. + 2. Paste the Script: + • Copy and paste the provided AppleScript code into Script Editor. + 3. Save the Script as an Application: + • Go to File -> Export. + • Set File Format to Application. + • Choose a name (e.g., “Display Config Toggle”) and location for your app, then click Save. + 4. Grant Necessary Permissions: + • Open System Preferences -> Security & Privacy -> Privacy tab. + • Select Full Disk Access or Automation, then add your new app if it’s not listed. + 5. Run the Application: + • Double-click the app to run it. You will see a dialog prompting you to choose between the “Initial” and “Alternate” configurations. diff --git a/displays-switch/bes-display-switch.scpt b/displays-switch/bes-display-switch.scpt new file mode 100644 index 0000000..7282878 --- /dev/null +++ b/displays-switch/bes-display-switch.scpt @@ -0,0 +1,43 @@ +-- Global Constants +property BES_DISPLAYPLACER_PATH : "/opt/homebrew/bin/displayplacer" +property BES_DEFAULT_PROPRESENTER_CONFIG_COMMAND : BES_DISPLAYPLACER_PATH & " 'id:4F494B73-B908-4053-8314-E259F924A0AA res:2560x1440 hz:60 color_depth:8 enabled:true scaling:on origin:(0,0) degree:0' 'id:98D31832-E235-4AAD-B122-81E6BC856D87 res:1920x1080 hz:60 color_depth:8 enabled:true scaling:off origin:(-1920,0) degree:0' 'id:8873492D-99CD-44AF-A7D0-F9C3F33212B8 res:1344x768 hz:60 color_depth:8 enabled:true scaling:off origin:(2560,0) degree:0'" +property BES_POWERPOINT_CONFIG_COMMAND : BES_DISPLAYPLACER_PATH & " 'id:4F494B73-B908-4053-8314-E259F924A0AA res:2560x1440 hz:60 color_depth:8 enabled:true scaling:on origin:(0,0) degree:0' 'id:8873492D-99CD-44AF-A7D0-F9C3F33212B8+98D31832-E235-4AAD-B122-81E6BC856D87 res:1344x768 hz:60 color_depth:8 enabled:true scaling:off origin:(2560,0) degree:0'" + +property BES_ERROR_SETTING_INITIAL_CONFIG_MESSAGE : "Eroare: " +property BES_ERROR_SETTING_ALTERNATE_CONFIG_MESSAGE : "Eroare: " +property BES_ERROR_IN_USER_CHOICE_DIALOG_MESSAGE : "Eroare: " +property BES_CHOOSE_DISPLAY_CONFIG_MESSAGE : "Alege configurația monitoarelor:" +property BES_PRO_PRESENTER_CFG_BUTTON : "Pro Presenter - Extend Display & Stage" +property BES_POWER_POINT_CFG_BUTTON : "PowerPoint - Mirror Display & Stage" +property BES_OK_BUTTON : "OK" + +-- Function to set the initial configuration (Pro Presenter Display + Stage) +on defaultProPresenterConfig() + try + do shell script BES_DEFAULT_PROPRESENTER_CONFIG_COMMAND + on error errMsg number errNum + display dialog BES_ERROR_SETTING_INITIAL_CONFIG_MESSAGE & errMsg & " (Error number: " & errNum & ")" buttons {BES_OK_BUTTON} default button BES_OK_BUTTON + end try +end defaultProPresenterConfig + +-- Function to set the alternate configuration +on setPowerPointConfig() + try + do shell script BES_POWERPOINT_CONFIG_COMMAND + on error errMsg number errNum + display dialog BES_ERROR_SETTING_ALTERNATE_CONFIG_MESSAGE & errMsg & " (Error number: " & errNum & ")" buttons {BES_OK_BUTTON} default button BES_OK_BUTTON + end try +end setPowerPointConfig + +-- Prompt user for configuration choice +try + set userChoice to display dialog BES_CHOOSE_DISPLAY_CONFIG_MESSAGE buttons {BES_PRO_PRESENTER_CFG_BUTTON, BES_POWER_POINT_CFG_BUTTON} default button BES_PRO_PRESENTER_CFG_BUTTON + + if button returned of userChoice is BES_PRO_PRESENTER_CFG_BUTTON then + defaultProPresenterConfig() + else if button returned of userChoice is BES_POWER_POINT_CFG_BUTTON then + setPowerPointConfig() + end if +on error errMsg number errNum + display dialog BES_ERROR_IN_USER_CHOICE_DIALOG_MESSAGE & errMsg & " (Error number: " & errNum & ")" buttons {BES_OK_BUTTON} default button BES_OK_BUTTON +end try