MacOS を快適に使うための設定

なお、時間ベースの自動実行については、Launchd 参照のこと。

スクリプト実行権限の変更

ここで書いた設定ファイルは実行可能(u)にしておく必要があるので,編集したら下記を実行する。

find ~/.config -type f | rg -v 'htop|.DS_Store' | rg 'rc$|.sh$' | xargs chmod u+x

mosh

フォルダをつくる

mkdir -p .config/yabai
mkdir -p .config/skhd
mkdir -p .config/scripts
mkdir -p .config/sketchybar/plugins
mkdir -p .config/zathura
mkdir -p .config/neomutt
mkdir -p .config/lf

Yabai

mac の window manager の yabai。インストールがちょっとめんどいんだけど、一番安定しているので使っている。

install from HEAD に慣れたほうがよさそう。 Ventura でも使えてる。

sha も変るだろうから、 /private/etc/sudoers.d/yabai もかえる必要ありだとおもう。

sudo yabai --load-sa
yabai -m signal --add event=dock_did_restart action="sudo yabai --load-sa"

yabai -m config layout bsp

yabai -m config window_gap 7
yabai -m config top_padding 5
yabai -m config bottom_padding 5
yabai -m config left_padding 5
yabai -m config right_padding 5

yabai -m config window_border on
yabai -m config window_border_width 2
yabai -m config active_window_border_color 0xffea9a97

yabai -m shadow off
yabai -m config window_opacity on
yabai -m config active_window_opacity 0.97
yabai -m config normal_window_opacity 0.85

yabai -m rule --add app="^System Preferences$" manage=off
yabai -m rule --add app="^System Settings$" manage=off
yabai -m rule --add app="^Spotify$" manage=off
yabai -m rule --add app="^Finder$" manage=off
yabai -m rule --add app="^Installer$" manage=off
yabai -m rule --add app="^Steam$" manage=off
yabai -m rule --add app="^Stickies$" manage=off
yabai -m rule --add app="^Paradox Launcher$" manage=off
yabai -m rule --add app="^FaceTime$" manage=off
yabai -m rule --add app="^Preview$" manage=on

外部ディスプレイ対応

内蔵ディスプレイはノッチ部分を含めない設定でいけるので、(ノッチ下端からの padding) 上でいいんだけど、外部ディスプレイに接続すると、加味されないから、ウィンドウが sketchybar とかぶる。その場合は、 yabai に下記を送ってやれば良い。画面解像度の関係で、mac 本来のトップバーとあわなくなるけど、まあいいでしょう。

if [[ $(system_profiler SPDisplaysDataType | rg Internal | wc -l | tr -d " \t") == "0" ]] ; then
    #echo "clam"
    yabai -m config top_padding 29
fi

差し替えた時用

brew services restart sketchybar
yabai --start-service

rescreen コマンドとして書いてある。

skhd

同じ作者による、key bind をいじる skhd。特に良く使うアプリケーション、ターミナルと emacs とブラウザに関しては、直接開くようにしてる。起動スクリプトは後述。

kinesis 360 Pro のために、 meh(cmd, alf, ctrl) を追加した。

# for the built-in keyboard
# cmd - return : "$HOME"/.config/skhd/open_iterm2.sh
rcmd - sound_up : "$HOME"/.config/skhd/open_emacsclient.sh
rcmd - sound_down : "$HOME"/.config/skhd/open_chromium.sh
rcmd - mute : "$HOME"/.config/skhd/open_iterm2.sh

# for the HHKB
ralt - 0x2A : "$HOME"/.config/skhd/open_emacsclient.sh
ralt - 0x18 : "$HOME"/.config/skhd/open_chromium.sh
ralt - 0x1B : "$HOME"/.config/skhd/open_iterm2.sh

# TODO: this I need to think about it...
meh - g : "$HOME"/.config/org-agenda.sh
meh - f : "$HOME"/.config/org-capture-task.sh
meh - d : "$HOME"/.config/skhd/open_emacsclient.sh
meh - s : "$HOME"/.config/skhd/open_chromium.sh
meh - a : "$HOME"/.config/skhd/open_iterm2.sh

skhd -o (ctrl + c で解除) 参考

メインの機能は yabai を操作することになります。どの領域にフォーカスするのかは、 vim に準じています。デスクトップの切り換えは、 ctrl にしていて。今フォーカスしているアプリを別のデスクトップに飛したい場合は shift

