AppleScript: Failure to create new iTerm2
Right after tell application "iTerm" add:

if (window count) is equal to 0 then reopen


This will open a new window if none are open but iTerm is running.



You can also use the following example AppleScript code to make sure window 1 exists before continuing, if one wasn't already open.

if (window count) is equal to 0 then
    reopen
    repeat until exists window 1
        delay 0.01
    end repeat
end if




By the way, if window 1 exists but is minimized, your code will still run in the minimized window. If that's an issue, then you'll need to code for that scenario or just always open a new window to run your code in, e.g:

set myWindow to (create window with default profile)
repeat until exists myWindow
    delay 0.01
end repeat