On 05/31/2021, at 08:27, TJ Luoma <[email protected] <mailto:[email protected]>> 
wrote:
> I had a feeling you'd know the answer to this one… because you had already 
> written it :-)
> 
> Unfortunately I'm getting an error that I don't understand at all. I pasted 
> the full message here 


Hey TJ,

Okay, here's a revised script that's working on Mojave with BBEdit 13.5.6.

I believe the changes I made should make the script viable through Big Sur, but 
I can't be certain without testing.

I bias my windows slightly left-of-center.  If you want to completely center 
then look for a comment with “comment-out” in the script and follow the 
instructions.

The window is not quite shrunken to it's minimal proportions with BBEdit 13.x – 
it's a few pixels off.  I assume this is because the UI element widths I used 
for BBEdit 12.x are a bit different in v13, but I haven't bothered to tweak it 
as yet.

See the values for:

gutterWidth
gutterPad
scrollbarWidth

Adjust as needed.

Enjoy.

--
Take Care,
Chris

-------------------------------------------------------------------------------------------
# Auth: Christopher Stone { Kudos to Shane Stanley for his generous help with 
ASObjC }
# dCre: 2018/09/16 22:24
# dMod: 2021/06/01 13:47
# 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.7" -- macOS 10.14 or later
use framework "Foundation"
use framework "AppKit"
use scripting additions
-------------------------------------------------------------------------------------------
property fontName : "Inconsolata-g"
property fontSize : "16.0"
-------------------------------------------------------------------------------------------

set screenWithMenu to current application's NSScreen's screens()'s 
objectAtIndex:0
set screenWithMenuFrameSize to (screenWithMenu's frame())
set screenHeight to (item 2 of (item 2 of screenWithMenuFrameSize)) as integer
set screenWidth to (item 1 of (item 2 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 < 400 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)

            # Comment-out this line if you want the window perfectly centered.
            set item 1 of winPos to (newXPosition - 64) -- I like a left offset.

            set its position to winPos
            
        end if
        
    end tell
end tell

-------------------------------------------------------------------------------------------

-- 
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/838485BA-F062-4372-BDB7-5FC8601055AD%40gmail.com.

Reply via email to