.. _targeted_input_example: Targeted Input ============== iTerm2 has a "broadcast input" feature that lets you send keyboard input to multiple sessions. The purpose of this script is to send a different string to each session to which input is broadcast. This script demonstrates a few things: * Running a web server in the script process * Registering a custom toolbelt tool * Using broadcast domains * Scripts with external dependencies The web server provides the user interface that allows you to enter the text to send to each session. The web page is rendered in a custom toolbelt tool the user can choose to enable. *Broadcast domains* are the abstraction that describes how keyboard input is broadcast. Any keypress in a session belonging to a particular broadcast domain goes to all sessions in that domain. A broadcast domain is a collection of sessions, and all broadcast domains are disjoint. This script depends on the aiohttp package. To install it, you must create this script as a "full environment" script. When you select the **New Python Script** menu item, choose **Full Environment** at the first prompt. After it is created, run the following command (supposing you named it **TargetedInput**; if it has a different name, modify the path below appropriately): .. code-block:: bash ~/Library/ApplicationSupport/iTerm2/Scripts/TargetedInput/iterm2env/versions/*/bin/pip3 install aiohttp Then, replace `targeted_input.py` with: .. code-block:: python #!/usr/bin/env python3 # NOTE: This script depends on aiohttp. import aiohttp import asyncio import iterm2 from aiohttp import web async def main(connection): app = await iterm2.async_get_app(connection) async def send_html(txt, request): binary = txt.encode('utf8') resp = web.StreamResponse() resp.content_length = len(binary) resp.content_type = 'text/html' await resp.prepare(request) await resp.write(binary) return resp def html_for_domain(domain): txt = '