Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/macos/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Macos 使用~/.bash_配置文件中的别名自动化Mac应用程序中的日常任务_Macos_Terminal_Workflow_Alias_.bash Profile - Fatal编程技术网

Macos 使用~/.bash_配置文件中的别名自动化Mac应用程序中的日常任务

Macos 使用~/.bash_配置文件中的别名自动化Mac应用程序中的日常任务,macos,terminal,workflow,alias,.bash-profile,Macos,Terminal,Workflow,Alias,.bash Profile,我知道您可以在~/.bash\u配置文件中创建别名,自动执行bash命令,如: alias fly="ssh username@ip_address -p22" 但我想知道是否有可能在Mac上的应用程序中自动化任务。例如,我知道您可以使用别名打开系统首选项: alias sys="open /Applications/System\ Preferences.app/" 但是,如何在系统首选项应用程序中自动导航?例如,我打开系统首选项,键入“网络”,然后频繁按enter键导航到网络设置 有没有

我知道您可以在
~/.bash\u配置文件
中创建别名,自动执行bash命令,如:

alias fly="ssh username@ip_address -p22"
但我想知道是否有可能在Mac上的应用程序中自动化任务。例如,我知道您可以使用别名打开系统首选项:

alias sys="open /Applications/System\ Preferences.app/"
但是,如何在系统首选项应用程序中自动导航?例如,我打开系统首选项,键入“网络”,然后频繁按enter键导航到网络设置


有没有办法用别名自动执行这些步骤?或者别名仅限于bash命令?

Shell别名仅限于Shell命令。但是,许多OSX应用程序可以由AppleScript命令控制,这些命令可以通过shell命令
osascript
发出。不过,引用有点棘手,因为AppleScript命令通常包含引号,然后需要在shell命令中用另一层引号括起来,然后在定义别名时用另一层引号括起来

有关使用AppleScript的注意事项,请参见(特别是系统首选项)。要获取系统首选项以显示网络窗格,可以使用以下AppleScript:

tell application "System Preferences"
    reveal pane id "com.apple.preference.network"
    activate
end tell
…可通过此
osascript
命令发出(请注意,脚本的每一行对应一个
-e
参数,并且AppleScript双引号被包装在shell的单引号中):

您可以为此创建别名:

alias networkprefs="osascript -e 'tell application \"System Preferences\"' -e 'reveal pane id \"com.apple.preference.network\"' -e 'activate' -e 'end tell'"
请注意,我必须避开用于AppleScript消费的双引号。。。相当混乱。我会使用一个shell函数(类似于别名,但没有引用奇怪之处):


谢谢你的回复!非常有用:)
alias networkprefs="osascript -e 'tell application \"System Preferences\"' -e 'reveal pane id \"com.apple.preference.network\"' -e 'activate' -e 'end tell'"
networkprefs() {
    osascript -e 'tell application "System Preferences"' -e 'reveal pane id "com.apple.preference.network"' -e 'activate' -e 'end tell'
}