cmd - l            : yabai -m window --focus east
cmd - k            : yabai -m window --focus north
cmd - j            : yabai -m window --focus south
cmd - h            : yabai -m window --focus west

ctrl - 1            : yabai -m space --focus 1 
ctrl - 2            : yabai -m space --focus 2 
ctrl - 3            : yabai -m space --focus 3 
ctrl - 4            : yabai -m space --focus 4 
ctrl - 5            : yabai -m space --focus 5 

ctrl - h            : yabai -m space --focus prev || yabai -m space --focus last && yabai -m window --focus first
ctrl - l            : yabai -m space --focus next || yabai -m space --focus first && yabai -m window --focus first

shift + ctrl - 1 : yabai -m window --space 1 && yabai -m space --focus 1
shift + ctrl - 2 : yabai -m window --space 2 && yabai -m space --focus 2
shift + ctrl - 3 : yabai -m window --space 3 && yabai -m space --focus 3
shift + ctrl - 4 : yabai -m window --space 4 && yabai -m space --focus 4
shift + ctrl - 5 : yabai -m window --space 5 && yabai -m space --focus 5

meh - j             : yabai -m window --focus south
meh - k             : yabai -m window --focus north
meh - l             : yabai -m window --focus east
meh - h             : yabai -m window --focus west

meh - 0x28            : yabai -m space --focus 1
meh - 0x04             : yabai -m space --focus 2
meh - 0x2B             : yabai -m space --focus 3
meh - 0x2F             : yabai -m space --focus 4
meh - 0x2C            : yabai -m space --focus 5

hyper - 0x28            : yabai -m window --space 1 && yabai -m space --focus 1
hyper - 0x04             : yabai -m window --space 2 && yabai -m space --focus 2
hyper - 0x2B             : yabai -m window --space 3 && yabai -m space --focus 3
hyper - 0x2F           : yabai -m window --space 4 && yabai -m space --focus 4
hyper - 0x2C            : yabai -m window --space 5 && yabai -m space --focus 5

フルスクリーンにする:

alt - f            : yabai -m window --toggle zoom-fullscreen

アプリの起動

沢山インスタンスを立ちあげて Dock が汚なくなったり、そもそも同じアプリの複数同時起動に制限がかかっていたりするらしいので、アプリ自体はひとつで、新しい window をつくるのには下記のスクリプトを使わないといけない。

(ungoogled) chromium

普通の chromium だったとしても google のサービスの依存があるらしいので、それをとりのぞいた ungoogled chromium というのがあるらしい。 Brave よりもいいし。色々やっていたら、普通に、Extension もいれられた。 safari が純正で間違いないんだけど、広告の表示でバグる事が多い気がするから却下。firefox は、apple script で操作するのが難しいし、はっきりいって死に体だから、敬遠。一応いれておくけどね

brew install --cask eloston-chromium
#!/bin/bash

# Detects if Chromium is running
if ! pgrep -f "Chromium" > /dev/null 2>&1; then
    open -a "/Applications/Chromium.app"
else
    # Create a new window
    script='tell application "Chromium" to make new window activate'
    ! osascript -e "${script}" > /dev/null 2>&1 && {
        while IFS="" read -r pid; do
            kill -15 "${pid}"
        done < <(pgrep -f "Chromium")
        open -a "/Applications/Chromium.app"
    }
fi

Alacritty

#!/bin/bash

# Detects if Alacritty is running
if ! pgrep -f "Alacritty" > /dev/null 2>&1; then
    open -a "/Applications/Alacritty.app"
else
    # Create a new window
    script='tell application "Alacritty" to keystroke "n" using command down'
    ! osascript -e "${script}" > /dev/null 2>&1 && {
        # Get pids for any app with "iTerm" and kill
        while IFS="" read -r pid; do
            kill -15 "${pid}"
        done < <(pgrep -f "Alacritty")
        open -a "/Applications/Alacritty.app"
    }
fi

iTerm

#!/bin/bash

# Detects if iTerm2 is running
if ! pgrep -f "iTerm" > /dev/null 2>&1; then
    open -a "/Applications/iTerm.app"
else
    # Create a new window
    script='tell application "iTerm2" to create window with default profile'
    ! osascript -e "${script}" > /dev/null 2>&1 && {
        # Get pids for any app with "iTerm" and kill
        while IFS="" read -r pid; do
            kill -15 "${pid}"
        done < <(pgrep -f "iTerm")
        open -a "/Applications/iTerm.app"
    }
