diff --git a/knotfiles/autostart.nu b/knotfiles/autostart.nu index c93e24f..2603c17 100644 --- a/knotfiles/autostart.nu +++ b/knotfiles/autostart.nu @@ -2,13 +2,15 @@ # # Copyright (C) 2022 KAAAsS -# 设置自启脚本 -export def install [ - path: path -] { - let filename = ($path | path basename) - mkdir $"($env.HOME_DIR)/.config/autostart" - echo $"[Desktop Entry] +use constraints.nu + +module linux { + export def install [ + path: path + ] { + let filename = ($path | path basename) + mkdir $"($env.HOME_DIR)/.config/autostart" + echo $"[Desktop Entry] Exec=($path) Icon=dialog-scripts Name=($filename) @@ -16,6 +18,68 @@ Path= Type=Application X-KDE-AutostartScript=true " | save -f $"($env.HOME_DIR)/.config/autostart/($filename).desktop" + null + } + + export def uninstall [ + path: path + ] { + let filename = ($path | path basename) + rm -f $"($env.HOME_DIR)/.config/autostart/($filename).desktop" + null + } +} + +module darwin { + export def install [ + path: path + ] { + let filename = ($path | path basename) + mkdir $"($env.HOME_DIR)/Library/LaunchAgents" + echo $" + + + + Label + ($filename) + ProgramArguments + + ($path) + + RunAtLoad + + + +" | save -f $"($env.HOME_DIR)/Library/LaunchAgents/($filename).plist" + launchctl load $"($env.HOME_DIR)/Library/LaunchAgents/($filename).plist" + null + } + + export def uninstall [ + path: path + ] { + let filename = ($path | path basename) + launchctl stop $"($filename)" + launchctl unload $"($env.HOME_DIR)/Library/LaunchAgents/($filename).plist" + rm -f $"($env.HOME_DIR)/Library/LaunchAgents/($filename).plist" + null + } +} + +use linux +use darwin + +# 设置自启脚本 +export def install [ + path: path +] { + if (constraints os linux) { + linux install $path + } else if (constraints os darwin) { + darwin install $path + } else { + error make { msg: "当前系统不支持自启脚本" } + } null } @@ -23,7 +87,12 @@ X-KDE-AutostartScript=true export def uninstall [ path: path ] { - let filename = ($path | path basename) - rm -f $"($env.HOME_DIR)/.config/autostart/($filename).desktop" + if (constraints os linux) { + linux uninstall $path + } else if (constraints os darwin) { + darwin uninstall $path + } else { + error make { msg: "当前系统不支持自启脚本" } + } null }