今天,我听了 Apptories 的最新一期:“实用工具大全回归”。 @viticci想知道如何根据当前屏幕显示或隐藏菜单栏中的图标。以下是我使用 Bartender 的操作方法:当我只使用 MacBook Pro 屏幕时隐藏图标,而当我在家或工作时使用大屏幕时显示所有图标。
免责声明
由于不太熟悉硬件相关的 Bash 脚本,我承认这些脚本是在人工智能的帮助下生成的。但它确实帮我完成了工作。可能仍然存在一些错误,可能需要进行一些调整。
触发因素
我有两个触发器:一个用于检测是否只有 MacBook Pro 屏幕处于活动状态,另一个用于检测是否有其他屏幕连接到计算机。
如果仅使用 MacBook Pro 屏幕,则检测脚本。
#!/bin/zsh # Script to detect if ONLY the built-in MacBook Pro screen is being used # Returns "true" if only the built-in screen is active, "false" if an external monitor is connected # Get complete information about the screens DISPLAY_INFO=$(system_profiler SPDisplaysDataType 2>/dev/null) # Count the total number of active screens ACTIVE_DISPLAYS=$(echo "$DISPLAY_INFO" | grep -c "Resolution:") # Count internal screens INTERNAL_DISPLAYS=$(echo "$DISPLAY_INFO" | grep -c "Connection Type: Internal") # Check if ONLY the built-in screen is being used if [[ $ACTIVE_DISPLAYS -eq 1 ]] && [[ $INTERNAL_DISPLAYS -eq 1 ]]; then echo "true" exit 0 else echo "false" exit 1 fi
如果连接了附加屏幕,则检测脚本。
#!/bin/zsh # Script to detect if an external screen is being used # Returns "true" if at least one external screen is being used, "false" otherwise # Get complete information about the screens DISPLAY_INFO=$(system_profiler SPDisplaysDataType 2>/dev/null) # Count the total number of active screens ACTIVE_DISPLAYS=$(echo "$DISPLAY_INFO" | grep -c "Resolution:") # Count internal screens INTERNAL_DISPLAYS=$(echo "$DISPLAY_INFO" | grep -c "Connection Type: Internal") # Calculate the number of external screens (total - internal) EXTERNAL_DISPLAYS=$((ACTIVE_DISPLAYS - INTERNAL_DISPLAYS)) # For debugging (comment these lines for normal use) # echo "ACTIVE_DISPLAYS: $ACTIVE_DISPLAYS" # echo "INTERNAL_DISPLAYS: $INTERNAL_DISPLAYS" # echo "EXTERNAL_DISPLAYS: $EXTERNAL_DISPLAYS" # Check if at least one external screen is being used if [[ $EXTERNAL_DISPLAYS -gt 0 ]]; then echo "true" exit 0 else echo "false" exit 1 fi
预设
上面提到的两个脚本会根据屏幕是否连接来激活两个预设。一个预设我称之为“小屏幕”,它会隐藏所有图标;另一个预设我称之为“大屏幕”,它会默认显示所有内容。