m

macos

自動檢測 macOS 並設置 iTerm2 偏好設定資料夾為自訂路徑。

0 0 0 1
1 #!/bin/sh
2
3 # 取得作業系統類型(例如 Darwin 表示 macOS)
4 system_type=$(uname -s)
5
6 # 如果是 macOS 系統,才執行以下區塊
7 if [ "$system_type" = "Darwin" ]; then
8
9 # 這裡可以考慮加入使用 Homebrew 安裝 iTerm2 的指令(例如 brew install --cask iterm2)
10 # 不過目前尚未實作
Last active 5 hours ago automation bash directory icloud macos script

這段腳本切換到使用者的 iCloud Drive 目錄 (Mobile Documents/com~apple~CloudDocs),確認目錄存在後切換到該目錄,並輸出當前工作目錄位置。

0 0 0 1
1 #!/bin/bash
2
3 BASE_DIR="$HOME/Library/Mobile Documents/com~apple~CloudDocs"
4
5 # 確認目錄是否存在
6 if [ -d "$BASE_DIR" ]; then
7 echo "切換到目標目錄: $BASE_DIR"
8 cd "$BASE_DIR" || { echo "無法進入目標目錄,退出腳本。"; exit 1; }
9 fi

這段 Python 程式碼利用 funcy 模組的裝飾器來 檢查目前作業系統,從而限制某些函式僅在特定平台上執行。具體來說,need_mac、need_linux、need_windows 與 need_unix 分別用來檢查是否在 macOS、Linux、Windows 或 Unix(macOS 或 Linux)系統上執行;函式 foo() 被 @need_unix 裝飾,僅允許在 Unix 系統上執行,而函式 bar() 則僅允許在 Windows 系統上執行。主程式透過 try-except 捕捉例外,若目前作業系統不符合要求,則輸出相應錯誤訊息。這樣的設計有助於確保平台專屬功能在正確的環境中執行,避免跨平台錯誤。

0 0 0 1
1 import platform
2 from funcy import decorator
3
4 @decorator
5 def need_mac(call):
6 """檢查系統是否為 macOS,若不是則拋出異常。"""
7 if platform.system() != "Darwin":
8 raise Exception("This functionality is only supported in macOS")
9 return call()