macbook优化-命令行打开App

在 macbook 上敲代码时,有时候想要打开QQMusic或者其它什么app,因为一直在命令行上操作,懒得去查看程序坞或者打开launchpad,所以想直接从命令行上启动App。macbook 可以使用 open AppPath 的方式打开App,但是输入 AppPath 比较麻烦。已经知道所有的App都安装在 /Applications 目录下,所以,如果写一个函数,直接到 /Applications 目录下查找想要的App,然后使用 open 命令打开,岂不是很方便 ?
bash 函数如下:

# start targetApp in /Applications, the specified targetAppName could be any sub string of the App's Name, ignoring the letter case
function start() {
    if [[ $# -eq 0 ]]
    then
        echo "usage: $FUNCNAME <targetAppName>"
        return
    fi
    targetApp=$(echo $1 | tr 'A-Z' 'a-z') #translate targetApp name to lower case, so when matching app's name, we can ignore the letter case
    APPDIR=/Applications
    cd ${APPDIR}
    for app in *.app
    do
        lowerCaseApp=$(echo ${app} | tr 'A-Z' 'a-z')
        if [[ $lowerCaseApp =~ ${targetApp} ]]
        then
            read -p "Shall I open ${APPDIR}/${app} ? (y/Y | n/N) " -t 5 choice 
            choice=$(echo $choice | tr 'A-Z' 'a-z')
            if [[ ${choice:0:1} == "y" ]]
            then
                open "${APPDIR}/${app}"
                return
            fi
            echo
        fi
    done
    echo "${1} not found in ${APPDIR}"
}

将该函数添加到 ~/.bashrc,然后执行 source ~/.bashrc,即可成功运行了。
例如:

$ start qqm #尝试打开QQMusic
Shall I open /Applications/QQMusic.app ? (y/Y | n/N) y

$ start mic #尝试打开 Microsoft 软件
Shall I open /Applications/Microsoft Excel.app ? (y/Y | n/N)  

Shall I open /Applications/Microsoft OneNote.app ? (y/Y | n/N) 

Shall I open /Applications/Microsoft Outlook.app ? (y/Y | n/N) 

Shall I open /Applications/Microsoft PowerPoint.app ? (y/Y | n/N) y

$ start word #尝试打开 Microsoft Word
Shall I open /Applications/Microsoft Word.app ? (y/Y | n/N) y
上一篇:2021年苹果电脑选购建议,资深用户带你讲透MacBook Air MacBook Pro(含M1)


下一篇:入门版本 MacBook Air 带给我无限惊喜