![]() 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. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |