displayplacer screen resolutions, mirror display settings and configurations

Hasibravo
New Contributor III

This script automates the configuration of display settings on macOS by checking the system architecture, downloading and installing the correct version of `displayplacer`, and setting up display mirroring with specified parameters. It ensures compatibility with ARM64 and Intel systems while optimizing display settings such as resolution, refresh rate, color depth, and scaling. The script uses JAMF parameters to adjust these settings dynamically.

Feel free to adjust the description if you have any specific points you'd like to highlight!

DisplayPlacer: https://github.com/jakehilborn/displayplacer

 

#!/bin/bash

# -----------------------------------------------------------------------------
# Script Name:     DisplayPlacer.sh
# Author:          [Muhammad Hasib]
# Created:         25Jul2024
# Last Modified:   25Jul2024
# Description:     This script checks the system architecture, downloads and
#                  installs the appropriate version of displayplacer, and 
#                  configures display settings for mirroring built-in and 
#                  external screens with specified resolution, refresh rate, 
#                  color depth, and scaling.
#                  It will 'OPTIMISE FOR' with an external display.	
# -----------------------------------------------------------------------------


# Define variables for display settings
RESOLUTION="1920x1080"
HZ="60"
COLOR_DEPTH="8"
SCALING="off"

# Define paths
DISPLAYPLACER_PATH="/usr/local/bin/displayplacer"
DISPLAYPLACER_URL_APPLE="https://github.com/jakehilborn/displayplacer/releases/download/v1.4.0/displayplacer-apple-v140"
DISPLAYPLACER_URL_INTEL="https://github.com/jakehilborn/displayplacer/releases/download/v1.4.0/displayplacer-intel-v140"

# Determine the architecture
ARCH=$(uname -m)
if [[ "$ARCH" == "arm64" ]]; then
    DISPLAYPLACER_URL="$DISPLAYPLACER_URL_APPLE"
else
    DISPLAYPLACER_URL="$DISPLAYPLACER_URL_INTEL"
fi

# Download and install displayplacer if not found
if [ ! -f "$DISPLAYPLACER_PATH" ]; then
    echo "displayplacer not found. Downloading and installing..."
    curl -L "$DISPLAYPLACER_URL" -o /tmp/displayplacer
    chmod +x /tmp/displayplacer
    sudo mv /tmp/displayplacer "$DISPLAYPLACER_PATH"
    echo "displayplacer installed at $DISPLAYPLACER_PATH."
fi

# List current display configurations and store the output
DISPLAY_CONFIG=$($DISPLAYPLACER_PATH list)

# Initialize arrays
BUILT_IN_ID=""
EXTERNAL_ID=""

# Process the display configurations
while read -r line; do
    if [[ "$line" =~ Persistent\ screen\ id:\ ([^ ]+) ]]; then
        PERSISTENT_ID="${BASH_REMATCH[1]}"
    elif [[ "$line" =~ Type:\ (.+) ]]; then
        TYPE="${BASH_REMATCH[1]}"
        
        # Assign persistent ID based on screen type
        if [[ "$TYPE" == *"built in"* ]]; then
            BUILT_IN_ID="$PERSISTENT_ID"
        else
            EXTERNAL_ID="$PERSISTENT_ID"
        fi
    fi
done <<< "$DISPLAY_CONFIG"

# Check if both screen IDs are found
if [ -z "$BUILT_IN_ID" ] || [ -z "$EXTERNAL_ID" ]; then
    echo "Could not find both built-in and external screens."
    exit 1
fi

# Debug: print extracted persistent ids
echo "Built-in screen ID: $BUILT_IN_ID"
echo "External screen ID: $EXTERNAL_ID"

# Construct the displayplacer command for mirroring
CMD="$DISPLAYPLACER_PATH"
CMD+=" \"id:$EXTERNAL_ID+${BUILT_IN_ID} res:$RESOLUTION hz:$HZ color_depth:$COLOR_DEPTH enabled:true scaling:$SCALING\""

# Debug: print the command that will be executed
echo "Executing command: $CMD"

