Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 64 additions & 8 deletions scripts/login_manager.bash
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ script_dir=$(cd "$(dirname "$0")" && pwd)
data_dir="${script_dir}/../data"
yaml_file="${data_dir}/login.yaml"

# --- 内部関数 ---
_get_current_items_as_yaml() {
local osascript_cmd='
tell application "System Events"
Expand All @@ -17,17 +18,20 @@ _get_current_items_as_yaml() {
return output
end tell'

if ! osascript -e "$osascript_cmd" 2> /dev/null \
| while IFS=$'\t' read -r path_val hidden_val; do
if ! osascript -e "$osascript_cmd" 2> /dev/null | while IFS=$'\t' read -r path_val hidden_val; do
if [[ -n "$path_val" ]]; then
path_val="${path_val%$'\r'}"
hidden_val="${hidden_val%$'\r'}"
printf -- "- path: %s\n hidden: %s\n" "$path_val" "$hidden_val"
Comment on lines +21 to 25
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[shfmt] reported by reviewdog 🐶

Suggested change
if ! osascript -e "$osascript_cmd" 2> /dev/null | while IFS=$'\t' read -r path_val hidden_val; do
if [[ -n "$path_val" ]]; then
path_val="${path_val%$'\r'}"
hidden_val="${hidden_val%$'\r'}"
printf -- "- path: %s\n hidden: %s\n" "$path_val" "$hidden_val"
if ! osascript -e "$osascript_cmd" 2>/dev/null | while IFS=$'\t' read -r path_val hidden_val; do
if [[ -n "$path_val" ]]; then
path_val="${path_val%$'\r'}"
hidden_val="${hidden_val%$'\r'}"
printf -- "- path: %s\n hidden: %s\n" "$path_val" "$hidden_val"

fi
done; then
fi
done; then
echo "🚨 Error: osascript failed to get login items." >&2
return 1
fi
}

# --- コマンド関数 ---

export_login_items() {
echo "🚀 Exporting current login items to $yaml_file..."
mkdir -p "$data_dir"
Expand Down Expand Up @@ -73,14 +77,22 @@ import_login_items() {
echo " - Adding items defined in YAML file..."
local path=""
local hidden=""
while IFS='' read -r line || [[ -n "$line" ]]; do

while IFS= read -r line || [[ -n "$line" ]]; do
if [[ "$line" == "- path: "* ]]; then
path="${line#*- path: }"
elif [[ "$line" == "hidden: "* ]]; then
path="${path%"${path##*[![:space:]]}"}"
path="${path%$'\r'}"

elif [[ "$line" == *"hidden: "* ]]; then
hidden="${line#*hidden: }"
hidden="${hidden#"${hidden%%[![:space:]]*}"}"
hidden="${hidden%$'\r'}"

if [[ -n "$path" && -n "$hidden" ]]; then
echo " - Adding: $path (hidden: $hidden)"
if ! osascript -e "tell application \"System Events\" to make new login item at end with properties {path:\"$path\", hidden:$hidden}"; then
local path_escaped="${path//\"/\\\"}"
if ! osascript -e "tell application \"System Events\" to make new login item at end with properties {path:\"$path_escaped\", hidden:$hidden}"; then
echo "🚨 Error: Failed to add login item: $path" >&2
exit 1
fi
Expand All @@ -93,6 +105,46 @@ import_login_items() {
echo "✅ Import complete. System login items have been synced."
}

# ✨ 新しく追加した機能 ✨
open_login_items() {
echo "📂 This will open all applications defined in $yaml_file."
if [[ ! -f "$yaml_file" ]]; then
echo "🚨 Error: YAML file not found. Please run 'export' first." >&2
exit 1
fi

# ユーザーに確認を求める (-n 1 で1文字だけ読み込む)
read -p "Are you sure you want to open all apps? (y/N) " -n 1 -r
echo # 改行して表示を整える

# 入力が 'y' または 'Y' の場合のみ処理を続行
if [[ $REPLY =~ ^[Yy]$ ]]; then
echo " - Opening applications..."
local path=""
while IFS= read -r line || [[ -n "$line" ]]; do
# 'path:' を含む行からパスを抽出
if [[ "$line" == "- path: "* ]]; then
path="${line#*- path: }"
path="${path%"${path##*[![:space:]]}"}" # 末尾の空白を削除
path="${path%$'\r'}" # 末尾の改行コードを削除
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[shfmt] reported by reviewdog 🐶

Suggested change
path="${path%$'\r'}" # 末尾の改行コードを削除
path="${path%$'\r'}" # 末尾の改行コードを削除


if [[ -n "$path" ]]; then
echo " - Opening: $path"
# 'open' コマンドでアプリケーションを起動
if ! open "$path"; then
echo "⚠️ Warning: Failed to open application: $path" >&2
fi
path="" # 次のアイテムのためにリセット
fi
fi
done < "$yaml_file"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[shfmt] reported by reviewdog 🐶

Suggested change
done < "$yaml_file"
done <"$yaml_file"

echo "✅ All specified applications have been launched."
else
echo "🚫 Operation cancelled."
fi
}

# --- メイン処理 ---
command="${1:-}"

case "$command" in
Expand All @@ -105,12 +157,16 @@ case "$command" in
import)
import_login_items
;;
open) # ✨ openコマンドを追加
open_login_items
;;
*)
echo "Usage: $0 {export|check|import}"
echo "Usage: $0 {export|check|import|open}"
echo
echo " export: Save current login items to ${yaml_file}"
echo " check: Compare current login items with the YAML file"
echo " import: Sync login items based on the YAML file (deletes all current items first)"
echo " open: Open all applications listed in the YAML file" # ✨ 説明を追加
exit 1
;;
esac
Loading