Here is a modified proposal.
It rely of this scheme:
set itsValue to "All portfolios"
set menuItems to {"name1", "name2", "name3", "All portfolios", "name5", "name6"}
set delim to character id 128127 & character id 128520
set asText to my recolle(menuItems, delim)
log asText -->(*name1????????name2????????name3????????All portfolios????????name5????????name6*)
set theEnd to item 2 of my decoupe(asText, itsValue & delim)
log theEnd --> (*name5????????name6*)
set wanted to item 1 of my decoupe(theEnd, delim)
log wanted --> (*name5*)
#=====
on decoupe(t, d)
local oTIDs, l
set {oTIDs, AppleScript's text item delimiters} to {AppleScript's text item delimiters, d}
set l to text items of t
set AppleScript's text item delimiters to oTIDs
return l
end decoupe
#=====
on recolle(l, d)
local oTIDs, t
set {oTIDs, AppleScript's text item delimiters} to {AppleScript's text item delimiters, d}
set t to l as text
set AppleScript's text item delimiters to oTIDs
return t
end recolle
#=====
Embedded in the script it give:
tell application "System Events"
tell process "Safari"
set frontmost to true
name of windows --> { "MacScripter / Post a reply", "Portfolio", …}
tell window "Portfolio"
-- class of UI elements --> {splitter group, button, button, button, group, button, toolbar}
tell splitter group 1
-- class of UI elements --> {splitter, tab}
tell tab group 1
-- class of UI elements --> {group}
tell group 1
-- class of UI elements --> {group}
tell group 1
-- class of UI elements --> {scroll area}
tell scroll area 1
-- class of UI elements --> {UI element, scroll bar}
tell UI element 1
-- class of UI elements
--> {group, group, group, group, group, group, group, group, group, table, group, group, group, group, group, group, group, group, group, group, group, group, group, group, group, group, scroll area, scroll bar, group, group, group}
tell group 8
-- class of UI elements --> {static text, pop up button, button, button}
set {{xleft, yTop}, {w, h}} to {position, size} of pop up button 1 -- ADDED
tell pop up button 1
-- set itsName to its name --> missing value
set itsValue to its value --> "All portfolios"
-- try to reveal the menu item
-- Use cliclic to click upon the pop up button
tell me to do shell script "/usr/local/bin/cliclick c:" & (xleft + w div 2) & "," & (yTop + h div 2)
repeat 20 times
delay 0.1
if exists menu 1 then exit repeat # The menu is expanded
end repeat
if (not (exists menu 1)) then error "The menu 1 really refuse to appear!"
tell menu 1
-- grab the name of menu items
set menuItems to name of menu items
--> {"name1", "name2", "name3", "All portfolios", "name5", "name6"}
set delim to character id 128127 & character id 128520
set asText to my recolle(menuItems, delim)
log asText -->(*name1????????name2????????name3????????All portfolios????????name5????????name6*)
set theEnd to item 2 of my decoupe(asText, itsValue & delim)
log theEnd --> (*name5????????name6*)
set wanted to item 1 of my decoupe(theEnd, delim)
log wanted --> (*name5*)
-- supposed to click a menu item below the original one
set {{xleft, yTop}, {w, h}} to {position, size} of menu item wanted -- ADDED, get the coordinates of the original menu item
tell me to do shell script "/usr/local/bin/cliclick c:" & (xleft + w div 2) & "," & (yTop + h div 2) -- ADDED, click it
end tell
end tell
end tell
end tell
end tell
end tell
end tell
end tell
end tell
end tell
end tell
end tell
#=====
on decoupe(t, d)
local oTIDs, l
set {oTIDs, AppleScript's text item delimiters} to {AppleScript's text item delimiters, d}
set l to text items of t
set AppleScript's text item delimiters to oTIDs
return l
end decoupe
#=====
on recolle(l, d)
local oTIDs, t
set {oTIDs, AppleScript's text item delimiters} to {AppleScript's text item delimiters, d}
set t to l as text
set AppleScript's text item delimiters to oTIDs
return t
end recolle
#=====