Hey Folks,
I use plain text files in BBEdit for many things and frequently want to resize their windows according to the maximum line length of the content.
I finally got fed up with doing this manually and dug into AppleScriptObjC enough to script it.
I’ve subtracted the gutter width back out of the center equation so the text content is centered.
It looks like it’s not possible to directly detect whether BBEdit’s sidebar is open or not – much less detect its width – so I’ve got a feature suggestion in to the guys at Bare Bones.
I realize it’s possible to pole View > Window Appearance > Show/Hide Sidebar for its status, but this is crude and cannot reveal the width of the sidebar.)
If the guys at Bare Bones won’t expose the sidebar to AppleScript then I’ll probably get peeved enough to use Keyboard Maestro’s Find Image on Screen action to find unique elements on each side of it to calculate its width.
In the meantime the workaround is to close the sidebar before running the script if necessary.
–
Best Regards,
Chris
----------------------------------------------------------------
# Auth: Christopher Stone { Kudos to Shane Stanley for his generous help with ASObjC }
# dCre: 2018/09/16 22:24
# dMod: 2018/09/17 00:24
# Appl: BBEdit
# Task: Resize width of front BBEdit window according to longest line and center.
# Libs: None
# Osax: None
# Tags: @Applescript, @Script, @ASObjC, @BBEdit, @Resize, @Width, @Front, @Window, @Longest, @Line, @Center
----------------------------------------------------------------
use AppleScript version "2.4" -- macOS 10.10 or later
use framework "Foundation"
use framework "AppKit"
use scripting additions
----------------------------------------------------------------
property fontName : "Menlo-Regular"
property fontSize : "14.0"
----------------------------------------------------------------
set screenWithMenu to current application's NSScreen's screens()'s objectAtIndex:0
set screenWithMenuFrameSize to (screenWithMenu's frame()'s |size|()) as list
set screenWidth to (item 1 of screenWithMenuFrameSize) as integer
set maxLineLength to 0
set maxLineContent to missing value
tell application "BBEdit"
tell front text window
repeat with theLine in its lines
if (length of theLine) > maxLineLength then
set maxLineLength to (length of theLine)
set maxLineContent to (contents of theLine)'s contents
end if
end repeat
end tell
end tell
set theFont to current application's NSFont's fontWithName:fontName |size|:fontSize
set theAtts to current application's NSMutableDictionary's ¬
dictionaryWithObject:theFont forKey:(current application's NSFontAttributeName)
set atrString to (current application's NSAttributedString's alloc()'s initWithString:maxLineContent attributes:theAtts)
set theSize to atrString's |size|()
set lineWidth to round (width of theSize)
if lineWidth < 600 then
error "Text content is too narrow to resize the window!"
end if
set gutterWidth to 64
set gutterPad to 18
set scrollbarWidth to 16
set newWindowWidth to lineWidth + gutterWidth + gutterPad + scrollbarWidth
tell application "BBEdit"
tell front window
if newWindowWidth > screenWidth then
set {null, y1, null, y2} to (get its bounds)
set its bounds to {0, y1, screenWidth, y2}
else
set {x1, y1, null, y2} to (get its bounds)
set newBounds to {x1, y1, x1 + newWindowWidth, y2}
set its bounds to newBounds
tell newBounds
set winWidth to (its item 3) - (its item 1)
end tell
set winPos to its position
set newXPosition to round ((screenWidth - winWidth) / 2)
set item 1 of winPos to (newXPosition - 64) -- I like a left offset.
set its position to winPos
end if
end tell
end tell
----------------------------------------------------------------