Tutorial

Nuevas actualizaciones y mejoras para Roundfleet.

Aviso importante

Los ejemplos de código y scripts proporcionados en estos tutoriales son solo para propósitos educativos. Roundfleet no es responsable de ningún problema, daño o vulnerabilidad de seguridad que pueda surgir del uso, modificación o implementación de estos ejemplos. Siempre revisa y prueba el código en un entorno seguro antes de usarlo en sistemas de producción.

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.