Hey Tj,

Here is the newest version of the script.

CHANGES:

    - macOS versions Sierra through Big Sur are supported automatically.
    - The font name and font size are managed automatically.
    - It sets the document magnification level to 1 if necessary.

--
Take Care,
Chris

-------------------------------------------------------------------------------------------
# Auth: Christopher Stone { Kudos to Shane Stanley for generous help with 
ASObjC }
# dCre: 2018/09/16 22:24
# dMod: 2021/06/26 19:08
# 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
# Vers: 2.00
-------------------------------------------------------------------------------------------
use AppleScript version "2.5" -- macOS 10.13 or later
use framework "Foundation"
use framework "AppKit"
use scripting additions
-------------------------------------------------------------------------------------------
property minimumLineWidth : 500
property gutterWidth : 64
property gutterPad : 18
property scrollbarWidth : 16
-------------------------------------------------------------------------------------------

if systemGreaterthanSierra() = true then
        set screenWithMenu to current application's NSScreen's screens()'s 
objectAtIndex:0
        set screenWithMenuFrameSize to screenWithMenu's frame()
        set screenWidth to (item 1 of item 2 of screenWithMenuFrameSize) as 
integer
else
        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
end if

set longestLineLength to 0
set maxLineContent to missing value

set lineCounter to 0

set {oldTIDS, AppleScript's text item delimiters} to {AppleScript's text item 
delimiters, linefeed}

tell application "BBEdit"
        if exists of front text window then
                tell front text window
                        display magnification
                        if display magnification ≠ 1 then set display 
magnification to 1
                        set displayFontName to display font
                        set displayFontSize to display font size
                        set lineContentList to text items of (get its text)
                end tell
        end if
end tell

set AppleScript's text item delimiters to oldTIDS

repeat with i in lineContentList
        set lineCounter to lineCounter + 1
        set lineLength to length of i
        if lineLength > longestLineLength then
                set longestLineLength to lineLength
                set longestLineNumber to lineCounter
        end if
end repeat

tell application "BBEdit"
        tell front document
                set widestLineContent to contents of line longestLineNumber's 
text
        end tell
end tell

set theFont to current application's NSFont's fontWithName:displayFontName 
|size|:displayFontSize
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:widestLineContent attributes:theAtts)
set theSize to atrString's |size|()
set lineWidth to round (width of theSize)

if lineWidth < minimumLineWidth then
        error "Text content is too narrow to resize the window!"
end if

set newWindowWidth to lineWidth + gutterWidth + gutterPad + scrollbarWidth

--» Set the Window Width.
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

-------------------------------------------------------------------------------------------
--» HANDLERS
-------------------------------------------------------------------------------------------
on systemGreaterthanSierra()
        set systemVersion to (get system info)'s system version
        set {oldTIDS, AppleScript's text item delimiters} to {AppleScript's 
text item delimiters, "."}
        set baseSystemVersion to (text items 1 thru 2 of systemVersion) as text
        set AppleScript's text item delimiters to oldTIDS
        if baseSystemVersion > 10.12 then
                return true
        else
                return false
        end if
end systemGreaterthanSierra
-------------------------------------------------------------------------------------------

-- 
This is the BBEdit Talk public discussion group. If you have a feature request 
or need technical support, please email "[email protected]" rather than 
posting here. Follow @bbedit on Twitter: <https://twitter.com/bbedit>
--- 
You received this message because you are subscribed to the Google Groups 
"BBEdit Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/bbedit/3FC66707-8414-4AF3-B294-28BFA9AC60E4%40gmail.com.

Reply via email to