fi

(参考) iTerm 自分のウィンドウを消す

tell application "iTerm2"
     tell current window to close current session
end tell

emacs

#!/bin/bash

/opt/homebrew/bin/emacsclient -c -e \
                "(select-frame-set-input-focus (selected-frame))" \
                "(delete-other-windows)" \

Safari

広告をうまく処理できないので、却下

#!/usr/bin/env bash

# Detects if Safari is running
if ! pgrep -f "Safari" > /dev/null 2>&1; then
    open -a "/Applications/Safari.app"
else
    # Create a new window
    script='tell application "Safari" to make new document activate'
    ! osascript -e "${script}" > /dev/null 2>&1 && {
        while IFS="" read -r pid; do
            kill -15 "${pid}"
        done < <(pgrep -f "Safari")
        open -a "/Applications/Safari.app"
    }
fi

広告があるサイトだと、safari バグるから、firefox にしたいけど、 osascript どうすればいいかわからないから、放置。

org-agenda

osascript <<END
tell application "System Events"
     set isRunning to (count of (every process whose name is "Emacs" and visible is true)) > 0
end tell

if not isRunning then
   do shell script "emacsclient -c -e '(select-frame-set-input-focus (selected-frame))' '(delete-other-windows)' '(org-agenda \"a\" \"c\")'"
else
   tell app "Emacs" to activate
   do shell script "emacsclient -e '(org-agenda \"a\" \"c\")'"
end if

END
chmod u+x ~/.config/org-agenda.sh

org-capture   PROPERTIES

:ID: 255a4b11-1274-4104-854c-cd3f84f761de

:END:

osascript <<END
tell application "System Events"
     set isRunning to (count of (every process whose name is "Emacs" and visible is true)) > 0
end tell

if not isRunning then
   do shell script "emacsclient -c -e '(select-frame-set-input-focus (selected-frame))' '(delete-other-windows)' '(org-capture nil \"m\")'"
else
   tell app "Emacs" to activate
   do shell script "emacsclient -e '(org-capture nil \"m\")'"
end if

END
chmod u+x ~/.config/org-capture-task.sh

sketchybar

上のバーの情報は出来るだけミニマルにしたい。mac の環境設定で、メニューバーは自動的に消えるようにしてる。こうすれば、元のメニューバーは情報てんこもりでもいいって事になる。

https://felixkratz.github.io/SketchyBar/config/bar に skechybar の設定項目がのっているのでかえたければ参考にする。

PLUGIN_DIR="$HOME/.config/sketchybar/plugins"

sketchybar --bar height=42        \
                 blur_radius=5   \
                 position=top     \
                 padding_left=17  \
                 padding_right=7 \
         color=0xe80a0e14

# external display
if [[ $(system_profiler SPDisplaysDataType | rg Internal | wc -l | tr -d " \t") == "0" ]] ; then
    sketchybar --bar height=24
fi

# HackGen35 Console NF
sketchybar --default updates=when_shown                    \
                     icon.font="HackGen35 Console NF:Regular:17.0" \
                     icon.color=0xddffffff                 \
                     label.font="SF Pro Rounded:Semibold:13" \
                     label.color=0xddffffff                \
                     label.padding_left=4                  \
                     label.padding_right=4                 \
                     icon.padding_left=4                   \
                     icon.padding_right=4

ねこちゃんマーク。犬派だけど。特に機能はない。

sketchybar --add item cat_icon left                            \
           --set cat_icon icon=󰄛                              \
                 label.drawing=off                    \
                 icon.font="HackGen35 Console NF:Regular:20.0" \
                 icon.color=0xffea9a97 \
                 icon.padding_right=40 \

スペース(デスクトップ) の表示と切り換え:

SPACE_ICONS=("󰇊" "󰇋" "󰇌" "󰇍" "󰇎" "󰇏" "󰎶" "󰲮" "󰎼" "󰽽")

for i in "${!SPACE_ICONS[@]}"
do
  sid=$(($i+1))
  sketchybar --add space space.$sid left                                 \
             --set space.$sid associated_space=$sid                      \
                              icon=${SPACE_ICONS[i]}                     \
                              icon.padding_left=8                        \
                              icon.padding_right=8                      \
                              icon.color=0x44ffffff                       \
                              icon.highlight_color=0xddffffff                       \
                              label.drawing=off                          \
                              script="$PLUGIN_DIR/space.sh"              \
                              click_script="yabai -m space --focus $sid"
