How do I populate a popup button

Okay, this subject has been addressed here (and elsewhere) before, but only in relation to AppleScript Studio. I’m trying to do this in Xcode and I’m lost:

script t2t_AppDelegate
	property parent : class "NSObject"
    
    tell application "Application"
        ...
    end tell

        tell window 1
            tell menu of popup button 1
                delete every menu item
                repeat with catListItem in catList
                    make new menu item at end of menu items with properties {title:catListItem}
                end repeat
            end tell
        end tell

	on applicationWillFinishLaunching_(aNotification)
		-- Insert code here to initialize your application before any files are opened 
	end applicationWillFinishLaunching_
	
	on applicationShouldTerminate_(sender)
		-- Insert code here to do any housekeeping before your application quits 
		return current application's NSTerminateNow
	end applicationShouldTerminate_
	
end script

catList is a valid list created in the first “tell” block. I’m getting an error when trying to build the project: “t2t_AppDelegate.applescript:23: error: Expected end of line but found identifier. (-2741)” – line 23 is “tell menu…”

Any suggestions? I’m guessing I need to wrap “tell window” in something else.

The applicationWillFinishLaunching_ and applicationShouldTerminate_ handlers suggest you’re creating an AppleScriptObjC application, but the rest of the code suggests you’re trying to use AppleScript Studio code in it. That’s not going to work – the two are entirely different.

Thanks for your help? I did post in the AppleScriptObj and Xcode forum, I did note that I’m using Xcode, and I mentioned that the code I posted was based on AppleScript Studio code found elsewhere on this site.

As this is my first foray into using Xcode, I presumed that a Cocoa-AppleScript Application could in fact use AppleScript. I’m quite willing to be told different, but I was hoping for an education in how I could populate a popup button this application.

You use AppleScript, but not like you do in AppleScript Studio – there is no object model of interface items to address. If you have a popup button in your window, you need to define a property in your script, set to missing value, and then connect it as an outlet in the interface (control-click-drag from the item to the application delegate icon, let the mouse button up, then click next to the property in the dialog that appears.)

The code part consists essentially of calling Objective-C methods using AppleScript. So if your property is called myPopup, it would be something like:

myPopup's removeAllItems()
myPopup's addItemsWithTitles_(listOfTitles)

You might find the viso on this page useful:

macosxautomation.com/applescript/develop/index.html

Okay, that makes sense. Hmm. It looks like my property declaration isn’t right:

property popCatList : missing value

The syntax coloring in Xcode has “missing” in purple and “value” in black. And this property isn’t showing as an outlet. This goes in the AppDelegate script, right?

Aargh! This is frustrating. I started over. New project. popCatList briefly showed as an outlet for the application delegate, then disappeared after I created the popup button.

New project. This time, I was able to connect popCatList to the Pop Up Button. However, there’s a warning icon on the connection and the tool tip states that my AppDelegate script doesn’t have an outlet named popCatList. Odd, since it let me connect it to the outlet to being with.

Where are you putting the property? It sounds like you’re doing the right thing…

It’s in my AppDelegate script:


script t2t_AppDelegate
    property parent : class "NSObject"
    property popCatList : missing value
    
    tell application "Application"
        ...
    end tell
    
    set catList to {"1","2","3"}
    
    popCatList's addItemsWithTitles_(catList)
	
	on applicationWillFinishLaunching_(aNotification)
		-- Insert code here to initialize your application before any files are opened 
	end applicationWillFinishLaunching_
	
	on applicationShouldTerminate_(sender)
		-- Insert code here to do any housekeeping before your application quits 
		return current application's NSTerminateNow
	end applicationShouldTerminate_
	
end script

catList is set in the first tell block, but I’m overriding it here for testing.

That tell block will never be called – it’s not in a handler. Put it inside the applicationWillFinishLaunching_ handler.

Okay, I’ve been treating this project like “normal” AppleScript. I’ll have to give some more attention to Sal’s site (the video you pointed me to was great!) and get up to speed on the right way to do things.

I took that chunk of code, put it where you suggested and now I’ve got the popup I want. Thanks!