add horizontal scroll bar to text view


Is there a way to add a horizontal scroll bar to a text view using applescript? If not, is there a way to do this using call method?



Text views by default come inside of a scroll view, which means they already have both horizontal and vertical scroll bars… so there’s nothing to add.

You can change the properties of the scroll view in interface builder. Just select the text view in your window and initially on the first click you will have the scroll view selected (it takes a second click to actually select the text view). You can tell what you have selected by looking at the top of the inspector window. It should say ‘Scroll View Attributes’ when you have the scroll view selected. In that inspector there’s choices so you can show or hide the scroll bars.

OK, sorry, I forgot to mention that I was using Xcode 2.3. I checked Xcode 3 and I found everything exactly how you said. Is there a work around for 2.3?



I haven’t used version 2.x of xcode for awhile but it should be very similar. I know text fields are still inside of scroll views by default so you should be able to see the scroll view attributes and make any needed adjustments.

I understand that a text view is in a scroll view. And Interface Builder should have an option for a horizontal scroller. They do have it in Xcode 3. But THERE IS NO OPTION in the panel on Xcode 2.5 or earlier. Screenshot: There must be a way to set a horizontal scroller from applescript. I tried everything that I could think of. When I was wading through the obj-C docs in Xcode, I found this:

  • (void)setHasHorizontalScroller:(BOOL)flag

I used it in the on launched handeler:

call method "setHasHorizontalScroller:" of (scroll view "scroll" of window "main") with parameters {true}

It worked to some extent, since it showed a horizontal scroll bar, but it didn’t let me scroll when a line of text reached the right side of the text view. Screenshot: Is there any other option for setting a horizontal scroller? It can’t be very hard.



Maybe this will help. Your command looks a little off… try this one:

call method "setHasHorizontalScroller:" of (scroll view "scroll" of window "main") with parameter YES

Scroll views should have the horizontal scroller by default. They only show when your text is wider than the field. They don’t show normally. Before doing the above command you may want to check if your scroll view already has one with:

set doesItHaveOne to call method "hasHorizontalScroller" of (scroll view "scroll" of window "main")
log doesItHaveOne

I think also by default a text view will wrap the lines of text. If it does this then you will never see the horizontal scroller because the text wraps and thus you will only ever see a vertical scroller. But you can probably change the wrapping style in IB such that it doesn’t wrap.

I was thinking about this today and looked. I couldn’t find a way to turn off the automatic wrapping of a text view in IB. I think this is your ultimate problem. You’ll never see a horizontal scroller with wrapping. So I made an objective-c class to turn wrapping off. It was a little more difficult than I thought but I got it to work. Here’s what to do.

In Xcode add a new file to your project from the file menu. Choose “objective-c class” as the file type. I named mine TextViewWrap. You’ll get an “.h” and “.m” file in your project. Add the following code to those two files:

==== TextViewWrap.h ====
#import <Cocoa/Cocoa.h>

@interface TextViewWrap : NSObject {

+(void)turnWrappingOff:(NSTextView *)textView;

==== TextViewWrap.m ====
#import “TextViewWrap.h”

const float LargeNumberForText = 1.0e7;

@implementation TextViewWrap
+(void)turnWrappingOff:(NSTextView *)textView {
// configure the scroll view
NSScrollView *scrollView = [textView enclosingScrollView];
[scrollView setHasVerticalScroller:YES];
[scrollView setHasHorizontalScroller:YES];

// configure the text container
NSTextContainer *textContainer = [textView textContainer];
[textContainer setWidthTracksTextView: NO];
[textContainer setHeightTracksTextView:NO];
NSSize size = [textContainer containerSize];
size.width = LargeNumberForText;
size.height = LargeNumberForText;
[textContainer setContainerSize: size];

// configure the text view
NSRect frame = [textView frame];
[textView setMinSize:frame.size];
[textView setMaxSize:NSMakeSize(LargeNumberForText, LargeNumberForText)];
[textView setHorizontallyResizable:YES];
[textView setVerticallyResizable:YES];


Then in IB connect your text view to this handler and add this applescript code…

on awake from nib theObject
	if name of theObject is "textView" then
		call method "turnWrappingOff:" of class "TextViewWrap" with parameter theObject
	end if
end awake from nib

That’s it. See if it works.

Excellent!!! :smiley: It works! I’m really glad, I had a lot of the functionality of my project based on the horizontal scrolling. Thank you!
Before I saw your post, I found this in the apple mailing lists: I don’t know if this is a plausible solution to my problem, it seems simpler, but I don’t know obj-c…

Thanks again,


That’s basically the same thing I did but it’s missing many details… which is why it looks easier. But once you add in the missing details it would pretty much be the same. It’s also missing one important detail that affects the way the scrolling works. I set a min and max size of the text view and what that does is automatically adjusts to scroll bar based on the length of the actual text that is in the field. That code just makes a big text container and the effect of that would mean your text view would have a scroller whether or not one is needed and there would be lots of white space after your text. It wouldn’t dynamically adjust itself to the width of your text.

Test my code. It scrolls exactly to the longest line and no more. Then if you make the text shorter or longer it auto-adjusts to fit the text. That code won’t do that.

Anyway I’m glad it works. :slight_smile:

Thanks Hank, this works a treat! I don’t understand why this feature isn’t built in.

As a ASOC newbie, I was pleased to be able to convert this AppleScript code:

on awake from nib theObject if name of theObject is "textView" then call method "turnWrappingOff:" of class "TextViewWrap" with parameter theObject end if end awake from nib
to this:

on awakeFromNib() tell class "TextViewWrap" of current application its turnWrappingOff_(textView) end tell end awakeFromNib
I don’t know if this is the best way, but it might help some other newbies out there.


Hank, I recently had some correspondence with you regarding your excellent ChangeFileDates command line tool - cheers for all your help with that. You seem to have fingers in lots of coding pies. :smiley: