
예, 저는 Apple Script에 대해 초보자로서 끔찍한 경험을 하고 있습니다.
현재 데스크톱 공간에서 새 터미널 창을 열어야 합니다. 터미널이 실행 중인 다른 공간으로 이동한 다음 다른 터미널 창을 열지 마십시오.
물론 터미널이 실행되고 있지 않으면 새 터미널 프로세스를 시작해야 합니다.
답변1
tell application "Terminal"
do script " "
activate
end tell
이상해 보이지만 터미널이 들어오는 "do script" 명령을 처리하는 방법의 이상한 점을 이용합니다. 각각에 대해 새 창을 만듭니다. 원한다면 실제로 이를 유용한 것으로 대체할 수 있습니다. 새 창을 연 직후에 원하는 것이 무엇이든 실행됩니다.
답변2
do 스크립트 " " 사이에 텍스트가 없으면 터미널에 추가 명령 프롬프트가 표시되지 않습니다.
tell application "Terminal"
do script ""
activate
end tell
답변3
나는 그것을 하는 세 가지 다른 방법을 생각할 수 있습니다(처음 두 개는 다른 곳에서 훔쳤지만 어디서 왔는지 잊어버렸습니다). 나는 매번 새 창을 열고 싶고 가장 짧기 때문에 applescript에서 쉘 스크립트를 호출하는 세 번째 것을 사용합니다.
최소 10.10 이후에 OS X에 내장된 스크립트와 달리 이들 모두는 파인더 창의 현재 작업 디렉토리인 모든 디렉토리에서 터미널을 엽니다(즉, 폴더를 열기 위해 폴더를 선택할 필요가 없습니다).
또한 Finder > 터미널 > Finder 서클을 완성하기 위한 몇 가지 bash 기능도 포함되었습니다.
1. 기존 탭을 재사용하거나 새 터미널 창을 만듭니다.
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
2. 기존 탭을 재사용하거나 새 터미널 탭을 만듭니다.
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
3. applescript에서 호출되는 쉘 스크립트를 통해 매번 새 창을 생성합니다.
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
4. (보너스) 터미널의 현재 작업 디렉터리에 대한 새 찾기 창을 여는 Bash 별칭
이 별칭을 .bash_profile에 추가하세요.
alias f='open -a Finder ./'
5. (보너스) 터미널 창의 디렉터리를 전면 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
}
답변4
위의 답변은 터미널이 이미 실행 중인 경우에만 작동합니다. 그렇지 않으면 한 번에 두 개의 터미널 창이 열립니다. 하나는 로 인해 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