macOS Finder菜单 在终端中打开 替换为ghostty

原理是在 .zshrc 里 编写脚本检测当前的终端类型。 如果不是 ghostty 的话,直接呼起ghostty。 脚本如下(建议贴在最顶端) # Redirect Finder-opened Apple Terminal sessions into Ghostty. if [[ -o intera...
macOS Finder菜单 在终端中打开 替换为ghostty
macOS Finder菜单 在终端中打开 替换为ghostty

原理是在 .zshrc 里 编写脚本检测当前的终端类型。
如果不是 ghostty 的话,直接呼起ghostty。

脚本如下(建议贴在最顶端)

# Redirect Finder-opened Apple Terminal sessions into Ghostty.
if [[ -o interactive && "${TERM_PROGRAM:-}" == "Apple_Terminal" && -z "${GHOSTTY_TERMINAL_TAKEOVER_DONE:-}" ]]; then
  export GHOSTTY_TERMINAL_TAKEOVER_DONE=1
  __ghostty_app="/Applications/Ghostty.app"
  __ghostty_lock_file="${TMPDIR:-/tmp}/ghostty-terminal-takeover-${UID}.lock"

  : >> "$__ghostty_lock_file" 2>/dev/null
  if ! zmodload zsh/system 2>/dev/null || ! zsystem flock -t 0 -f __ghostty_lock_fd "$__ghostty_lock_file" 2>/dev/null; then
    exit 0
  fi

  if [[ -d "$__ghostty_app" ]]; then
    if /usr/bin/osascript - "$PWD" <<'APPLESCRIPT'
on run argv
  set targetDir to item 1 of argv

  tell application "/Applications/Ghostty.app"
    set cfg to new surface configuration
    set initial working directory of cfg to targetDir

    if (count of windows) is 0 then
      set win to new window with configuration cfg
      set targetTab to selected tab of win
    else
      set win to front window
      set targetTab to new tab in win with configuration cfg
      select tab targetTab
    end if

    activate window win
    focus (focused terminal of targetTab)
  end tell
end run
APPLESCRIPT
    then
      {
        sleep 1.2
        /usr/bin/defaults write com.apple.Terminal NSQuitAlwaysKeepsWindows -bool false >/dev/null 2>&1
        /usr/bin/defaults write com.apple.Terminal ApplePersistenceIgnoreState -bool true >/dev/null 2>&1
        /usr/bin/osascript >/dev/null 2>&1 <<'APPLESCRIPT'
tell application "Terminal"
  close every window saving no
end tell
APPLESCRIPT
        sleep 0.2
        /usr/bin/pkill -x Terminal >/dev/null 2>&1
      } &!
      exit 0
    fi
  fi

  unset GHOSTTY_TERMINAL_TAKEOVER_DONE
  unset __ghostty_app
  unset __ghostty_lock_file
  unset __ghostty_lock_fd
fi

注意

Apple Terminal会在被kill后重新启动时恢复session,如果出现了使用此指令打开了多个 ghostly窗口的问题,可以使用如下命令行修复:

defaults write com.apple.Terminal NSQuitAlwaysKeepsWindows -bool false
defaults write com.apple.Terminal ApplePersistenceIgnoreState -bool true

3 个帖子 - 3 位参与者

阅读完整话题

来源: linux.do查看原文