error "iTerm got an error: TERM environment variable not set."

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.

The problem is: the iTerms.app doesn’t have do script command like Terminal.app.
So, your script even doesn’t compile.

KniazidisR. Thanks for trying that. It’s of no use, so I’ll delete my post.

Now, assuming we use already the zsh shell as default, instead of the bash shell:

  1. The iTerms.app doesn’t have do script command like Terminal.app to do script telling directly to iTerms.app
  2. 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”.

Thanks everyone

Works perfect.

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