done

右は、時計と電池がなくなったら電池の残量表示

mail のチェックはデフォルトで 5 分になっているので、そんなに更新をする必要がない。

launchctl list | rg homebrew
sketchybar --add item clock right                        \
           --set clock   update_freq=10                  \
                         script="$PLUGIN_DIR/clock.sh"   \
                                                         \
       --add alias "Control Center,Battery" right              \
           --set "Control Center,Battery" update_freq=30 \
                                            label.drawing=off\
                                                         \
    --add item mail right \
           --set mail script="$PLUGIN_DIR/mail.sh" \
           update_freq=30

設定の反映をさせるコマンド?らしい

sketchybar --update
echo "sketchybar configuration loaded.."

plugins

どれも書く程ではないようみえるけど、毎回別に実行されるスクリプトは別に書いておかないといけない。

clock.sh

#!/usr/bin/env sh
sketchybar --set $NAME label="$(date '+%H:%M')" label.padding_right=10

space.sh

#!/usr/bin/env sh
sketchybar --set $NAME icon.highlight=$SELECTED icon.hightligh_color=0xffffffff

mail.sh

count mails in new folder

fd -ptf /new/ ~/Mail/ -E 'Trash' -E 'All*' -E 'yasushi.accounts*' -E 'yasushi*' -E Archive | wc -l | tr -d ' \t'

using himalaya

himalaya list -o json | jq length
NEWMAIL=$(himalaya list -o json | jq length)
if [[ $NEWMAIL -ne "0" ]]; then
    sketchybar --set $NAME icon.font="SF Pro Rounded:Regular:13" icon.color=0xffffffff icon="􀈫" label.drawing=on label="$NEWMAIL" click_script="~/.config/sketchybar/open_mutt.sh"
else
    sketchybar --set $NAME icon.font="SF Pro Rounded:Regular:13" icon="􀈣" label.drawing=off icon.color=0x33ffffff click_script="~/.config/sketchybar/open_mutt.sh"
fi

open iterm2 and run a command.

#!/bin/bash
osascript - "$@" <<EOF
on run argv
tell application "iTerm"
    activate
    set new_term to (create window with default profile)
    tell new_term
        tell the current session
            repeat with arg in argv
               write text arg
            end repeat
        end tell
    end tell
end tell
end run
EOF

use above to open mutt (aliased to neomutt)

#!/usr/bin/env bash
$HOME/.config/sketchybar/iterm_cmd.sh ~/.config/neomutt/open_close_neomutt.sh

おわったら、セッションごと消す

neomutt

if [[ $! -eq "0" ]]; then
    osascript ~/.config/scripts/close_iterm.as
fi

AquaSKK

日本語の入力は AquaSKK を使います。

元々 config は ~/Library/Application Support/AquaSKK/keymap.conf

英数モードにすると iTerm とか支障をきたすので、出来るだけそうならないようする。

###
### keymap.conf
###

# ======================================================================
# event section
# ======================================================================

SKK_JMODE        ctrl::j
SKK_ENTER        group::hex::0x03,0x0a,0x0d||ctrl::m
SKK_CANCEL        ctrl::g||hex::0x1b
SKK_BACKSPACE        hex::0x08||ctrl::h
SKK_DELETE        hex::0x7f||ctrl::d
SKK_TAB            hex::0x09||ctrl::i
SKK_PASTE        ctrl::y
SKK_LEFT        hex::0x1c||ctrl::b||keycode::7b
SKK_RIGHT        hex::0x1d||ctrl::f||keycode::7c
SKK_UP            hex::0x1e||ctrl::a||keycode::7e
SKK_DOWN        hex::0x1f||ctrl::e||keycode::7d
SKK_PING        ctrl::l
SKK_UNDO                ctrl::/

# ======================================================================
# attribute section(for SKK_CHAR)
# ======================================================================

ToggleKana        q
ToggleJisx0201Kana    ctrl::q
SwitchToAscii        ctrl::shift::l
SwitchToJisx0208Latin    ctrl::shift::alt::l

EnterAbbrev        /
EnterJapanese        Q
NextCompletion        .
PrevCompletion        ,
NextCandidate        hex::0x20||ctrl::n
PrevCandidate        x||ctrl::p
RemoveTrigger        X

