Trying to open the current finder window in iTerm but i get the following error
error “iTerm got an error: TERM environment variable not set.” number 1
tell application "Finder"
if exists Finder window 1 then
set currentFolder to target of Finder window 1 as alias
else
return
end if
end tell
tell application "iTerm"
activate
do shell script "cd " & quoted form of POSIX path of currentFolder & "; clear"
activate
end tell
If I change the line: tell application “iTerm” to tell application “Terminal” it works.
I’d really like it to work with iTerm as my colour scheme is setup the way I want.
Now, assuming we use already the zsh shell as default, instead of the bash shell:
The iTerms.app doesn’t have do script command like Terminal.app to do script telling directly to iTerms.app
I can’t find equivalent of bash’s clear command in the zsh shell.
Using GUI scripting I can solve the OP’s problem this way:
----------------------------------- iTerm.app ----------------------------------
-- script: Clear terminal contents & Go to indicated directory
-- by: Kniazidis Robert, Greece
-- date: 5 Apr 2020
--------------------------------------------------------------------------------------
tell application "Finder"
if exists Finder window 1 then
set currentFolder to target of Finder window 1 as alias
set currentFolderPath to quoted form of POSIX path of currentFolder
else
return
end if
end tell
tell application "iTerm" -- wait until window "zsh" appears as frontmost
activate
repeat until window "zsh" exists
delay 0.02
end repeat
end tell
-- go to current location
tell application "System Events" to keystroke "cd " & currentFolderPath & return
-- wait while current session of current tab of window of iTerm is busy (changes current directory)
tell application "iTerm" to tell current session of current tab of window "zsh"
repeat while (is processing) is true
delay 0.02
end repeat
end tell
-- clear all window contents, except the last line
tell application "System Events" to keystroke "k" using command down
NOTE:
Although iTerms is preferable to use with zsh shell as default, you can use it when the current account has bash shell as default too. In this case you can use the same script, replacing the name of window “zsh” with “bash”.
I find today none GUI-scripting solution (used command write text, similar to do script of Terminal.app):
----------------------------------- iTerm2.app ----------------------------------
-- script: Clear terminal contents & Go to indicated directory
-- by: Kniazidis Robert, Greece
-- updated: 11 Apr 2020
--------------------------------------------------------------------------------------
tell application "Finder"
if not (exists Finder window 1) then return
set currentFolder to target of Finder window 1 as alias
end tell
tell application "iTerm"
activate
repeat until window 1 exists
delay 0.02
end repeat
tell current session of current tab of window 1
write text "cd " & quoted form of POSIX path of currentFolder & ";clear"
end tell
end tell