Skip to content

Instantly share code, notes, and snippets.

@TylerLeonhardt
Created September 12, 2019 05:55
Show Gist options
  • Save TylerLeonhardt/e9360149cd41518d6ec2b5eb3bf8868d to your computer and use it in GitHub Desktop.
Save TylerLeonhardt/e9360149cd41518d6ec2b5eb3bf8868d to your computer and use it in GitHub Desktop.
Invoke a PowerShell script in your iTerm2 Status Bar
#!/usr/bin/env python3.7
import iterm2
import asyncio
import subprocess
# Icons are base64-encoded PNGs. The first one is 32x34 and is used for Retina
# displays. The second is 16x32 and is used for non-Retina displays.
ICON2X = "iVBORw0KGgoAAAANSUhEUgAAACAAAAAiCAYAAAA+stv/AAAABGdBTUEAALGPC/xhBQAAAWJJREFUWMPtVzsOgkAQRRIas4UdsTI0FhzDG1BwChrOYW1nTaPewsIL2JDY2HgBKisdk0cyEl2XWcniZ5KXEJiZ99hdZgbP+5udnQBndgG+T8CAMHyjgCFyGpMvCVuCeuE7BnSmkGtpKiIkHPFmTREBISUUhJJQASXupfBpkl+QMzRdhYiJWOFeQjiwZX+GA3w9xNbkUdtzcAvYEaaEOSPYE3JCjDdUuM7xrPabI3YnIedWk58JGcHX+PrwOTMRVpYw8lmLuBkTkUjJA7bnmSA+Y2cikAhI2Z77gnifnYlUIqBAcG6xhTlyFJLgEsGxhYAYOUrTrsa7W4VgZSFAIUel4bmr6by2dyHgEc9dTee1vYsteMTT30Po/DN0Xoicl2Lnzej2CS0II4t2PEIOJSGvJ5m1xUCyeTJZGZM3J5m2I1mkGe9eChCNUZrxrvUqTN74ZzSxLOk/+Gf0MQKMulqv7Qpwm6+awd/XXAAAAABJRU5ErkJggg=="
ICON = "iVBORw0KGgoAAAANSUhEUgAAABAAAAARCAYAAADUryzEAAAABGdBTUEAALGPC/xhBQAAAK9JREFUOMtjYKAxqIRiskE9FBMFvIDYEYgZoXxJIDaHYkmoGEjOCYg9sRnABMQWQKwDxLuA+BoQz4Ti61AxkEZ+qFqsgAuIbwNxGZoiJqjYbaganKANiOfikZ8LVYMTnAFiYzzyxlA1GFH1GYnmwWMAD5pacBSzQiVYSXABsh7qhgHFsaAExHZ40gFIThGX5iAg1gBiZmiKk4T62RgpJYLkYoBYd3BmJmyAFVtUIQMAZuAlWgiKRrsAAAAASUVORK5CYII="
async def foo(connection):
app = await iterm2.async_get_app(connection)
icon = iterm2.StatusBarComponent.Icon(1, ICON)
icon2x = iterm2.StatusBarComponent.Icon(2, ICON2X)
# Register the status bar component.
component = iterm2.StatusBarComponent(
short_description="Invoke PowerShell",
detailed_description="Invokes a PowerShell script.",
knobs=[iterm2.statusbar.StringKnob("Script", "Enter script here...", "'Hello World!'", "Script")],
exemplar=">_",
update_cadence=None,
identifier="com.tylerleonhardt.pwsh",
icons=[icon,icon2x])
@iterm2.StatusBarRPC
async def bar(knobs):
"""Invoke PowerShell and grab the stdout."""
proc = subprocess.run(["/usr/local/bin/pwsh", "-NoLogo", "-NoProfile", "-c", knobs["Script"]], capture_output=True)
if proc.stdout:
return proc.stdout.rstrip().decode('ascii')
return "Failed..."
# Register the component.
await component.async_register(connection, bar)
asyncio.sleep(5)
iterm2.run_forever(foo)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment