Display last line of last active terminal (iterm2)

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
      }
    }

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).

1 Like

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

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, ...

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

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.

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

cool :+1: thanks!

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

it works perfect :clap: . thank you!

thanks for your awesome work!
If I want to display the last 2 (or maybe 3) lines, what changes should be made to the code?

Also: Wouldn't it be also possible to use shell code on this? The tail command would turn the code into a one-liner.

edit:
after some experimenting, here is my solution. Maybe it helps someone else too:

tell application "iTerm"
   set varcont to contents of current session of current window
   set thescript to "echo \"" & varcont & "\" | awk 'NF > 0' | tail -n2"
end tell

do shell script (thescript)

Source: http://hints.macworld.com/article.php?story=20030805175235977

Unfortunately the Script to prevent the app from opening iTerm does not work for me :frowning:

Edit: It works now! Unsure of what I did differently, but thanks for the widget!

1 Like

Although this preset doesn't seem to get a huge amount of use, I found it to be exactly what I needed!

I went ahead and combined @oliviermattelaer's and @crabman's suggestions (along with the original preset) into the following script:

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

tell application "iTerm"
	tell current session of current window
		set myprofile to (profile name)
		if myprofile = "Default" then
			set outvalue to contents
		end if
	end tell
end tell


if myprofile is not "Default" then
	tell application "iTerm"
		set i to 0
		repeat with aWindow in windows
			if profile name of current session of aWindow = "Default" then
				set outvalue to contents of current session of aWindow
				exit repeat
			end if
			
		end repeat
		#display dialog length of application
	end tell
end if

return do shell script "tail -r << ITERM_END_OF_CONTENT | sed -nE '/[^[:space:]]/{ s/^[[:space:]]+|[[:space:]]+$//gp; q; }'
" & outvalue & "
ITERM_END_OF_CONTENT"

And finally, here is the new, full preset!

{
  "BTTPresetName" : "iTerm",
  "BTTPresetColor" : "61.526400, 233.721440, 236.640000, 255.000000",
  "BTTPresetUUID" : "7741048C-9960-4F7D-BFD9-F3726BF216BE",
  "BTTPresetContent" : [
    {
      "BTTAppBundleIdentifier" : "BT.G",
      "BTTAppName" : "Global",
      "BTTAppAutoInvertIcon" : 1,
      "BTTTriggers" : [
        {
          "BTTWidgetName" : "iTerm",
          "BTTTriggerType" : 639,
          "BTTTriggerTypeDescription" : "Apple Script Widget",
          "BTTTriggerClass" : "BTTTriggerTypeTouchBar",
          "BTTPredefinedActionType" : 49,
          "BTTPredefinedActionName" : "Launch Application \/ Open File \/ Start Apple Script …",
          "BTTLaunchPath" : "file:\/\/\/Applications\/iTerm.app",
          "BTTEnabled2" : 1,
          "BTTUUID" : "A40A440B-A002-4F92-9AD9-EDF26876006A",
          "BTTNotesInsteadOfDescription" : 0,
          "BTTEnabled" : 1,
          "BTTModifierMode" : 0,
          "BTTOrder" : 84,
          "BTTDisplayOrder" : 0,
          "BTTMergeIntoTouchBarGroups" : 0,
          "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",
              "BTTEnabled2" : 1,
              "BTTAlternateModifierKeys" : 0,
              "BTTUUID" : "A21C22D3-C0D0-4FA1-A268-E8E1F29765AE",
              "BTTNotesInsteadOfDescription" : 0,
              "BTTEnabled" : 1,
              "BTTModifierMode" : 0,
              "BTTOrder" : 0,
              "BTTDisplayOrder" : 0,
              "BTTMergeIntoTouchBarGroups" : 0
            }
          ],
          "BTTTriggerConfig" : {
            "BTTTouchBarAppleScriptString" : "if application \"iTerm\" is not running then\r\treturn \"$_\"\rend if\r\rtell 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\rreturn do shell script \"tail -r << ITERM_END_OF_CONTENT | sed -nE '\/[^[:space:]]\/{ s\/^[[:space:]]+|[[:space:]]+$\/\/gp; q; }'\n\" & outvalue & \"\nITERM_END_OF_CONTENT\"",
            "BTTTouchBarButtonColor" : "13.052275, 129.472945, 12.817138, 255.000000",
            "BTTTouchBarItemPlacement" : 0,
            "BTTTouchBarAppleScriptStringRunOnInit" : true,
            "BTTTouchBarAlwaysShowButton" : false,
            "BTTScriptType" : 0,
            "BTTTouchBarScriptUpdateInterval" : 1,
            "BTTTouchBarButtonWidth" : 100,
            "BTTTouchBarItemIconHeight" : 22,
            "BTTTouchBarButtonTextAlignment" : 0,
            "BTTTouchBarAlternateBackgroundColor" : "255.000000, 38.028511, 0.000000, 255.000000",
            "BTTTouchBarAppleScriptUsePath" : 0,
            "BTTTBWidgetWidth" : 400,
            "BTTTouchBarColorRegex" : "(\\]\\$|>|\\]\\#)\\s*$",
            "BTTTouchBarItemIconWidth" : 22,
            "BTTTouchBarButtonName" : "iTerm",
            "BTTTouchBarButtonFontSize" : 15,
            "BTTTouchBarOnlyShowIcon" : false,
            "BTTTouchBarFreeSpaceAfterButton" : "5.000000",
            "BTTTouchBarItemPadding" : 0
          }
        }
      ]
    },
    {
      "BTTAppBundleIdentifier" : "com.apple.finder",
      "BTTAppName" : "Finder",
      "BTTAppAutoInvertIcon" : 1,
      "BTTTriggers" : [

      ]
    }
  ],
  "BTTPresetSnapAreas" : [

  ]
}

Thanks for all of the ideas, and I hope that this modification proves useful! :slight_smile:

2 Likes

I decide to left my code here as well, maybe some one will found it useful. Mainly, idea the same, few changes:

  • I lock profile to use Hot Key Window, but not to not use active one. You could change the name in variable 'profile'.
  • Implement logic to show either last or line above last. So if last line is empty or waiting for input command logic will return previous line.

Hope it will help someone.

Apple Script:

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

	set profile to "Hotkey Window"
	set consoleText to getIterm(profile)
	set output to getLine(consoleText, 2)

	if the length of output < 3 or output contains computer name of (system info) then
		set output to "🔼: " & getLine(consoleText, 3)
	else
		set output to "⏺: " & output
	end if

	return output

	on getIterm(profile)
		tell application "iTerm"
			set i to 0
			repeat with aWindow in windows
				if profile name of current session of aWindow = profile then
					set outValue to contents of current session of aWindow
					exit repeat
				end if
			end repeat
		end tell
		return outValue
	end getIterm

	on getLine(consoleText, lineNumber)
		return do shell script "tail -r << ITERM_END_OF_CONTENT | sed '" & lineNumber & "q;d'
	" & consoleText & "
	ITERM_END_OF_CONTENT"
	end getLine
1 Like
Imprint | Privacy Policy