You are not logged in.
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.
Applescript:
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.
Applescript:
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.
Applescript:
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:
Applescript:
{"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:
Applescript:
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:
Applescript:
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:
Applescript:
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.
Applescript:
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.
Applescript:
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:
Applescript:
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.
Applescript:
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:
Applescript:
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
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
It would be worth to incorporate this tip http://www.macscripter.net/viewtopic.ph … 42#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.
2)
Applescript:
on launched theObject
tell window 1
...
show
end tell
end launched
Offline