How to get selected item from NSPopUpButton?

property myPopUp : missing value on startbuttonpressed_(sender) if myPopUp selectedItem = "Item 1" display dialog "This is Item 1" else display dialog "Failed" end if end startbuttonpressed_ 

I compiled this code successfully, but received the message "Failed", although I selected "Item 1".
I think my error is "myPopUp selectedItem", but I don't know how to fix it. <How to get the selected item from NSPopUpButton?

+4
source share
2 answers

If you look at the NSPopUpButton documentation , you can see all the methods you can name and what they inherit from. In the Getting User Selection section, you have:

 – selectedItem – titleOfSelectedItem – indexOfSelectedItem – objectValue 

Of course, these are all methods, so if you want to get the index of the selected value, you must call if you want:

 set my_index to myPopup indexOfSelectedItem() 

Looking at the indexOfSelectedItem entry in the docs:

 indexOfSelectedItem Returns the index of the item last selected by the user. - (NSInteger)indexOfSelectedItem Return Value The index of the selected item, or -1 if no item is selected. 

We get an overview of the function from above, and then using the function, and finally a description of the return value. This tells us that indexOfSelectedItem does not accept any parameters (if it would look like - (NSInteger)indexOfItemWithTitle:(NSString *)title ). The return value on the left will be NSInteger, NOT Applescript Integer. Although Applescript may be able to handle it the same way, in some situations this may give you problems. The solution is to never consider NSString as an AS String and never consider NSInteger as an AS Integer. To do the conversion, we would have to change it to an AS string, and then to an AS integer:

 set my_index to ((myPopup indexOfSelectedItem()) as string) as integer 

So for your code, if it looks like this, you can use either indexOfSelectedItem or titleOfSelectedItem

+14
source

The if condition should be like this:

 if (myPopup titleOfSelectedItem()) = "Item 1" then 
+2
source

Source: https://habr.com/ru/post/1495296/


All Articles