常用脚本文档

常用脚本可以把 Shell 脚本和 AppleScript 放进 Finder 右键菜单。适合把重复的文件处理、项目命令、系统开关、Git 操作、内部工具调用做成一次右键操作。

脚本可以完全自定义:名称、类型、图标、是否需要选中文件、是否需要选中文件夹、是否显示执行日志,都可以按工作流配置。

脚本类型

Shell 脚本执行时,MouseBoost 会先切换到当前选中文件所在目录。也就是说,很多项目命令可以直接写 git statusnpm run buildmake,不需要自己先写 cd

可用变量

变量 含义
@rfpath 当前选中文件或文件夹路径。
@rfpaths 多个选中文件或文件夹路径。
@rfrawpath 当前选中文件或文件夹的原始路径。
@rfpos 当前选中文件或文件夹所在目录。
@rfposs 多个选中文件或文件夹所在目录。
@rfname 当前选中文件或文件夹名称,包含扩展名。
@rfnames 多个选中文件或文件夹名称,包含扩展名。
@rfnam 当前选中文件或文件夹名称,不包含扩展名。
@rfnams 多个选中文件或文件夹名称,不包含扩展名。
@rfext 当前选中文件扩展名。
@rfexts 多个选中文件扩展名。
@rday MouseBoost 使用天数。
@rloading 执行脚本时显示加载提示,适合耗时操作。
@rftocommonpath 选择常用路径。过多使用可能影响右键菜单加载速度。

变量写法建议

如果脚本要处理路径,优先使用 MouseBoost 变量,而不是手动复制 Finder 路径。

open @rfpath

处理当前文件所在目录:

cd @rfpos

处理多个选中文件:

for file in @rfpaths
do
  echo "$file"
done

耗时命令可以加上 @rloading

@rloading
git pull

默认 Shell 脚本

根据文件创建同名文件夹

选中文件后创建一个同名文件夹,不包含扩展名。

mkdir @rfpos/@rfnam

tar 压缩

选中文件或文件夹后,在当前目录生成 tar.gz 文件。

@rloading
cd @rfpos
tar -zcvf @rfpos/@rfnam.tar.gz @rfnames

重启 Finder

killall Finder

切换台前调度

macOS 13+ 可用。

state=$(defaults read com.apple.WindowManager GloballyEnabled 2>/dev/null)
if [ "$state" = "0" ] || [ -z "$state" ]; then
defaults write com.apple.WindowManager GloballyEnabled -bool true
else
defaults write com.apple.WindowManager GloballyEnabled -bool false
fi

切换台前调度最近 App

macOS 13+ 可用。

state=$(defaults read com.apple.WindowManager AutoHide 2>/dev/null)
if [ "$state" = "0" ] || [ -z "$state" ]; then
defaults write com.apple.WindowManager AutoHide -bool true
else
defaults write com.apple.WindowManager AutoHide -bool false
fi

显示本机 IP

该脚本会显示执行日志。

ipconfig getifaddr en0 || ipconfig getifaddr en1

清空剪贴板

pbcopy < /dev/null

默认 AppleScript 脚本

欢迎提示

实际文案会根据当前语言和 App 名称生成,下面是结构示例。

set content to "Welcome to use MouseBoost"
set btns to {"Cancel", "Go Review"}
try
    display dialog content buttons btns default button 2 with icon note with title "Tip"
    do shell script "open https://apps.apple.com/app/id1551462255"
on error
    display alert "Canceled"
end try

切换深色 / 浅色外观

tell application "System Events"
tell appearance preferences
set dark mode to not dark mode
end tell
end tell

启动屏幕保护

tell application "System Events"
start current screen saver
end tell

切换到深色外观

tell application "System Events"
set dark mode of appearance preferences to true
end tell

切换到浅色外观

tell application "System Events"
set dark mode of appearance preferences to false
end tell

锁屏

tell application "System Events"
set frontApp to name of first application process whose frontmost is true
tell application frontApp to activate
keystroke "q" using {command down, control down}
end tell

睡眠显示器并锁定

do shell script "pmset displaysleepnow"

睡眠

tell application "Finder" to sleep

切换菜单栏自动隐藏

tell application "System Events"
tell dock preferences
set autohide menu bar to not autohide menu bar
end tell
end tell

切换 Dock 自动隐藏

tell application "System Events"
tell dock preferences
set autohide to not autohide
end tell
end tell

弹出所有外置磁盘

tell application "Finder"
eject (every disk whose ejectable is true and local volume is true)
end tell

清空废纸篓

tell application "Finder"
empty trash
end tell

Git 动态脚本

Git 菜单会根据当前目录是否是 Git 仓库动态显示。如果当前文件夹还不是 Git 仓库,会显示初始化入口;如果已经是 Git 仓库,会显示常用 Git 操作。

Git Init Here

git init @rfpath

Git Pull

@rloading
git pull

Git Push

@rloading
git push

Git Add & Commit

MSG=$(osascript -e 'text returned of (display dialog "Commit message:" default answer "" with title "Git Add & Commit")' 2>/dev/null)
[ -n "$MSG" ] && git add @rfpath && git commit -m "$MSG"

Git Add

git add @rfpath

Git Commit

MSG=$(osascript -e 'text returned of (display dialog "Commit message:" default answer "" with title "Git Commit")' 2>/dev/null)
[ -n "$MSG" ] && git commit -a -m "$MSG"

Git Fetch

@rloading
git fetch --all

Git Status

该脚本会显示执行日志。

git status

Git Log

该脚本会显示执行日志。

git log --oneline -20

Git Diff

该脚本会显示执行日志。

git diff @rfpath

专业用户可以怎么改

用编辑器打开当前目录

code @rfpos

把选中文件复制到一个常用目录

cp @rfpath ~/Desktop/

对当前项目执行构建

@rloading
npm run build

把图片批量交给自定义工具处理

for file in @rfpaths
do
  /path/to/your-tool "$file"
done

使用注意事项