# Execute the constructed command
eval $CMD

 

1 REPLY 1

fgonzale
Contributor

This works great! Though I did have to make a small addition to the script as the path "/usr/local/bin" doesn't usually exist in a clean state (at least not in Sonoma). So I added a mkdir /usr/local/bin line

mkdir /usr/local/bin

 

Modified script:

#!/bin/bash

# -----------------------------------------------------------------------------
# Script Name:     DisplayPlacer.sh
# Author:          [Muhammad Hasib]
# Created:         25Jul2024
# Last Modified:   25Jul2024
# Description:     This script checks the system architecture, downloads and
#                  installs the appropriate version of displayplacer, and 
#                  configures display settings for mirroring built-in and 
#                  external screens with specified resolution, refresh rate, 
#                  color depth, and scaling.
#                  It will 'OPTIMISE FOR' with an external display.	
# -----------------------------------------------------------------------------


# Define variables for display settings
RESOLUTION="1920x1080"
HZ="60"
COLOR_DEPTH="8"
SCALING="off"

# Define paths
DISPLAYPLACER_PATH="/usr/local/bin/displayplacer"
DISPLAYPLACER_URL_APPLE="https://github.com/jakehilborn/displayplacer/releases/download/v1.4.0/displayplacer-apple-v140"
DISPLAYPLACER_URL_INTEL="https://github.com/jakehilborn/displayplacer/releases/download/v1.4.0/displayplacer-intel-v140"

# Determine the architecture
ARCH=$(uname -m)
if [[ "$ARCH" == "arm64" ]]; then
    DISPLAYPLACER_URL="$DISPLAYPLACER_URL_APPLE"
else
    DISPLAYPLACER_URL="$DISPLAYPLACER_URL_INTEL"
fi

# Download and install displayplacer if not found
if [ ! -f "$DISPLAYPLACER_PATH" ]; then
    echo "displayplacer not found. Downloading and installing..."
    curl -L "$DISPLAYPLACER_URL" -o /tmp/displayplacer
    chmod +x /tmp/displayplacer
    mkdir /usr/local/bin
    sudo mv /tmp/displayplacer "$DISPLAYPLACER_PATH"
    echo "displayplacer installed at $DISPLAYPLACER_PATH."
fi

# List current display configurations and store the output
DISPLAY_CONFIG=$($DISPLAYPLACER_PATH list)

# Initialize arrays
BUILT_IN_ID=""
EXTERNAL_ID=""

# Process the display configurations
while read -r line; do
    if [[ "$line" =~ Persistent\ screen\ id:\ ([^ ]+) ]]; then
        PERSISTENT_ID="${BASH_REMATCH[1]}"
    elif [[ "$line" =~ Type:\ (.+) ]]; then
        TYPE="${BASH_REMATCH[1]}"
        
        # Assign persistent ID based on screen type
        if [[ "$TYPE" == *"built in"* ]]; then
            BUILT_IN_ID="$PERSISTENT_ID"
        else
            EXTERNAL_ID="$PERSISTENT_ID"
        fi
    fi
done <<< "$DISPLAY_CONFIG"

# Check if both screen IDs are found
if [ -z "$BUILT_IN_ID" ] || [ -z "$EXTERNAL_ID" ]; then
    echo "Could not find both built-in and external screens."
    exit 1
fi

# Debug: print extracted persistent ids
echo "Built-in screen ID: $BUILT_IN_ID"
echo "External screen ID: $EXTERNAL_ID"

# Construct the displayplacer command for mirroring
CMD="$DISPLAYPLACER_PATH"
CMD+=" \"id:$EXTERNAL_ID+${BUILT_IN_ID} res:$RESOLUTION hz:$HZ color_depth:$COLOR_DEPTH enabled:true scaling:$SCALING\""

# Debug: print the command that will be executed
echo "Executing command: $CMD"

# Execute the constructed command
eval $CMD

 

probably could have made the new mkdir part a conditional statement but if the directory already exists then mkdir wont do anything