You are not logged in.
Creating AppleScript Studio applications complete with interfaces is a great way to make your scripts user friendly, and allow users to specify information that affects how your script will behave. However, when lots of options are necessary, an interface can quickly become cumbersome. This is where tab views can become very handy. A tab view can allow you to organize your interface elements into structured groups of common elements, which can be navigated by a user selecting the appropriate tab.
Creating a Tab View
We're going to walk through the process of interacting with a tab view in a few different ways. However, before we do this, we need to create an AppleScript Studio project that contains a tab view. This particular project will be used only to demonstrate how a tab view works. However, you can take the techniques that we will discuss here, and put them to more practical use within your own AppleScript Studio projects.
Begin by creating a new AppleScript Studio project in Xcode, using the AppleScript Application template provided by Apple. Name the project Tab View Example, and save it into the desired location on your hard drive.
Next, double click on the MainMenu.nib element within your project to open the project's interface in Interface Builder. The MainMenu.nib window should now be displayed in Interface Builder. Double click on the Window instance within the MainMenu.nib window to display the project's interface window, if it's not already visible.
Click on the Cocoa Container Views button in the toolbar of the Palettes window (command + /). You should see a number of container objects, one of which is a tab view. See figure 1.
Applescript:
set content of text field 1 of tab view item 1 of tab view 1 of window 1 to "Some Text"
Setting the Selected Tab
Suppose you are creating a tab view within what will serve as a preferences window for your AppleScript Studio project. In cases like this, you may want your tab view to default to displaying a specific tab whenever the preferences window is opened. To ensure that this is the case, you will probably want to write code that will make sure that the desired tab view is displayed when the window is opened. This may be done by modifying the value of the current tab view item property of the tab view. Set the value of this property to a reference to the desired tab view item.
To demonstrate this, modify the awake from nib handler within your example project, as follows.
Applescript:
on awake from nib theObject
tell tab view 1 of window 1
set current tab view item to tab view item "tab2"
end tell
end awake from nib
When you build and run your project, the example code above should cause the second tab view item to become selected when the interface is loaded and displayed. Of course, if you wanted to use this code in the preferences window scenario that was previously mentioned, then you would want to perform this task whenever the code that displays the preferences window is executed.
Triggering Code when a Tab is Selected
Tab views can also be configured to respond to events when tabs are selected. In our example project's interface, we have configured the tab view to respond to the will select tab view item event handler. This handler will be executed once the user has selected a tab, and that tab is about to be displayed in the interface. To illustrate the functionality of the will select tab view item handler, add the following code to your example project.
Applescript:
on will select tab view item theObject tab view item tabViewItem
if name of tabViewItem = "tab1" then
set theText to text returned of (display dialog "Please enter some text:" default answer "")
set content of text field 1 of tabViewItem to theText
end if
end will select tab view item
Next, build and run the project. When the interface is displayed, the second tab should be displayed automatically by the code in the awake from nib handler. Select Tab 1, and the will select tab view item handler will be called. The code within this handler will determine if the first tab was selected, based on its name. If the first tab was selected, then you will be prompted to enter some text. The contents of the text field in the first tab view item will then be set to the specified text. See figure 5.
Applescript:
on clicked theObject
tell tab view 1 of window 1
set current tab view item to tab view item "tab1"
end tell
end clicked
Next, build and run your example project. Again, the second tab should be displayed by the awake from nib handler. However, this time, the tab view outline and the tabs themselves should not be visible. See figure 7.
Ben Waldie, Automated Workflows, LLC
AppleScript & Automator Tips & Articles - http://www.automatedworkflows.com/tips/tips.html
Mac Automation Made Simple Video Podcast Series - http://www.automatedworkflows.com/tips/podcast.html
Automator for Mac OS X Visual QuickStart Guide - http://www.automatedworkflows.com/books/books.html
Offline