如何在終端上創(chuàng)建一個(gè)macOS替身?
How to create a macOS alias file in terminal?
最近一次系統(tǒng)更新導(dǎo)致系統(tǒng)崩潰,讓我下決心解決掉這個(gè)坑:把程序移出 [/Application]
遇到的問(wèn)題:
- 經(jīng)常使用Launchpad炫刷,所以需要把 app/app鏈接 放入系統(tǒng)分區(qū)(如: [/Application] )才能在Launchpad顯示
- 需要方便地將 app/app鏈接 放入 [/Application] 宪潮,因此采用shell腳本纪他,一鍵執(zhí)行蛛倦。
- unix 的 ln -s 雖然可以創(chuàng)建軟鏈,但是創(chuàng)建出來(lái)的軟鏈無(wú)法在Launchpad中顯示昼伴,而且軟鏈會(huì)因?yàn)橐苿?dòng)源文件的位置后失效(比如以后要對(duì)源app文件夾分類(lèi))
解決方法:
注意 這是一行代碼
osascript -e 'tell application "Finder" to make alias file to POSIX file "/Volumes/MyDrive/Software/Opera.app" at POSIX file "/Applications"'
描述:
使用 osascript 調(diào)用系統(tǒng)Finder程序匾旭,從不同于系統(tǒng)分區(qū)的位置 [/Volumes/MyDrive/Software/Opera.app] ,創(chuàng)建一個(gè)替身到[/Application] 下面
最終腳本:
目錄準(zhǔn)備:
- Volumes
- MyDrive
- Software
- Opera.app
- Google Chrome.app
- Firefox.app
-
install.sh
- Software
- MyDrive
描述:
創(chuàng)建一個(gè)叫做MyDrive的分區(qū)圃郊,在分區(qū)下創(chuàng)建一個(gè)Software的文件夾价涝,文件夾內(nèi)存放app程序,以及 install.sh 腳本
腳本內(nèi)容:
#!/bin/bash
set -e
cd "$(dirname "$0")/"
ROOT_PATH=$(pwd -P)
for src in $(find "$ROOT_PATH" -maxdepth 1 -name '*.app' -not -path '*.git*' | sed 's/ /-/g')
do
SOURCE_PATH=$(echo ${src}|sed s'/-/ /g')
DESTINATION_FOLDER="/Applications"
osascript -e "tell application \"Finder\" to make alias file to POSIX file \"$SOURCE_PATH\" at POSIX file \"$DESTINATION_FOLDER\""
done
使用方式:
1. 重裝系統(tǒng)
2. chmod +x install.sh持舆,雙擊圖標(biāo)執(zhí)行 或者 終端中執(zhí)行 ./install.sh
缺點(diǎn):
無(wú)法處理pkg安裝的程序色瘩,
腳本缺點(diǎn):循環(huán)處理.app文件伪窖,遇到空格并不是用IFS,而是直接替換字符串實(shí)現(xiàn)居兆,存在問(wèn)題
待改進(jìn):
重裝系統(tǒng)后雖然容易實(shí)現(xiàn)“恢復(fù)app”這個(gè)概念覆山,但是app對(duì)應(yīng)的配置文件,數(shù)據(jù)等丟失泥栖。需要建立起每個(gè)app對(duì)應(yīng)配置文件的dotfile簇宽,在github上更新。