Tutorial

New updates and improvements to Roundfleet.

Important notice

The code examples and scripts provided in these tutorials are for educational purposes only. Roundfleet is not responsible for any issues, damages, or security vulnerabilities that may arise from the use, modification, or implementation of these examples. Always review and test code in a secure environment before using it in production systems.

Managing Screen Mirroring Icon in Mac Menu Bar

Screen Mirroring on Mac utilizes Apple's AirPlay technology to wirelessly display your Mac's screen content on compatible devices like Apple TV or AirPlay-enabled smart TVs. For administrators managing multiple Mac systems, controlling the visibility of the Screen Mirroring icon in the menu bar can help streamline the user interface and maintain consistency across devices.

This guide provides shell scripts to programmatically show or hide the Screen Mirroring icon in the Mac menu bar, particularly useful for fleet management and automated deployment scenarios.

Understanding Screen Mirroring

Screen Mirroring allows you to:

  • Display your Mac's screen on Apple TV or compatible smart TVs
  • Mirror presentations during meetings
  • Stream content to larger displays
  • Share your screen wirelessly within the same network

The Screen Mirroring option can be controlled through System Settings, but for automated management across multiple devices, shell scripts provide a more efficient solution.

Prerequisites

Before running these scripts, ensure you have:

  • Administrative privileges on the Mac
  • Terminal access
  • macOS 10.15 or later
  • Understanding of shell scripting basics

Script to Show Screen Mirroring Icon

This script makes the Screen Mirroring icon visible in the menu bar:

#!/bin/bash

# Get the current user and their UID
CurrentUser=$(ls -l /dev/console | awk '/ / { print $3 }')
CurrentUserUID=$(id -u "$CurrentUser")

# Enable Screen Mirroring in menu bar
launchctl asuser $CurrentUserUID sudo -iu "$CurrentUser" defaults write com.apple.airplay showInMenuBarIfPresent -int 1

echo "Screen Mirroring icon has been enabled in the menu bar"

What this script does:

  1. Identifies the current user: Uses /dev/console to determine who is currently logged in
  2. Gets user ID: Retrieves the numeric user ID for the current user
  3. Applies setting: Uses launchctl asuser to run the defaults command in the user's context
  4. Enables the icon: Sets showInMenuBarIfPresent to 1 to show the icon

Script to Hide Screen Mirroring Icon

This script hides the Screen Mirroring icon from the menu bar:

#!/bin/bash

# Get the current user and their UID
CurrentUser=$(ls -l /dev/console | awk '/ / { print $3 }')
CurrentUserUID=$(id -u "$CurrentUser")

# Disable Screen Mirroring in menu bar
launchctl asuser $CurrentUserUID sudo -iu "$CurrentUser" defaults write com.apple.airplay showInMenuBarIfPresent -int 0

echo "Screen Mirroring icon has been hidden from the menu bar"

What this script does:

  1. Identifies the current user: Same process as the show script
  2. Gets user ID: Retrieves the numeric user ID
  3. Applies setting: Uses launchctl asuser for proper user context
  4. Hides the icon: Sets showInMenuBarIfPresent to 0 to hide the icon

Usage Instructions

Running the Scripts

  1. Save the script to a file (e.g., show_screen_mirroring.sh or hide_screen_mirroring.sh)
  2. Make it executable:
    chmod +x show_screen_mirroring.sh
  3. Run the script:
    ./show_screen_mirroring.sh

Verification

After running either script, you can verify the changes:

  1. Check the menu bar: The Screen Mirroring icon should appear or disappear based on the script executed
  2. Check System Settings:
    • Go to System Settings > Control Center > Screen Mirroring
    • The "Always Show in Menu Bar" option should reflect the script's action

Advanced Usage

Checking Current Status

You can check the current status of the Screen Mirroring icon:

#!/bin/bash

CurrentUser=$(ls -l /dev/console | awk '/ / { print $3 }')
CurrentUserUID=$(id -u "$CurrentUser")

