Changing A Window's Content Using A Custom View

Hey guys, I hope I’m not asking too much, but I wanted to load different views into a custom view, so that I can change the content of my window, without ‘nasty’ visual tricks, like having one box over another, and hiding the boxes. I was wondering if this was possible. I can’t find any reference on the net, except for on the applescript definitions site at ADC it says something like ‘it is possible to swap out the content of a window using a view’. I’ve posted a screenshot of the 3 windows that I’ve set up, 1 which is the window, with a large custom view, and the other 2 being the two custom views which I want to load into the view.
Oh also, if this is possible, how would I address items within the loaded view. would it be like
item “x” of view “” of box “” of window “”
or just:
item “x” of box “” of window “” (as if it was a window, not a view).

Switching out a content view is a little bit unnecessary for most every purpose. This is ideally what a tab view is for. You can create a tab view with no border, tabs, or background, and then easily switch between tabs to get your content to change. You can use any kind of control to change the current tab view item, or you can do it programmatically. To show a particular tab view item, use something like…

tell tab view "myTabView" of window "someWindow"
	set current tab view item to tab view item "someTabViewItem"
end tell

For more information check out the apple developer doc for tab view’s. I wouldn’t recommend trying to deal with switching out whole content views. Apple’s window doc doesn’t recommend it either.


Okay, but what if you need it this way?
Is there a way to do it without having to use the NSTabView?

Let’s say you have a small window with about 6 possible view contents.
6 tab item titles don’t fit into a small window - but the content views just have a few elements (so that it would look silly in a larger window).

And there are dozens of apps which do it the non-tab way.

cheers for the reply jobu, I think I’ll do it the tab view method. But is it possible to do it with the custom views? Things like the login panel for Mac OS X use custom views and swap views in and out. But I think that isn’t written in AppleScript!

Obviously it’s not written in AS “ obviously not even possible in vanilla AS.

But is there a Objective-C (but compatible with ASS apps) method to do this?

Actually, switching out the content view of a window is easy. You simply need to use the ‘content view’ property of the window and pass it a valid content view. The best method would be to create an “extra” window which contains the contents you want to switch to. You can then use a couple lines of code to switch it out…

tell window "Window2" to set contentView2 to content view of it
tell window "Window1" to set content view to contentView2

What you’ll find, though, is that apple warns of doing this for a reason. The content view of a window is not meant to be switched out repeatedly in ASS. In fact, I can’t get it to do it more than once. It only is allowing me to change it out one time, and all subsequent attempts are ignored. There may be some workarounds to this problem, either through some AS trickery or by using obj-c methods… but I simply don’t see the value. Both tomhennigan and Vincent, you seem so hell-bent on doing it this way. Is there some particular reason, or are you just curious? There are of course lots of ways to achieve certain things, and how apple does it… or any other app does it, for that matter… may not necessarily be the best or correct way of doing it. Apple has a history of doing strange things, just because they can. And they often break their own guidelines and recommendations… just because they can. How did you come to the conclusion that they are switching out whole content views in the login screen? They may be swaping subviews of a parent nsview, but I doubt that they are swapping out the whole window’s content view.

If you have a real reason why you need to actually swap out the content view of the window, then please elaborate on what you’re trying to achieve. If you’re looking for a particular effect or capability that you can not get by some other easier means in AS, then it may actually be worth looking into further. Otherwise, most of the features you’d want should be available to you with the tools you’ve already got.


I don’t want to change the content of a window but of a custom view somewhere in the window!

Please excuse my very late answer. I somehow forgot about it as I thought I had subscribed to this topic, but actually hadn’t. :frowning:

I want to write a small floating tool with a small window (maximally width: 350; height:350).
The problem is that it shall offer about five (maybe more) functions of which each shall have several options given.

I could make a small window with “links” to open a seperate window for each of these features.
That would solve this issue - but that is what I absolutelly don’t want!

My reasons:

Just One window because:
It shall be “tool” which’s window floats above another app.
If I had several windows they would cover the whole desktop - no way!
If I had a larger window it would cover large parts of the desktop - no way!
And the app’s look shall be compact! (I’m already using GUI elements in “mini” look)

I could use a tabview but the tabitems would take too much width.
I’d like to use a toolbar - but then I couldn’t swap the content via a tabview!

I’m a bit confused. do you realize that you can have a tab view without visible tabs?

Edit: Also, to be sure, you’re wanting something comparable to Interface Builder’s “palletes” or the inspector in Pages?


Also, you could use a toolbar to switch between tab view items.

You can still use tabs without them taking up any window real estate. In IB add a NSTabView to your window. With the tab view selected (not the tab view ITEM), make it tabless.

As a quick test I created a tab view with 4 tab view. In each tab view I placed a text box that had a number in it so I could tell which tab view I was looking at. Below this I added 2 buttons (on the main window – not in the tab(s)). I connected the buttons to the tab view so that the first button took me to the next tab view item and the 2nd button took me to the previous tab view item. The nice thing is this didn’t require a single line of code.

If you don’t want to use buttons to go from one tab view item to the next, you could switch views using code based on options selected in the interface.

[Edit: Bruce just beat me to this. lol]

Hope this helps,
Brad Bumgarner, CTA

Bruce, Brad you’re my heros!

I somehow never realized the feature of hiding the tabs. :rolleyes:

I’ll use this!