How to keep terminal window open in AppleScript after end tell

applescriptiterm

I have this osascript with a command

tell application "iTerm2"
  create window with default profile command "uwm"
end tell

Now the iTerm window closes at end tell but I want to keep iTerm window open to see my command result and work further. Any hints?

Best Answer

If you want the iTerm2 window to stay open and active for further use, you cannot use command with create window with default profile.

You need to use the write command in proper context, e.g.:

tell application "iTerm2"
    create window with default profile
    tell current session of window 1 to write text "echo hello"
end tell

The above example AppleScript code used with osascript:

osascript -e 'tell application "iTerm2"' -e 'create window with default profile' -e 'tell current session of window 1 to write text "echo hello"' -e 'end tell'
  • Replace "echo hello" in the example AppleScript code with your command, e.g.: "uwm"

Note: osascript can execute the example AppleScript code from a file using, e.g.:

osascript /path/to/filename.applescript

From the iTerm2 AppleScript Dictionary:

write v : Send text as though it was typed.

     write specifier : The session to send to
           [contents of file file] : Filename to send the contents of
           [text text] : Text to send
           [newline boolean] : If newline should be added to end of text (default: yes)



Note: The example AppleScript code is just that and does not employ any error handling and is meant only to show one of many ways accomplish a task. The onus is always upon the User to add/use appropriate error handling as needed/wanted.