(and I hope enough other people will gain enough knowledge from what I am turning into
a series of exceedingly long posts to justify their length and complexity)
On Jul 16, 2016, at 4:13 PM, Stockly, Ed <email@hidden> wrote:
--Question 1: why do I need as item on theWindow but not on theTab?
Don't know. (Sometimes the magic works, sometimes the magic doesn't work.)
-->Now, I want to break this up into handlers. I was pretty careful, and tried it a few different ways, but I can't get this to work. The problem occurs at the line marked ERROR.
This is not a valid repeat command:
-- -- repeat with theTab in the tabs of theWindow as item
-->Question 2: What can I do to make it work.
-->Question 3: Is there a way to write this so i don't have to put as item everywhere?
This doesn't work either. Or at least not when I try to call another handler with theTab
as its argument. Unclear to me whether theTab should be passed as is to the handler
and let the handler dereference it with "as item" or dereference it first and pass the
dereferenced tab, but I can't get either approach to work.
-- repeat with theTab in the tabs of theWindow
-- set theTab to theTab as item
This doesn't work.
Also you may try this:
set allWindows to the windows
repeat with theWindow in allWindows
Not the problem:
repeat with theWindow in the windows
handler(theWindow)
end
works fine.
Then, in the handler, I am referencing theWindow argument with "as item"
as you suggested, and that works. What I am unable to do is take this
a level further and pass each tab to another handler.
I am very frustrated. This code conforms to a pattern seen frequently throughout
programs in all languages:
Do actions 1
for each element
do actions 2
for each subelement
do actions 3
I have no problem implementing this without handlers:
tell application "Safari"
activate
log the (current date) as string
repeat with theWindow in the windows
if name of theWindow as item is not "" then
log the name of theWindow as item
repeat with theTab in the tabs of theWindow
log " " & the name of theTab as item
end repeat
end if
end repeat
end tell
The problems occur when I want to program the pattern more appropriately
following this pseudo-code
on run
do actions 1
for each element
handler1(elementt)
on handler1(element)
do actions 2
for each subelement
handler2(subelement)
on handler2(subelement)
do actions 3
This is as close as I have come. There is still an error in logWindow.
script logWindows
property parent : application "Safari"
on run
activate
repeat with theWindow in the windows
my logWindow(theWindow)
end repeat
end run
on logWindow(theWindow)
log the name of theWindow as item
-- Safari got an error: Can’t make |tabs| of item 1 of every window into type reference.
set allTabs to the tabs of theWindow
repeat with theTab in allTabs
my logTab(theTab)
end repeat
end logWindow
on logTab(theTab)
log " " & the name of theTab as item
end logTab
end script
run logWindows