AppleScript to make window wide enough to not scroll?

178 views
Skip to first unread message

TJ Luoma

unread,
May 30, 2021, 6:15:05 PM5/30/21
to BBEdit MailingList

Hi all, my name is TJ and I don't understand AppleScript, hence I come, again, asking for help from those who do.

I like to set "Soft Word Wrap" to "Window Width".

But then I often find that I want to adjust the width of the window so that it is _just_ wide enough to not need a horizontal scroll bar.

Tonight it occurred to me that this would be a good place for automation, but I can't figure out if that's possible, and Google is no help because it just tells me how to change word wrap or other word-wrap related things that are not the thing I'm looking for.

So it's possible that there's no way to do this, or there is a way and I just wasn't able to find the right search terms.

Anyone know if this can be done, or should I start a campaign to have it added to BBEdit 14 ;-?

Thanks, all.

~ Tj




Bruce Van Allen

unread,
May 30, 2021, 8:42:43 PM5/30/21
to BBEdit MailingList
On 30 May 2021, at 15:14, TJ Luoma wrote:

> But then I often find that I want to adjust the width of the window so
> that
> it is _just_ wide enough to not need a horizontal scroll bar.

I don’t recall a scriptable way to relate line length to window width.
Different font sizes and non-monospaced fonts would make it complicated,
for one thing.

But if you were satisfied with *visual* rather than programmatic
determination of the right width, you could use a script that simply
enlarged the window width in steps each time it was called, eventually
cycling back to “original” size. With an assigned keyboard shortcut,
you would simply press the shortcut until the window has stepped up to
the width you want.

I use a script like that to widen tabs, to help in viewing tabular data
with wider “columns”.

Something like this, but there are others on this list who can surely
improve it:

##
tell application "BBEdit"
set thisWin to text window 1
set oldBounds to bounds of thisWin
--For help when setting your min and max widths,
--uncomment the next line to see the window width:
--display dialog item 3 of oldBounds
set minWidth to 800
set maxWidth to 1600
set increment to 100
set newBounds to oldBounds
set curWidth to item 3 of oldBounds
if curWidth ≥ maxWidth then
set item 3 of newBounds to minWidth
else
set item 3 of newBounds to (item 3 of oldBounds) + increment
end if
set bounds of thisWin to newBounds
end tell

##

Save that in your BBEdit Scripts folder, and assign it a keyboard
shortcut.

Thanks,

- Bruce

_bruce__van_allen__santa_cruz__ca

Christopher Stone

unread,
May 31, 2021, 4:35:33 AM5/31/21
to BBEdit-Talk
On 05/30/2021, at 17:14, TJ Luoma <luo...@gmail.com> wrote:
Hi all, my name is TJ and I don't understand AppleScript, hence I come, again, asking for help from those who do.

I like to set "Soft Word Wrap" to "Window Width".

But then I often find that I want to adjust the width of the window so that it is _just_ wide enough to not need a horizontal scroll bar.

Tonight it occurred to me that this would be a good place for automation, but I can't figure out if that's possible, and Google is no help because it just tells me how to change word wrap or other word-wrap related things that are not the thing I'm looking for.


Hey TJ,

There's probably only one place on the whole Internet to find that.


The original script works for me on macOS 10.12.6 Sierra, but there's a change to AppleScript in High Sierra that has to be adjusted for.

Rich kindly added sidebar width to BBEdit's AppleScript lexicon after I wrote this, but I have not (as yet) changed the script to adapt to its open/closed state.

Presently the script expects the sidebar to be closed when run.

If you can't follow the thread and get it running on your system let me know.  I can reproduce the issue on my MacBook Air (Mojave), so I should be able to fix it without too much trouble.

If I have time I might look at it late Monday.


--
Take Care,
Chris

TJ Luoma

unread,
May 31, 2021, 9:27:55 AM5/31/21
to BBEdit MailingList

Hi Chris

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 


FWIW, I don't use the sidebar. All of my BBEdit windows are one-document each (unless I'm doing a mass-search-and-replace, and then I close that windo as soon as I'm done).

I also use 'Inconsolata-g' 16 as my font size (used to be 13, but not long after I got a 4K monitor I finally got tired of always having to increase the zoom level on all my BBEdit windows so I finally caved and increased the default font size :-) 

~ Tj





--
This is the BBEdit Talk public discussion group. If you have a feature request or need technical support, please email "sup...@barebones.com" 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 bbedit+un...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/bbedit/11C1632C-D694-402A-9E53-8E44B07923A3%40gmail.com.

Christopher Stone

unread,
Jun 1, 2021, 3:22:27 PM6/1/21
to BBEdit-Talk
On 05/31/2021, at 08:27, TJ Luoma <luo...@gmail.com> 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

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

TJ Luoma

unread,
Jun 1, 2021, 7:25:46 PM6/1/21
to BBEdit MailingList
On Tue, Jun 1, 2021 at 3:22 PM Christopher Stone <listmei...@gmail.com> wrote:

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

You are a prince among men.

Thank you! Thank you! Thank you! 

~ Tj

p.s. - I have an "always on" M1 Mac Mini running Big Sur (obviously). If you'd like Screen Sharing access to it for testing purposes, let me know. It would be easy enough to set up, and I thought it might be useful until you get your own B.S. Mac. You've already got my email address.




Christopher Stone

unread,
Jun 26, 2021, 8:23:58 PM6/26/21
to BBEdit-Talk
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 screenWithMenuFrameSizeas 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 screenWithMenuFrameSizeas integer
end if

set longestLineLength to 0
set maxLineContent to missing value

set lineCounter to 0

set {oldTIDSAppleScript's text item delimitersto {AppleScript's text item delimiterslinefeed}

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 {nully1nully2to (get its bounds)
set its bounds to {0, y1screenWidthy2}

else

set {x1y1nully2to (get its bounds)
set newBounds to {x1y1x1 + newWindowWidthy2}
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 {oldTIDSAppleScript's text item delimitersto {AppleScript's text item delimiters"."}
set baseSystemVersion to (text items 1 thru 2 of systemVersionas text
set AppleScript's text item delimiters to oldTIDS
if baseSystemVersion > 10.12 then
return true
else
return false
end if
end systemGreaterthanSierra
-------------------------------------------------------------------------------------------

TJ Luoma

unread,
Jun 26, 2021, 9:32:27 PM6/26/21
to bbe...@googlegroups.com

On Jun 26, 2021 at 8:23:54 PM, Christopher Stone <listmei...@gmail.com> wrote:
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.

This works great so far! I will give it additional testing next week, but on first tests it seems to be working great.

It does leave a horizontal scrollbar, but there’s nowhere to scroll it. If I enlarge the window by just a tiny bit (as little as I can), it goes away. I’m not sure if that’s something that can easily be changed (just to make the scrollbar disappear) but if not that’s really a minor nit.

Thank you!

~ Tj


Christopher Stone

unread,
Jun 26, 2021, 10:29:27 PM6/26/21
to BBEdit-Talk
On 06/26/2021, at 20:32, TJ Luoma <luo...@gmail.com> wrote:
I’m not sure if that’s something that can easily be changed (just to make the scrollbar disappear) but if not that’s really a minor nit.


Tweak one of these properties until its wide enough:

property gutterWidth : 64
property gutterPad : 18
property scrollbarWidth : 16


-Chris
Reply all
Reply to author
Forward
0 new messages