Answers:
tell application "Terminal"
do script " "
activate
end tell
看起来很奇怪,但是它利用了Terminal处理传入的“ do script”命令的方式的一个奇怪之处。它为每个窗口创建一个新窗口。如果需要,您实际上可以用一些有用的东西代替它;打开新窗口后,它将执行您想要执行的任何操作。
如果您在do脚本“”之间没有任何文本,则不会在终端中看到额外的命令提示符。
tell application "Terminal"
do script ""
activate
end tell
我可以想到三种不同的方式(前两种是从其他地方偷来的,但我忘了在哪里)。我使用第三个脚本,它从applescript调用了一个shell脚本,因为我想每次都打开一个新窗口,因为它是最短的。
与至少从10.10开始内置在OS X中的脚本不同,所有这些脚本都在finder窗口中当前工作目录所在的目录中打开终端(即,无需选择文件夹即可打开它)。
还包括一些bash功能,以完成Finder> Terminal> Finder圈子。
tell application "Finder" to set myDir to POSIX path of (insertion location as alias)
tell application "Terminal"
if (exists window 1) and not busy of window 1 then
do script "cd " & quoted form of myDir in window 1
else
do script "cd " & quoted form of myDir
end if
activate
end tell
tell application "Finder" to set myDir to POSIX path of (insertion location as alias)
tell application "Terminal"
if not (exists window 1) then reopen
activate
if busy of window 1 then
tell application "System Events" to keystroke "t" using command down
end if
do script "cd " & quoted form of myDir in window 1
end tell
tell application "Finder"
set myDir to POSIX path of (insertion location as alias)
do shell script "open -a \"Terminal\" " & quoted form of myDir
end tell
将此别名添加到您的.bash_profile。
alias f='open -a Finder ./'
将此功能添加到您的.bash_profile。
cdf() {
target=`osascript -e 'tell application "Finder" to if (count of Finder windows) > 0 then get POSIX path of (target of front Finder window as text)'`
if [ "$target" != "" ]; then
cd "$target"; pwd
else
echo 'No Finder window found' >&2
fi
}
仅当Terminal已在运行时,以上答案才有效。否则,它会一次打开两个终端窗口-一个因为,do script
一个因为activate
。
您可以通过一个简单的if ... else来防止这种情况:
if application "Terminal" is running then
tell application "Terminal"
do script ""
activate
end tell
else
tell application "Terminal"
activate
end tell
end if
奖金:
如果您想直接运行命令,则可以通过击键来完成(不是很优雅-我知道!但是可以)
[...]
else
tell application "Terminal"
activate
tell application "System Events" to keystroke "ls -la"
tell application "System Events" to key code 36
end tell
end if