error getting toolbar items

I’m trying to use a toolbar in my project for the first time. I can get a button from a toolbar with no trouble using the “clicked” action. But I need to put a search field and a popup button in the toolbar too… and I’m getting errors while trying to access them.

Here’s what I have… for example with the popup button:
In interface builder while in column mode I can go down the chain of the items and look at the top of the inspector window to get the type of item and here’s what I see along with the name that I gave each item (if I gave it a name it’s in quotes).

window “mainWindow” → toolbar → toolbar item “searchType” → pop up button “searchTypeValue” → pop up button cell → menu → menu item

So now I need to access the currently selected menu item of the popup button. When the popup button was in a regular window I could get it with

set searchType to contents of popup button "searchTypeValue" of window "mainWindow".

So now that it’s in the toolbar I try the following all with the same error stating “can’t get popup button… error -1728”.

set searchType to contents of popup button "searchTypeValue" of toolbar of window "mainWindow"
set searchType to contents of popup button "searchType" of toolbar of window "mainWindow"
set searchType to contents of popup button "searchTypeValue" of toolbar item "searchType" of toolbar of window "mainWindow"

Can somebody help me reference this object correctly?

If you look at post #3 here Bruce said the above quote. I can access buttons but nothing else, I even tried a text field. So maybe anything other than buttons aren’t accessible right now. I guess I have to take the search field and the popup button out of the toolbar.

Note: buttons can be accessed as follows…

toolbar item "buttonName" of toolbar of window 1

If you have any other ideas or know how to do this in ObjC let me know.

Thanks for posting this, useful to know.

I would also be interested in workarounds via Objective C etc. as I was also hoping to add a search field to the toolbar I created via Interface Builder. I have been able to activate the toolbar buttons just fine. I access them through the regular “on clicked” handler by checking the name of the button.

However, I confirm your problems with the search box. When, for example, adding a simple “keyboard up” handler that logs something or displays a dialog, you get the following:

Can’t make «class » id 36 into type reference. (-1700)

I get similar results for code added for every other handler I tried to add to the search box.

Hi all!

I’ve dragged and text field to my toolbar but now… How can I pick up the text from there to a variable?

I’ve already tryed:

set var1 to contents of text field "var1f" of toolbar of window 1 set var2 to contents of text field "var2f" of toolbar of window 1
but it doesn’t work at all, I end up with this error:
“Can’t get «class texF» “var1f” of «class tbar» of window 1. (-1728)”

Help please…!

Here’s an easy way to get a reference for an object. Hook the object (i.e. the text field) up to “awake from nib” in IB.

property var1fRef : missing value

on awake from nib theObject
   if name of theObject is "var1f" then
      set var1fRef to theObject
   end
end

Now you have a reference that you can use in your code. So your code should be…

set var1 to contents of var1fRef

hi, thanks by you help… but I can’t just get it work… when I add you code… I end up with this error:
“Can’t make «class tooI» id 1 of «class tbar» of window id 2 into type «class utf8». (-1700)”

My question is… " window id 2" there’s no windows 2 on my project only one window! --.–‘’

Anyway… I think that it isn’t working because I’ve that text filed and a button on my project… and my script is this for now:



property var1fRef : missing value


on awake from nib theObject
	if name of theObject is "var1f" then
		set var1fRef to theObject
	end if
end awake from nib


on clicked theObject

        set var1 to contents of var1fRef
	say var1

end clicked

So… theoretically it should work… but… it doesn’t work at all :stuck_out_tongue:

My second question is… if the text filed is under “on awake from nib” this process will only happen when I open up the app… so then if i change the text it will work?

What can I do?

Thanks!

Have you made var1fRef a property or global variable. You can’t use it in the “on clicked” handler if you haven’t.

property var1fRef : missing value

hi again… sorry I’ve posted it wrong

I’ve placed on the begin of my code (out of any handler xd) like you said:

property var1fRef : missing value

but… Can you explain it to me better… I’m new to the property stuff :stuck_out_tongue: hihi

When you create a variable it is a “local” variable by default. That means that the variable is only good in the handler that you use it. Right now you have 2 handlers, “awake from nib” and “on clicked”. So the local variable var1fRef that you create in awake from nib is only available in that handler. In order to use it in all handlers throughout your project you have to make it a “global” variable.

In order to make a variable global you can either 1) define it as a property at the top of your script or 2) declare it global at the top of your script… in applescript studio it makes no difference which one you use. I usually use a property so that I can assign global variables some initial value. In this case we gave it “missing value” and as such I would use it in my code like this to avoid errors…

if var1fRef is not missing value then
   -- use the variable
else
   log "var1fRef does not have a value yet"
end

You could use this at the top of your script instead of the property if you didn’t need an initial value. This would make all the variables global too.

global variable1, variable2, variable3

Thanks for your great explantion, now I’m understanding the problem… or maybe not…

So…

I’ve tryed… but… as property or global I always get the same error message…

So I’ve created a new project and put just that code… here go some screen shots of my actual problem :wink:

Code and Interface Builder Design:
http://img382.imageshack.us/img382/8476/imagem4.png

App running without click anything:
http://img40.imageshack.us/img40/7636/imagem5g.png

Afther enter some text and click on the set button this happens:

http://img83.imageshack.us/img83/7641/imagem6z.png

So…? But the text change if I change my original text and then click on the button…

What’s the problem?

So I’ve do some search under Apple Website about toolbars in applescript studio… and I can’t found anything… :frowning: seem like this is a new thing only available after leopard…

Thanks for the upcoming help :wink: hihi

It looks like it’s getting the reference to the toolbar item fine. Your problem is that when you get the “contents” of it that you are getting the reference to the toolbar item back instead of the contents of the text field. I think you can fix that… just change the word contents to content. There’s a slight difference between contents and content. I think in this case “content” will work for you.

set var1 to content of var1fRef

I’ve changed it to “content”, like you said but… when I click the button:

“Can’t get «class conT» of «class tooI» id 1 of «class tbar» of window id 2. (-1728)”

”.”’

Well then it seems that a text field won’t work in a toolbar created in Interface Builder. You are doing everything right and it still doesn’t work. In 10.5 apple added the ability to create a toolbar in IB, and using that in an applescript studio project has not been very reliable. Prior to 10.5 toolbars were created manually. So at this point your only other option would be to go the manual route if you absolutely need a text field in the toolbar. You can search the forums on how to do that.

Yes… ok seems like IB and Xcode doesn’t like too much toolbars in AppleScript :wink:

So… yes I’ve already started searching how to build it using the old method… Seems like it’s a little bit more complicated… but I will try to use it…

Anyway thanks by all your time and dedication to my problem :wink: