Access Tab View content

Hi,

I make application which include Tab View in main window.

This Tab View contain two tabs Tab1 and Tab2.

How I put the result to Text field which located in Tab1

tell tab view "Tab1" of window of theObject
	try
		
		set contents of text field "Size" to ySize as string
		
	end try
end tell

This script does nothing. Where is problem?

Best regards,

norro

Hi,

you have also to reference the tab (tab view item)

tell tab view item 1 of tab view "Tab1" of window of theObject
	try
		set contents of text field "Size" to ySize as string
	end try
end tell

Note: for debugging it’s recommendable to comment out all try blocks to get error messages

Hello,

I don’t seem to find the right syntax to get text from edits:

These are placed

in a Box,
which is placed in the 1st Tab (out of 3)
of my TabView.

Here is my code:

on will select tab view item theObject tab view item tabViewItem
	
end will select tab view item


on clicked theObject
	tell tab view item 1 of tab view 1 of window of theObject
		set textToFind to contents of text field 1 of box 2 of tab view item 1 of tab view 1 of window 1
		set textToWrite to contents of text field 2 of box 2 of tab view item 1 of tab view 1 of window 1
	end tell
	if (name of theObject is "btnGo") then
		tell window "main"
			display dialog textToFind
		end tell		
	end if
end clicked

Not sure what to do of all this and whether the code should be in “on clicked theObject” (my action button) or in “on will select tab view”.

Can anyone help ?

on will select tab view item. is used to make certain settings just before the tab changes

you have double referencing in your code.
The tell block contains a reference to the tab view item, so you have to omit it referencing the text fields


on clicked theObject
	tell tab view item 1 of tab view 1 of window of theObject
		set textToFind to contents of text field 1 of box 2
		set textToWrite to contents of text field 2 of box 2
	end tell
	if (name of theObject is "btnGo") then
		tell window "main"
			display dialog textToFind
		end tell
	end if
end clicked

OK, great :wink:

But how do I use conditions with tabs ?
Each tab will have a different function, so it needs to call a different part of my code. I would like it to look like this :


On clicked theObject

[b]If Tab1 is selected then [/b]
    do this 
    do that
end if

[b]If Tab2 is selected then[/b]
    do something else
    do another thing
end if

And so on ... 

End clicked

What is the syntax for that kind of condition? Havent found anything on that ?

as you have only two tabs, it’s quite easy
First define a property


property isTabViewItemOne : true -- is true if tab view item 1 is selected

then connect the tab view to the handler on selected tab view item
change “firstItem” to the name of the label of the first item


on selected tab view item theObject tab view item tabViewItem
	set isTabViewItemOne to (label of tabViewItem is "firstItem")
end selected tab view item

now you have a boolean value to distinguish the tabs

on clicked theObject
	if isTabViewItemOne then
		-- do something
	else
		-- do something else
	end if
end clicked

actually, that was an example but I have 3 tabs … and might be forced to add 1 or 2 more in the next steps of my project.

Does it work the same way ?

No, a boolean value has only 2 states.
Try it this way


property currentTabLabel : ""

on selected tab view item theObject tab view item tabViewItem
	set currentTabLabel to label of tabViewItem
end selected tab view item

on clicked theObject
	if currentTabLabel is "firstItem" then
		-- do something
	else if currentTabLabel is "secondItem" then
		-- do something else
	else if currentTabLabel is "thirdItem" then
	.	
		
	end if
end clicked

Nothing works anymore, now that I’ve done that !

Sure I can switch tabs without getting an error, but I cant get text from the text fields anymore :

No result was returned from some part of this expression. (-2763)

Maybe you must use this syntax


.
 set currentTabLabel to (get label of tabViewItem)
.

well I have redone my entire script, and I confirm that this syntax just doesnt work…

I have tested it all up and down … I need to look for something else !

dont you think I can just use the “on will” handler instead, as stated here ?

for example, I force tab1 to display at opening, and then


on will select tab view item theObject tab view item tabViewItem
   if name of tabViewItem = "tab1" then
       set this and that
       blah blah
   end if
  if name of tabViewItem = "tab2" then
        do blah blah blah 
        do this & that
   end if
  if name of tabViewItem = "tab3" then
        do blah blah blah 
        do this & that
   end if
end will select tab view item

EDIT | Never mind, I’m just talking out of my A**, it cant work because I need to use “on click” for my button …
and I realized you cant have a handler inside a handler (or i havent found the way to do it!).

That depends on the purpose. If you want to make settings before the tab was changed,
for example preventing a particular tab from opening, use will select.
Otherwise use selected tab.

I tested the code and it worked perfectly, to preset the variable currentTabLabel to the current tab, I added in the will finish launching handler

set currentTabLabel to label of current tab view item of tab view 1 of window 1

Note: I recommend to name the UI elements like

tab view "options" of window "main"

instead of using index numbers

Right, I just tried that and it somehow interferes with my code.
So I found a workaround: I added a button with the same name but a different ID for each tab, that way I can play with my code this way:


on clicked theObject

if name of theObject is "button1"
    do this
end if

if name of theObject is "button2"
    do that
end if

end clicked

It’s not exactly what I wanted to do because each button is located INSIDE the tabView, but it does its job, and it makes everything SO MUCH EASIER !

Thanks so much for your help (yet again!) StefanK :wink:

now I got it, you want to do actions depending on the change of the tab view item, not on a button,
then everything must be in the selected handler, not in the on clicked handler

on selected tab view item theObject tab view item tabViewItem
	set currentTabLabel to (get label of tabViewItem)
	if currentTabLabel is "firstItem" then
		-- do something
	else if currentTabLabel is "secondItem" then
		-- do something else
	else if currentTabLabel is "thirdItem" then
	.	
		
	end if

end selected tab view item