-
Notifications
You must be signed in to change notification settings - Fork 0
feat(dotfiles): add open command to launch applications from YAM… #159
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
shiron-dev
wants to merge
2
commits into
main
Choose a base branch
from
fix/login
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -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" | ||||||
|
|
@@ -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" | ||||||
| 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" | ||||||
|
|
@@ -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 | ||||||
|
|
@@ -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'}" # 末尾の改行コードを削除 | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [shfmt] reported by reviewdog 🐶
Suggested change
|
||||||
|
|
||||||
| 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" | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [shfmt] reported by reviewdog 🐶
Suggested change
|
||||||
| echo "✅ All specified applications have been launched." | ||||||
| else | ||||||
| echo "🚫 Operation cancelled." | ||||||
| fi | ||||||
| } | ||||||
|
|
||||||
| # --- メイン処理 --- | ||||||
| command="${1:-}" | ||||||
|
|
||||||
| case "$command" in | ||||||
|
|
@@ -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 | ||||||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[shfmt] reported by reviewdog 🐶