UpperCases        group::A-K,M-P,R-Z
Direct            group::keycode::0x41,0x43,0x45,0x4b,0x4e,0x51-0x59,0x5b,0x5c,0x5f
InputChars              group::hex::0x20-0x7e

CompConversion        alt::hex::0x20||shift::hex::0x20

# ======================================================================
# handle option
# ======================================================================

AlwaysHandled           group::keycode::0x66,0x68
PseudoHandled           ctrl::l

Zathura

軽量 pdf ビューワー

さんざんやったけど、 emacsclient -c file で開けるじゃん。emacs 入れない事があるかもしれないから一応のこしておく

https://github.com/zegervdv/homebrew-zathura/issues/87

poppler

set statusbar-h-padding 0
set statusbar-v-padding 0
set page-padding 1

set recolor true
set inputbar-bg  \#15161e
set inputbar-fg \#c0caf5
set default-bg \#15161e
set default-fg \#c0caf5
set statusbar-bg \#15161e
set statusbar-fg \#c0caf5
set highlight-active-color \#9ece6a
set highlight-color \#e0af68

set recolor-lightcolor \#15161e
set recolor-darkcolor \#c0caf5

map R reload
map r rotate
map K scroll half-up
map J scroll half-down
map i recolor

:20 で 20 ページにいける。

tab

other tips

デフォルトで iTerm2 + nvim で開く。

open xxx.rs すると、なぜか、vscode が開いてしまって、悲しい気持ちになるのでそれを回避する。 Automator で下記を設定する必要あり。

https://gist.github.com/charlietran/43639b0f4e0a01c7c20df8f1929b76f2

on run {input, parameters}
set path to POSIX path of input
set cmd to "nvim " & quote & path & quote
tell application "iTerm"
    activate
    set new_term to (create window with default profile)
    tell new_term
        tell the current session
             command cmd
        end tell
    end tell
end tell
end run

reboot でウィンドウ一掃

reboot したい時は、大抵一回全部アプリを落したい時なのに、デフォルトで sudo reboot すると、元々起動していたアプリが起動してうざい。

defaults write -g ApplePersistence -bool no

Mac Ports

brew はなんだかんだ、使うけど、mac ports は忘れる

sudo port selfupdate

gitignore .DSStore

.DS_Store
git config --global core.excludesfile ~/.gitignore_global

xcode-select

unreal wants ’xcode’ in the Application folder.

sudo xcode-select -switch /Applications/Xcode.app/Contents/Developer
sudo xcode-select -switch /Library/Developer/CommandLineTools

git

git はほぼ cli で使うんだけど、ui を使った方一行づつコミットしたい時とかなんだかんだ便利だと思う。癖で多分 cli を使うんだろうけど、少しづつ慣れていこう。(cli で使えた方が、咄嗟の外部サーバーでの利用を考えた時に便利だからそれは覚えておく必要あり。)

brew install gitui

(もちろん)vim-keybinding 化する。

mkdir -p $HOME/.config/gitui
curl https://raw.githubusercontent.com/extrawurst/gitui/master/vim_style_key_config.ron -o $HOME/.config/gitui/key_bindings.ron

bluetooth

sudo pkill bluetoothd
rm /Library/Preferences/com.apple.bluetooth.plist
rm /Users/yasushi/Library/Preferences/ByHost/com.apple.Bluetooth.*.plist

pam (sudo を時計でやる)

:tangle /etc/pam.d/sudo

# sudo: auth account password session
# auth       sufficient     pam_watchid.so  "reason=execute a command as root"
auth       sufficient     pam_smartcard.so
auth       required       pam_opendirectory.so
account    required       pam_permit.so
password   required       pam_deny.so
session    required       pam_permit.so

make file の TARGET を帰る必要があり、その時に darwin の version が必要になる。 

clang -dumpmachine
arm64-apple-darwin22.4.0

あるいは swift -version らしいけどちょっと違う?

typo in /etc/pam.d/sudo

if make a typo in /etc/pam.d/sudo, you’ve effectively locked yourself up from sudo. You bypass this by auth-ing through Finder.

open /etc/pam.d

then sudo > “Get Info”, unlock it (with the icon), then change “Everyone” to “Read & Write.” You will now able to edit this through vim.

Date: 2022-07-29 Fri 13:27