Re: need explanation for trivial Safari script behavior
Re: need explanation for trivial Safari script behavior
- Subject: Re: need explanation for trivial Safari script behavior
- From: Mitchell L Model <email@hidden>
- Date: Sat, 16 Jul 2016 21:35:58 -0400
Thank you. Much of this reminds me of my favorite paragraph from this mailing list
in a long time:
in Vol 13, Issue 23, 6/3/16, from has <email@hidden>
>> Basically, like 99.99% of ASers he understands the language up to a
>> point, which is enough to produce a script that usually/mostly works,
>> then beyond that point he has a lookup table of magical incantations to
>> apply iteratively to the bits that don't until they do: "1. try wrapping
>> in parentheses and see if it works now", "2. try adding a `my` prefix
>> and see if it works now", "3. try adding a `get` prefix…", and so on.
Oh, my, yes. I must spend 20% of my AppleScript debugging time rotating through
incantations like this until something works. Sometimes I stop to figure out why,
but most of the time I figure if it didn't work the way I wrote it I probably wouldn't
understand why it worked when it did.
My biggest bugaboo has always been list iteration. I mostly avoid
repeat with theThing in theList
constructions, since they never seem to work. I usually use
set theWindows to the windows -- huh? what does this do? :-)
repeat with n from 1 to length of theWindows
-- do something with
-- item n of theWindows -- I guess this dereferences the item
end
and hate it because of the extraneous assignment and amateur-looking
numerical looping.
Sometimes
repeat with theWindow in the windows
-- do something with
-- contents of theWindow -- huh? why? does this really work? can't remember
end
or even
set theWindows to the windows -- huh? what does this do? :-)
repeat with theWindow in theWindows
-- do something with -- does this really ever work? I don't know
-- theWindow
end
and yes, I throw in gets for good measure here and there. Of course that's necessary
when arguing with the compiler about where parentheses go, but sometimes it's
just magical incantation.
In this list exchange I have learned a new magical incantation:
theWindow as item
Why anything in a list would ever NOT be an item is beyond me. EVERYTHING in a list
is an item of that list. Why would "as item" do anything?
What a stupid language. But I have always loved it. I even put "the" in most of the places
where it could go. Considering the vast array of languages I have used seriously, and
how complex some of them were/are, I embarrassed to admit my love for AppleScript,
and my enjoyment of programming in Bizarro-world English.
There should be an official web page documenting the most important magical incantations
and when they should be applied. There should be about 6 or 8 of those incantations
that buy nearly all of what's available.
>>
> 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
yeah, the "as item" there was just guessing -- I was dropping in one of my many attempts to
get this repeat to work -- I never used "as item" before seeing it in this exchange.
>
> -->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?
>
> -- repeat with theTab in the tabs of theWindow
> -- set theTab to theTab as item
>
I could swear I tried this, but I'll try it again
> Also you may try this:
>
> set allWindows to the windows
> repeat with theWindow in allWindows
>
> and
>
> set allTabs to the tabs of theWindow
> repeat with theTab in allTabs
>
>
>
> -->Question 4: I have been iterating over lists in AppleScript since it was introduced. I can't figure out why this one is giving me so much trouble. Is this perhaps just flakiness in Safari's AppleScript implementation?
>
> That might account for safari behaving differently with windows and tabs, but dereferencing a list in a that kind of a repeat loop has been part of appleScript from the start.
>
> -->But, I think it's designed to work the way you want, but just doesn't. In otherwords, as long as the orgiginal item is still available, the script should be able to dereference on the fly (as it seems to do with tabs)
>
> It may be that when you've used that form of a repeat loop before, it was automatically deferencing.
>
> You can identify when its a reference and when its and item by looking at the log, and if it says {item 1 of x} or {tab one of ...} its a reference
>
Well yeah that's how I know I'm in trouble :-). The arguments with the compiler are about how to extract the real value from the reference. Sometimes it's "contents of". Sometimes it's "item n of". And now I know it is sometimes simply "as item".
And there are probably other things. Can there be a reference to a reference? If so, how would you tell from looking at a log entry that shows a reference? Are there other ways to dereference a reference?
_______________________________________________
Do not post admin requests to the list. They will be ignored.
AppleScript-Users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
Archives: http://lists.apple.com/archives/applescript-users
This email sent to email@hidden