Apple Scripting for Terminal

Hello,

I am trying to write scripting to open up terminal if its closed or new window if its already open. For some reason currently if its closed it will open up 2 windows for terminal. But if Terminal is already open it will open a new window. Please help, this is the current script I am using. I have already tried getting rid of "activate" all that does is hide the windows. Also have tried adding "in window 1" while that works for getting rid of the second window if its closed. If its open but no windows it won't work.

set theScript to "ssh " & (theusername as string) & "@shell.3537282.com"
tell application "Terminal"
activate
do script theScript
end tell

It may depend on what version of macOS you're in, but try this:

tell application id "com.apple.Terminal"
	launch
	
	tell (a reference to window 1)
		if not (exists (it where it is visible)) then ¬
			do script "" -- creates new window
			
		repeat until tab 1 is not busy
			delay 0.1
		end repeat
		
		do script theScript in it
	end tell
	
	activate
end tell
Imprint | Privacy Policy