I have the following AppleScript code in a Run AppleScript action in Automator:
on run {input, parameters}
set appName to "iTerm"
if application appName is not running then
tell application appName to activate
end if
tell application "iTerm"
tell current session of current tab of current window
write text "cd Desktop"
split horizontally with default profile
split vertically with default profile
end tell
tell second session of current tab of current window
write text "whoami"
end tell
tell third session of current tab of current window
write text "ls -al"
split vertically with default profile
end tell
tell fourth session of current tab of current window
write text "echo Hello World"
end tell
end tell
return input
end run
However the application runs okay only if there is an iTerm instance already open. It will not open a new one for me.
How should I modify it so that it spins up a new iTerm window and executes the four session split and command execution?