![]() ![]() Of course, echo-ing various text is not that useful, that was just to demonstrate the concept. This time you should get two tabs opened with the first one having run echo hello and the second one having run echo world. Go ahead and use the Open Quickly window again ( Cmd + Shift + O) to run daily-startup.py again. Edit your script as follows and save it: #!/usr/bin/env python3.7 import iterm2 async def main ( connection ): app = await iterm2.async_get_app(connection) window = app.current_terminal_window if window is not None : # Open a tab await window.async_create_tab() # Get the active session in this tab session = app.current_terminal_window.current_tab.current_session # Send text to the session as though the user had typed it await session.async_send_text( 'echo hello \n ' ) # Open another tab await window.async_create_tab() session = app.current_terminal_window.current_tab.current_session await session.async_send_text( 'echo world \n ' ) else : print ( "No current window" ) n_until_complete(main) The newline character \n must be appended to each command to actually submit it. To do this, we need to get the current session associated with each tab, and then use the async_send_text method to send text through to the tab as if a user had entered it directly into the terminal. Now that we know the script is working, let's go ahead and open another tab, and run some commands in each tab. The script should run and you should see a new tab open. Make sure the focus is in iTerm when you do this: To launch it, enter Cmd + Shift + O, then start typing the name of the script, when its highlighted, hit Enter to run it. ![]() There are several different ways to run a script but the easiest is to use the "Open Quickly" window. It doesn't do any real work yet but let's do a quick test and run it just to make sure everything is setup correctly. n_until_complete(main) Run ScriptĬurrently, all the daily-startup.py script does is to open a new tab in the current terminal window. # When main returns the program terminates. print ( "No current window" ) # Make a connection to iTerm2 and invoke the main function in an asyncio event loop. app = await iterm2.async_get_app(connection) # Fetch the “current terminal window” from the app (returns None if there is no current window) window = app.current_terminal_window if window is not None : # Add a tab to current window using the default profile await window.async_create_tab() else : # You can view this message in the script console. ![]() async def main ( connection ): # Get a reference to the iterm2.App object - a singleton that provides access to iTerm2’s windows, # and in turn their tabs and sessions. This is required because # iTerm2 communicates with the script over a websocket connection, # any time the script sends/receives info from iterm2, it has to wait for a few milliseconds. I've added a few comments to explain what each part does: #!/usr/bin/env python3.7 # Import the iterm2 python module to provide an interface for communicating with iTerm import iterm2 # All the script logic goes in the main function # `connection` holds the link to a running iTerm2 process # `async` indicates that this function can be interrupted. This will open the script in an editor with a basic skeleton already filled in. Next choose the Basic environment (Full Environment is only for more complex scripts that need to install other packages, that won't be necessary for simply opening tabs and running commands):Ĭhoose Simple script template (because the script we will write will simply run its commands and then exit, it doesn't need to stay open listening for iTerm events):įinally, give your script a name to save it, I name mine daily-startup.py but you can choose any name you like: If prompted, go ahead and download the Python runtime: ![]() To get started, make sure iTerm is open, then select from the menu: Scripts -> Manage -> New Python Script as shown below: This post will walk you through how to achieve this automation using iTerm and Python. Do you find yourself opening many iTerm tabs every day to do the same daily routine such as opening editors, running a build, starting up various services etc? If yes, good news, this tedious daily startup routine can be automated, saving you precious minutes each day. ![]()
0 Comments
Leave a Reply. |
Details
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |