BBEdit, Tail Mode, and bbtail

A new version of BBEdit, 14.5, was released a couple of days ago, and the feature that caught my eye was Tail Mode. It’s a way of viewing a log file in a way that persistently updates as new text is added to the file. Lots of processes send information (errors, connections, etc.) to log files, and when you’re debugging code, it’s often very useful to be able to see the log file update in real time. That’s what Tail Mode is for.

macOS has the Console app for doing this, but I’ve never liked Console. There’s also the old-school way of using the “follow” option to the tail command in a Terminal window:

tail -f /var/log/system.log

It is this use of tail that gives Tail Mode its name.

What I like about Tail Mode is that it works like tail -f but by doing so in a BBEdit window, all my ingrained habits for searching and copying text will work, which they don’t in Terminal or iTerm.

To turn on Tail Mode, you can choose the View‣Text Display‣Tail Mode menu item or bring up the Text Options window and check the Tail Mode box.

Text Options

You can also set a file’s language to Log File, which will turn Tail Mode on without any other action from you. By default, files with a .log extension are opened with their language set to Log File and Tail Mode on.

Yesterday, Mark Gardner had a great idea:

Already emailed asking for a bbedit -f or bbtail command

Although I have a feeling Rich Siegel will soon have a bbtail command to go along with bbdiff and bbfind, I couldn’t wait. Here’s my combination shell script/AppleScript:

bash:
 1:  #!/bin/zsh
 2:  
 3:  # Get the absolute path to the file argument.
 4:  f="$1:a"
 5:  
 6:  # Open the file in BBEdit and go to the end. By setting the source
 7:  # language to Log File, the Text Display will be set to Tail Mode.
 8:  # This works for BBEdit 14.5 and later.
 9:  osascript << OSAEND
10:  tell application "BBEdit"
11:    activate
12:    open "$f"
13:    tell front text window
14:      set source language to "Log File"
15:      set lastChar to length of contents
16:      select character (lastChar + 1)
17:    end tell
18:  end tell
19:  OSAEND

I think the comments explain everything reasonably well. I’ll just link to the parts of the zsh user manual that cover the :a modifier used in Line 4 to expand the filename argument to its full absolute path and the << redirection that creates a here-document out of the AppleScript code between the OSAEND strings and feeds it to the osascript command.

As far as I can tell, BBEdit doesn’t have an AppleScript for setting Tail Mode directly, but by setting the language to Log File in Line 14, I achieve the same result.

I suppose I could have done this entirely in AppleScript with an on run routine, but I don’t know how to get an absolute path from a partial path in AppleScript.