status=$(launchctl asuser $CurrentUserUID sudo -iu "$CurrentUser" defaults read com.apple.airplay showInMenuBarIfPresent 2>/dev/null)

if [ "$status" = "1" ]; then
    echo "Screen Mirroring icon is currently shown in menu bar"
elif [ "$status" = "0" ]; then
    echo "Screen Mirroring icon is currently hidden from menu bar"
else
    echo "Screen Mirroring menu bar setting is not configured"
fi

Compatibility Notes

  • macOS 12.0 and below: The setting is located in System Preferences > Dock & Menu Bar > AirPlay
  • macOS 13.0 and above: The setting is in System Settings > Control Center > Screen Mirroring
  • User Override: Users can manually change this setting from the System Settings, overriding the script's configuration

Best Practices

  1. Test First: Always test scripts on a single device before bulk deployment
  2. Backup Settings: Consider backing up current settings before making changes
  3. User Communication: Inform users about changes to their menu bar configuration
  4. Documentation: Keep records of which devices have which configurations
  5. Regular Audits: Periodically verify that settings remain as intended

Troubleshooting

Common Issues

  1. Permission Denied: Ensure the script has sudo privileges
  2. User Not Found: The script may fail if run when no user is logged in
  3. Setting Not Applied: Try logging out and back in to refresh the menu bar

Debugging

Add debugging to your scripts:

#!/bin/bash

# Enable debugging
set -x

CurrentUser=$(ls -l /dev/console | awk '/ / { print $3 }')
echo "Current user: $CurrentUser"

CurrentUserUID=$(id -u "$CurrentUser")
echo "User UID: $CurrentUserUID"

# Continue with the rest of the script...

Security Considerations

  • These scripts require administrative privileges
  • Always validate the source of scripts before execution
  • Consider implementing logging for audit trails
  • Test scripts in a controlled environment first

Conclusion

Managing the Screen Mirroring icon visibility in the Mac menu bar through shell scripts provides administrators with powerful tools for maintaining consistent user interfaces across their Mac fleet. Whether you're deploying to a single device or managing hundreds of Macs, these scripts offer a reliable, automated solution.

Remember to thoroughly test any scripts in your environment and consider the user experience when making interface changes. With proper implementation, you can streamline your Mac management workflow while maintaining the flexibility that users need for their daily work.

Related Tutorials

Set Screen Saver on macOS

Configure and deploy custom screen savers on Mac devices using scripts to manage slideshow styles, timing, and photo sources.

Jul 7, 2025

Menu Bar Customization and Icon Management on macOS

Customize menu bar icons, hide unwanted indicators, and manage user interface elements across macOS devices with enterprise-grade configuration management and user experience optimization.

Jul 7, 2025

Reducing Motion and Transparency on macOS Devices

Learn how to configure accessibility settings to reduce motion and transparency on macOS devices using shell scripts for better user experience and performance optimization.

Jul 7, 2025

Set Up Policy Banners on macOS Devices

Learn how to create, manage, and deploy interactive policy banners for macOS devices to ensure compliance and communicate organizational policies effectively.

Jul 7, 2025

Notification Management and User Experience Optimization on macOS

Advanced notification system management, productivity enhancement, distraction control, and enterprise communication policies for macOS devices with automated deployment and user experience analytics.

Jul 7, 2025

Finder Customization Management on macOS

Learn how to customize Finder preferences, desktop display options, file visibility settings, and interface elements on macOS devices using command-line scripts for fleet management.

Jul 7, 2025

Device Location Tracking on macOS

Learn how to track Mac device location using IP address geolocation, implement security monitoring, and manage fleet devices with location-based policies and compliance reporting.

Jul 7, 2025

Configure Stage Manager on macOS Devices

Learn how to enable, disable, and customize Stage Manager settings on macOS devices using shell scripts for Mac fleet management.

Jul 7, 2025