Getting Started with AppleScript Studio - Buttons (Part 1)

Last month, we began discussing how to get started with using AppleScript Studio (click here for last month’s column). In the column, we covered creating an AppleScript Studio project in Xcode, designing a user interface, linking the interface to AppleScript code within the project, and building and running the completed project.

This month, we are going to continue to work with AppleScript Studio. Specifically, we will be focusing on interacting with buttons.

There are many different styles of buttons in AppleScript Studio, including push buttons, popup buttons, checkboxes, radio buttons, and more. In addition to varying visually in appearance, the code you will write to interact with different buttons will vary too, depending on the button type. We are going to begin by exploring two of the more commonly used styles of button – push buttons and popup buttons.

Preparing to Follow Along

In order to follow along with the examples, you are encouraged to create a new AppleScript Studio project. Begin by launching Xcode (installed with the Mac OS X Developer Tools). Next, select New Project… from the File menu, and create a new project named ‘Button Demo’ using the AppleScript Application project template that is included with Xcode.

Within the project, double click on MainMenu.nib to open the project’s interface in Interface Builder. Next, design the main window of your project’s interface to contain two push buttons and a popup button, as shown in figure 1.

Figure 1. Example Interface

While we could modify the titles of the push buttons and the contents of the popup button within Interface Builder, we won’t be doing this now. Rather, we will write AppleScript code to perform these functions during the execution of our script. We will, however, assign AppleScript names to the push buttons at this time, so that we can accurately target them from within our project code. We will also configure the buttons to respond to specific events.

Select the first push button in the main interface window. Assign an AppleScript name of button1 to the button in the AppleScript pane of the Inspector palette, and connect its clicked event handler to the Button Demo.applescript script in the project. See figure 2. Repeat this same process for the second push button, but assign a name of button2 instead.

Figure 2. Assigning an AppleScript Name and Clicked Event Handler to a Push Button

Next, select the popup button in the main interface window. For this button, we will not assign an AppleScript name. However, we will assign an event handler. Connect the popup button’s choose menu item event handler to the Button Demo.applescript script in the project. See figure 3.

Figure 3. Assigning a Choose Menu Item Event Handler to a Popup Button

Next, we will configure code to execute when our project is launched. To do this, select File’s Owner in the MainMenu.nib window in Interface Builder, and connect its launched event handler to the Button Demo.applescript script in the project. See figure 4.

Figure 4. Assigning a Launched Event Handler to a Project

We’re now ready to begin adding AppleScript code to the project, which will interact with the buttons in our interface. Save the interface, and click the Edit button at the bottom of the AppleScript pane of the Inspector palette. This should bring Xcode to the front, and the Button Demo.applescript script should be displayed, containing three event handlers – launched, clicked, and choose menu item – which should appear as follows:


on launched theObject
	(*Add your script here.*)
end launched

on clicked theObject
	(*Add your script here.*)
end clicked

on choose menu item theObject
	(*Add your script here.*)
end choose menu item

If you are so inclined, go ahead and build and run the project to test out the interface. Keep in mind that, because we have not added any code within the event handlers of our script, nothing will happen when you interact with the interface at this time.

Push Buttons

Setting the Title of a Push Button

We have not yet assigned titles to the push buttons within our interface. This will be done dynamically when our project is launched. To make this happen, we will add code to the launched handler of our script, which will set the value of each button’s title property. For example, the following code will set the title of the first button to Cancel, and the title of the second button to Process.


on launched theObject
	tell window 1
		set title of button "button1" to "Cancel"
		set title of button "button2" to "Process"
	end tell
end launched

Build and run the project, and verify that the titles of these two push buttons are properly assigned, as shown in figure 5.

Figure 5. Dynamically Titled Buttons in an Interface

Clicked Event Handler

Next, we will configure the clicked handler within our script to execute specific AppleScript code when a button is clicked. If the Cancel button is clicked, we want the script to quit. If the Process button is clicked, we want the script to display a dialog.

Using an if/then statement, we can check the title of the button that was clicked, in order to determine the proper course of action to take. The following code demonstrates how this may be done:


on clicked theObject
	if title of theObject = "Cancel" then
		quit
	else if title of theObject = "Process" then
		display dialog "Processing..."
	end if
end clicked

