Display last line of last active terminal (iterm2)

touch-bar

#1

Hi,

I wanted to have on my touchbar the last line of my shell displayed such that I can loosely control what is happening.

  1. This is also color coded (green when the terminal is working and red when is waiting at shell prompt.
  2. This ignore the hotkey shell
  3. when you click on it, you will open the associate window

This requires iterm2 but you do not need to have shell integration activated.

Enjoy,

Olivier

  {
      "BTTWidgetName" : "last line of iterm2",
      "BTTTriggerType" : 639,
      "BTTTriggerTypeDescription" : "Apple Script Widget",
      "BTTTriggerClass" : "BTTTriggerTypeTouchBar",
      "BTTPredefinedActionType" : -1,
      "BTTPredefinedActionName" : "No Action",
      "BTTEnabled2" : 1,
      "BTTEnabled" : 1,
      "BTTOrder" : 6,
      "BTTAdditionalActions" : [
        {
          "BTTTriggerType" : -1,
          "BTTTriggerClass" : "BTTTriggerTypeTouchBar",
          "BTTPredefinedActionType" : 172,
          "BTTPredefinedActionName" : "Run Apple Script (blocking)",
          "BTTInlineAppleScript" : "tell application \"iTerm\"\r\ttell current session of current window\r\t\tset myprofile to (profile name)\r\t\tif myprofile = \"Default\" then\r\t\t\treturn\r\t\tend if\r\tend tell\rend tell\r\rif myprofile is not \"Default\" then\r\ttell application \"iTerm\"\r\t\tset i to 0\r\t\trepeat with aWindow in windows\r\t\t\tset i to i + 1\r\t\t\tif profile name of current session of current tab of aWindow = \"Default\" then\r\t\t\t\tselect aWindow\r\t\t\t\treturn\r\t\t\t\texit repeat\r\t\t\tend if\r\t\t\t\r\t\tend repeat\r\t\t#display dialog length of application\r\tend tell\rend if",
          "BTTShellTaskActionScript" : "tell application \"iTerm\"\r\ttell current session of current window\r\t\tset myprofile to (profile name)\r\t\tif myprofile = \"Default\" then\r\t\t\treturn\r\t\tend if\r\tend tell\rend tell\r\rif myprofile is not \"Default\" then\r\ttell application \"iTerm\"\r\t\tset i to 0\r\t\trepeat with aWindow in windows\r\t\t\tset i to i + 1\r\t\t\tif profile name of current session of current tab of aWindow = \"Default\" then\r\t\t\t\tselect aWindow\r\t\t\t\treturn\r\t\t\t\texit repeat\r\t\t\tend if\r\t\t\t\r\t\tend repeat\r\t\t#display dialog length of application\r\tend tell\rend if",
          "BTTEnabled2" : 1,
          "BTTEnabled" : 1,
          "BTTOrder" : 0
        },
        {
          "BTTTriggerType" : -1,
          "BTTTriggerClass" : "BTTTriggerTypeTouchBar",
          "BTTPredefinedActionType" : 49,
          "BTTPredefinedActionName" : "Launch Application \/ Open File \/ Start Apple Script …",
          "BTTLaunchPath" : "file:\/\/\/Applications\/iTerm.app",
          "BTTEnabled2" : 1,
          "BTTEnabled" : 1,
          "BTTOrder" : 1
        }
      ],
      "BTTTriggerConfig" : {
        "BTTTouchBarItemIconHeight" : 22,
        "BTTTouchBarItemIconWidth" : 22,
        "BTTTouchBarItemPadding" : 0,
        "BTTTouchBarFreeSpaceAfterButton" : "5.000000",
        "BTTTouchBarButtonColor" : "13.052275, 129.472945, 12.817138, 255.000000",
        "BTTTouchBarAlwaysShowButton" : "0",
        "BTTTouchBarAppleScriptString" : "tell application \"iTerm\"\r\ttell current session of current window\r\t\tset myprofile to (profile name)\r\t\tif myprofile = \"Default\" then\r\t\t\tset outvalue to contents\r\t\tend if\r\tend tell\rend tell\r\r\rif myprofile is not \"Default\" then\r\ttell application \"iTerm\"\r\t\tset i to 0\r\t\trepeat with aWindow in windows\r\t\t\tif profile name of current session of aWindow = \"Default\" then\r\t\t\t\tset outvalue to contents of current session of aWindow\r\t\t\t\texit repeat\r\t\t\tend if\r\t\t\t\r\t\tend repeat\r\t\t#display dialog length of application\r\tend tell\rend if\r\rset endline to \"\n\"\rset startpos to -3\rset stoppos to -1\rset lastline to \"\"\rrepeat while length of lastline < 2\r\trepeat while lastline does not start with endline\r\t\tset startpos to startpos - 1\r\t\tset lastline to text startpos thru stoppos of outvalue\r\tend repeat\r\tstoppos = startpos\r\tset lastline to trimThis(lastline)\rend repeat\r\r\r\r\rset outvalue to lastline\rset terminal to \"]$\"\r\rreturn lastline\r\r\ron trimThis(someText)\r\tset endline to \"\n\"\r\t\r\trepeat until (someText does not start with \" \" and someText does not start with endline and length of someText > 2)\r\t\ttry\r\t\t\tset someText to text 2 thru -1 of someText\r\t\ton error\r\t\t\treturn \"\"\r\t\tend try\r\tend repeat\r\t\r\trepeat until (someText does not end with \" \" and someText does not end with endline and length of someText > 2)\r\t\tset someText to text 1 thru -2 of someText\r\tend repeat\r\t\r\treturn someText\rend trimThis\r",
        "BTTTouchBarColorRegex" : "(\\]\\$|>|\\]\\#)\\s*$",
        "BTTTouchBarAlternateBackgroundColor" : "255.000000, 38.028511, 0.000000, 255.000000",
        "BTTTouchBarScriptUpdateInterval" : 2
      }
    }

#2

Thank you, this seems to work fine here.

I have slightly edited your post to put the JSON into the three markdown ticks ``` so it is highlighted correctly.

Probably on Sunday I will provide a much better way to include presets here in this forum. I will then convert your post accordingly :slight_smile:

(There will be a helper service, share.folivora.ai where people can upload json files or paste their code. This will give the uploader a link he can share. On this link it will automatically show the content of the shared preset an auto-import button and the "developer verified" status).


#3

Is this also possible for the normal Terminal.app? That would be awesome :heart_eyes:


#4

As far as i know the Terminal.app does not support applescript as deeply as iTerm2 and it is not possible to have such feature in Terminal.

I would actually strongly recommend iTerm2 instead of Terminal. You have so many feature that are available for iTerm2 including overlay terminal, password manager, ...


#5

Good point. I just switched to iTerm now :sunglasses:.


#6

Thanks for the tip. Use iTerm now and find it great. There is one more thing. Maybe you can give me a hint ...
Currently starts when BTT also starts iTerm.app automatically. Is it possible to set it so that it only starts when iTerm opens or through a button etc.


#7

Ok, this is not a problem for me since I have a terminal open 100% of the time.
I will see (tonight or tommorow) how to prevent iterm to open automatically with BTT.

Cheers,

Olivier


#8

cool :+1: thanks!


#9

You can add this at the begining of the script to prevent the app to open iTerm

if application "iTerm" is not running then
	return " "
end if

Cheers,

Olivier


#10

it works perfect :clap: . thank you!