MacOS

Posted by Adam on August 24, 2022
### [[滑鼠]羅技木星軌跡球 如何在macbook自訂鍵 ](https://www.ptt.cc/bbs/Key_Mou_Pad/M.1582204955.A.BA8.html) #### [Hammerspoon](https://www.hammerspoon.org/) #### [相關設定調整](https://github.com/tekezo/Karabiner/issues/814) ```lua -- HANDLE SCROLLING local oldmousepos = {} local scrollmult = -4 -- negative multiplier makes mouse work like traditional scrollwheel mousetap = hs.eventtap.new({5}, function(e) oldmousepos = hs.mouse.getAbsolutePosition() local mods = hs.eventtap.checkKeyboardModifiers() if mods['ctrl'] and mods['cmd'] then -- print ("will scroll") local dx = e:getProperty(hs.eventtap.event.properties['mouseEventDeltaX']) local dy = e:getProperty(hs.eventtap.event.properties['mouseEventDeltaY']) local scroll = hs.eventtap.event.newScrollEvent({dx * scrollmult, dy * scrollmult},{},'pixel') scroll:post() -- put the mouse back hs.mouse.setAbsolutePosition(oldmousepos) -- return true, {scroll} return true else return false, {} end -- print ("Mouse moved!") -- print (dx) -- print (dy) end) mousetap:start() ``` ```lua -- HANDLE SCROLLING local oldmousepos = {} -- positive multiplier (== natural scrolling) makes mouse work like traditional scrollwheel local scrollmult = 4 -- The were all events logged, when using `{"all"}` mousetap = hs.eventtap.new({0,3,5,14,25,26,27}, function(e) oldmousepos = hs.mouse.getAbsolutePosition() local mods = hs.eventtap.checkKeyboardModifiers() local pressedMouseButton = e:getProperty(hs.eventtap.event.properties['mouseEventButtonNumber']) -- If OSX button 4 is pressed, allow scrolling local shouldScroll = 3 == pressedMouseButton if shouldScroll then local dx = e:getProperty(hs.eventtap.event.properties['mouseEventDeltaX']) local dy = e:getProperty(hs.eventtap.event.properties['mouseEventDeltaY']) local scroll = hs.eventtap.event.newScrollEvent({dx * scrollmult, dy * scrollmult},{},'pixel') scroll:post() -- put the mouse back hs.mouse.setAbsolutePosition(oldmousepos) return true, {scroll} else return false, {} end -- print ("Mouse moved!") -- print (dx) -- print (dy) end) mousetap:start() -- 檔案更新時自動重新載入 Hammerspoon 設定檔 function reloadConfig(files) for _,file in pairs(files) do if file:sub(-4) == ".lua" then hs.reload() -- 重新載入 Hammerspoon 設定檔 return; end end end hammerWatcher = hs.pathwatcher.new(os.getenv("HOME") .. "/.hammerspoon/", reloadConfig):start() hs.alert.show("Config loaded") print("Config loaded") -- 定義一個 function 來調整預設輸出裝置的音量 local function adjustVolume(delta) return function() -- 取得目前預設輸出裝置的音量 local currVolume = hs.audiodevice.defaultOutputDevice():outputVolume() hs.audiodevice.defaultOutputDevice():setOutputVolume(currVolume + delta) end end -- 將 rightctrl + ] 設定為音量增加 5 -- 將 rightctrl + [ 設定為音量減少 5 hs.hotkey.bind({"ctrl"}, "]", adjustVolume(5)) hs.hotkey.bind({"ctrl"}, "[", adjustVolume(-5)) ``` ### [Mac 上如何使用Emoji 表情符號](https://mrmad.com.tw/mac-emoji-shortcut) 在任何能夠輸入的頁面中,透過 「Control+Command⌘+空白鍵」三個按鍵快捷鍵。 ### [iTerm2 於 irc 使用 Option (alt) 切換視窗的設定](https://blog.longwin.com.tw/2015/01/mac-iterm2-irc-option-alt-esc-2014/) 1. 開啟 iTerm2 1. Preferences -> Profiles -> Keys 1. 最下方的 Left option 從 "Normal" 改成 "+Esc" 1. 關掉視窗就可以直接使用了~ ### [Mac 快捷鍵](https://is.gd/UUBVfW) ### [How do I start the docker daemon on macOS?](https://apple.stackexchange.com/questions/373888/how-do-i-start-the-docker-daemon-on-macos) ``` open -a Docker ``` or ``` brew install colima colima start # create VM with 1CPU, 2GiB memory and 10GiB storage colima start --cpu 1 --memory 2 --disk 10 # modify an existing VM to 4CPUs and 8GiB memory. colima start --cpu 4 --memory 8 docker ps -a ``` # 開機執行腳本 在 macOS 中,若要在系統開機時自動執行腳本(Script),最標準、穩定且蘋果官方推薦的方法是使用 launchd 機制(建立一個 .plist 設定檔)。 以下是為您整理的最簡明操作步驟: ## 步驟 1:準備好您的腳本檔案 1. 開啟終端機 (Terminal)。 2. 建立並編輯您的腳本(例如放在 ~/Documents/myscript.sh): ``` nano ~/Documents/myscript.sh ``` 3. 輸入您的腳本內容(第一行必須是 #!/bin/bash 或 #!/bin/zsh),例如: ``` #!/bin/zsh# 在這裡輸入您想執行的指令,例如記錄開機時間 echo "Boot time: $(date)" >> ~/Desktop/boot_log.txt ``` 4. 儲存並退出(在 nano 中按 Ctrl + O 儲存,再按 Ctrl + X 離開)。 5. 非常重要: 賦予腳本執行權限: ``` chmod +x ~/Documents/myscript.sh ``` ------------------------------ ## 步驟 2:建立 launchd 的 .plist 設定檔 launchd 透過 .plist 檔案來管理啟動任務。 1. 在終端機中,於使用者的 LaunchAgents 目錄下建立一個新的設定檔: ``` nano ~/Library/LaunchAgents/com.user.bootscript.plist ``` 2. 將以下內容完整複製並貼上(請將代碼中的 /Users/您的用戶名/Documents/myscript.sh 改為您真實的腳本路徑): ``` <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://apple.com"> <plist version="1.0"> <dict> <key>Label</key> <string>com.user.bootscript</string> <key>ProgramArguments</key> <array> <string>/Users/您的用戶名/Documents/myscript.sh</string> </array> <key>RunAtLoad</key> <true/> </dict> </plist> ``` 3. 儲存並退出(Ctrl + O 然後 Ctrl + X)。 ------------------------------ ## 步驟 3:載入並啟用該開機任務 1. 賦予該 .plist 檔案正確的安全權限: ``` chmod 644 ~/Library/LaunchAgents/com.user.bootscript.plist ``` 2. 使用 launchctl 指令將這個任務註冊到系統中: ``` launchctl bootstrap gui/$(id -u) ~/Library/LaunchAgents/com.user.bootscript.plist ``` 這樣就完成了!下次您的 Mac 開機並登入桌面時,系統就會自動背景執行該腳本。 ------------------------------ ## 💡 常見問題與管理指令 * 如何測試腳本有沒有成功被 launchd 執行? 您不用真的重啟 Mac,可以直接輸入以下指令手動觸發測試: ``` launchctl kickstart -k gui/$(id -u)/com.user.bootscript ``` * 如果以後想要「取消」這個開機自動執行? 輸入以下指令解除註冊,並把 .plist 檔案刪除即可: ``` launchctl bootout gui/$(id -u)/com.user.bootscript.plist rm ~/Library/LaunchAgents/com.user.bootscript.plist ```