Build and run the project, and verify that clicking each push button results in the desired action being taken.

Popup Buttons

Populating Popup Buttons

We are now ready to write code to populate the popup button on our interface. To do this, we first need to create a list of values, which can be added to the popup button as menu items. For this demonstration, we will use the following list:


{"Menu Item 1", "Menu Item 2", "Menu Item 3", "Menu Item 4", "Menu Item 5"}

By default, any popup button will contain at least one pre-existing menu item. Therefore, in order to ensure that there are no stray menu items in our popup after it has been populated with our new menu items, the first thing we will want to do is remove any existing menu items. This can be done by deleting every existing menu item, as follows:


tell menu of popup button 1
	delete every menu item
end tell

Please note that, as demonstrated in the example code above, menu items within a popup button are actually contained within the menu of a popup button, rather than within the popup button itself.

To add multiple new menu items to a popup button, use a repeat statement, which will use the make command, in order to create a new menu item in the popup button during each cycle of the repeat loop. For example:


tell menu of popup button 1
	repeat with aMenuItem in thePopupItems
		make new menu item at end of menu items with properties {title:aMenuItem}
end repeat
end tell

Now, in order to integrate these techniques into our specific project, they should be merged together and inserted into the launched handler within our project’s code. The modified launched handler should appear as follows:


on launched theObject
	tell window 1
		set title of button "button1" to "Cancel"
		set title of button "button2" to "Process"
		
		set thePopupItems to {"Menu Item 1", "Menu Item 2", "Menu Item 3", "Menu Item 4", "Menu Item 5"}
		tell menu of popup button 1
			delete every menu item
			repeat with aMenuItem in thePopupItems
				make new menu item at end of menu items with properties {title:aMenuItem}
			end repeat
		end tell
	end tell
end launched

Build and run the project, and verify that the popup button contains the correct dynamically created menu items, as shown in figure 6.

Figure 6. A Dynamically Populated Popup Button

Setting the Current Menu Item of a Popup Button

Using the technique above to add menu items to a popup button, you will find that the default selected menu item will be the first menu item in the popup button. In the case of our project, this would be “Menu Item 1”. To change the selected menu item of a popup button, you may set the title property of the popup button to the title of the menu item that you want to be selected.


set title of popup button 1 to "Menu Item 3"

Another way this may be done is by setting the current menu item property of the popup button to the specified menu item, using its index (positioning in the list of menu items). For example, the following code would set the current menu item to the third menu item of the popup button.


tell popup button 1
	set current menu item to menu item 3
end tell

Retrieving Values from a Popup Button

To retrieve the titles of all menu items within a popup button, request their titles, as follows:


tell popup button 1
	title of every menu item
end tell
--> {"Menu Item 1", "Menu Item 2", "Menu Item 3", "Menu Item 4", "Menu Item 5"}

To retrieve the title of the currently selected menu item, request the title of the popup button’s current menu item.


tell popup button 1
	title of current menu item
end tell
--> "Menu Item 3"}

Choose Menu Item Event Handler

Now, let’s integrate some of this code into our project. We are going to insert code into the choose menu item event handler, which will display the title of the current menu item whenever the user makes a selection from the popup button. The modified choose menu item event handler should appear as follows:


on choose menu item theObject
	display dialog (title of current menu item of theObject) as string
end choose menu item

Build and run the project, and verify that the code within the choose menu item event handler is executed when you make a selection from the popup button.

In Conclusion

Now that we have explored some basic techniques for interacting with push buttons and popup buttons in AppleScript Studio projects, you are encouraged to begin incorporating these techniques into your own projects. If you encountered any problems while following along, you are welcome to download an example AppleScript Studio project, which incorporates all of the code within this column. You may download this project here.

As we proceed with exploring AppleScript Studio, we will continue to discuss techniques for interacting with push buttons and popup buttons, as well as other types of buttons.

Until next time… just script it!

-Ben Waldie

It would be worth to incorporate this tip http://www.macscripter.net/viewtopic.php?pid=102842#p102842 into this tutorial so that buttons don’t change their labels on users’ eyes. I. e.:

  1. In Interface Builder uncheck the box “Visible at Launch” in the window attributes section of the inspector.
on launched theObject
    tell window 1
        ...
        show
    end tell
end launched