• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Resize Width of Front BBEdit Window According to Longest Line and then Center
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Resize Width of Front BBEdit Window According to Longest Line and then Center


  • Subject: Resize Width of Front BBEdit Window According to Longest Line and then Center
  • From: Christopher Stone <email@hidden>
  • Date: Sun, 16 Sep 2018 23:05:28 -0500

Hey Folks,

I use plain text files in BBEdit for many things and frequently want to resize
them 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.

--
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/16 22:57
# 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)

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

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

 _______________________________________________
Do not post admin requests to the list. They will be ignored.
AppleScript-Users mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
Archives: http://lists.apple.com/archives/applescript-users

This email sent to email@hidden

  • Follow-Ups:
    • Re: Resize Width of Front BBEdit Window According to Longest Line and then Center
      • From: Christopher Stone <email@hidden>
  • Prev by Date: Read and Write Data Types?
  • Next by Date: Re: Resize Width of Front BBEdit Window According to Longest Line and then Center
  • Previous by thread: Read and Write Data Types?
  • Next by thread: Re: Resize Width of Front BBEdit Window According to Longest Line and then Center
  • Index(es):
    • Date
    • Thread