LiveCode Can't Count Images

Anything beyond the basics in using the LiveCode language. Share your handlers, functions and magic here.

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
RCozens
Posts: 138
Joined: Thu Aug 05, 2021 6:42 am
Location: Manchester, CA USA

LiveCode Can't Count Images

Post by RCozens » Thu Apr 20, 2023 5:59 am

Evening All,

The attached stack shows that "the number of images of this stack" will return different values depending on which card is open.

I just changed Serendipity Editors' Image Lister Pallet's logic to create an image list on a card-by-card basis. In one stack the list grew from 2 images to 61.

Cheers!
Rob
Attachments
Image Counter.zip
(2.28 KiB) Downloaded 72 times
Rob Cozens dba Serendipity Software Company
Manchester, CA USA

Each new generation gives more attention to the man-made world...
and less attention to the world that made man.

LCMark
Livecode Staff Member
Livecode Staff Member
Posts: 1212
Joined: Thu Apr 11, 2013 11:27 am

Re: LiveCode Can't Count Images

Post by LCMark » Thu Apr 20, 2023 6:33 am

@RCozens: LiveCode can count images just fine and is also quite adept at filling in missing clauses in incomplete chunk expressions :)

Expressions involving any control type chunk are only really 'complete' if they contain a card and a stack reference *or* a background and a stack reference.

Typically in code which manipulates controls, you'll use short forms of chunk expressions which rely on current context - e.g.

Code: Select all

   repeat with i = 1 to the number of images
      answer the name of image i
   end repeat
We're use to such 'short form' expressions being card relative (i.e. these map to 'number of images on this card of this stack' and 'image i of this card of this stack'). (The reason being that controls are 'owned' by cards - and backgrounds - and not by stacks - even if that is where they are stored).

This means that to keep things consistent, when a chunk expression misses out a card or background reference but does provide a stack reference, the engine fills in 'this card'. i.e.

Code: Select all

  the number of images of this stack
Is actually interpreted as:

Code: Select all

  the number of images of this card of this stack
This just reinforces the fact that you access controls (using number and name references) by card and *not* directly from the stack.

Of course, as with all things in life there is an exception to this rule! The 'id' based chunks do not auto-complete with a card, just with a stack:

Code: Select all

    control id 100
means the same as:

Code: Select all

   control id 100 of this stack
The reason is that ids are unique within a stack - so there is no ambiguity as to which control might be meant unlike the 'control <number>' or 'control <name>' forms. For this kind of reference, any further context (e.g. card, or group) which is specified acts as a further condition on whether the lookup is successful. (The engine can tell immediately whether there is, say, a `control id 100` in a stack - and it then checks what owners are required - whether they be cards or groups).

RCozens
Posts: 138
Joined: Thu Aug 05, 2021 6:42 am
Location: Manchester, CA USA

Re: LiveCode Can't Count Images

Post by RCozens » Thu Apr 20, 2023 5:59 pm

[/quote]
LCMark wrote:
Thu Apr 20, 2023 6:33 am
LiveCode can count images just fine and is also quite adept at filling in missing clauses in incomplete chunk expressions :)

Expressions involving any control type chunk are only really 'complete' if they contain a card and a stack reference *or* a background and a stack reference.
Hi,

Did you run my sample stack? One gets the same result when typing 'the number of images of stack "Image Counter"'.

When I run this code on the stack I mentioned, it returns 2 images (the images on card 1):

Code: Select all

on mouseUp  -- 10 May 22:RCC
   answer file "Select the stack you wish to scan:"
   if the result is "Cancel" then exit mouseUp
   put it into stackName
   if there is no stack stackName then
      beep
      answer warning stackName&&"is not a LiveCode stack!"
      exit mouseUp
   end if
   put empty into field "Image List"
   put empty into theList
   repeat with x = 1 to the number of images of stack stackName
      get the id of image x of stack stackName
      put it into theId
      get the filename of image x of stack stackName
      if it is empty then get character 2 to -2 of word 2 of the name  of image x of stack stackName
      put theId&&it&return after theList
   end repeat
   set the title of this stack to "Image Lister:"&&stackName
   sort lines of theList numeric by word 1 of each
   put theList into field "Image List"
   close stack stackName
end mouseUp
When I run this code it returns 61 images:

Code: Select all

on mouseUp  -- 19 Apr 23:RCC
   answer file "Select the stack you wish to scan:"
   if the result is "Cancel" then exit mouseUp
   put it into stackName
   if there is no stack stackName then
      beep
      answer warning stackName&&"is not a LiveCode stack!"
      exit mouseUp
   end if
   put empty into field "Image List"
   put empty into theList
   repeat with x = 1 to the number of cards of stack stackName
      put the number of images of card x of stack stackName into ImageCount
      if ImageCount = 0 then next repeat
      repeat with y = 1 to imageCount
         get the id of image y of card x of stack stackName
         put it into theId
         get the filename of image y of card x of stack stackName
         if it is empty then get character 2 to -2 of word 2 of the name of image y of card x of stack stackName
         put theId&&it&return after theList
      end repeat
   end repeat
   set the title of this stack to "Image Lister:"&&stackName
   sort lines of theList numeric by word 1 of each
   put theList into field "Image List"
   close stack stackName
end mouseUp
Rob
Rob Cozens dba Serendipity Software Company
Manchester, CA USA

Each new generation gives more attention to the man-made world...
and less attention to the world that made man.

LCMark
Livecode Staff Member
Livecode Staff Member
Posts: 1212
Joined: Thu Apr 11, 2013 11:27 am

Re: LiveCode Can't Count Images

Post by LCMark » Thu Apr 20, 2023 6:19 pm

Did you run my sample stack? One gets the same result when typing 'the number of images of stack "Image Counter"'.
Yes I did...

The `the number of images of this stack` is interpreted as `the number of images of this card of this stack`.

Counting controls is only ever relative to some card - if one isn't specified then the engine substitutes 'this card' - i.e. the card currently displayed in the stack.

So I'd expect your first handler above to only ever operate on the card currently displayed (hence why you get 2 - when the stack is on card 1); and I'd expect your second handler above to get all images on all cards, as it loops through them and explicitly counts the images on each card.

RCozens
Posts: 138
Joined: Thu Aug 05, 2021 6:42 am
Location: Manchester, CA USA

Re: LiveCode Can't Count Images

Post by RCozens » Thu Apr 20, 2023 8:10 pm

LCMark wrote:
Thu Apr 20, 2023 6:19 pm
The `the number of images of this stack` is interpreted as `the number of images of this card of this stack`.
My mistake! I interpreted the Dictionary's syntaxes for "the number of" as simply examples of how the function is used. I don't think it is very clear that different container references are restricted to different objects|parts|controls. And I think it is counterintuitive that a stack reference is restricted to the current card for some objects|parts|controls but not others. If I want the number of objects|parts|controls for a card, I can simply ask for it.

Anyway, thanks for reducing my ignorance.

Cheers!

Rob
Rob Cozens dba Serendipity Software Company
Manchester, CA USA

Each new generation gives more attention to the man-made world...
and less attention to the world that made man.

RCozens
Posts: 138
Joined: Thu Aug 05, 2021 6:42 am
Location: Manchester, CA USA

Re: LiveCode Can't Count Images

Post by RCozens » Fri Apr 21, 2023 5:20 pm

Morning All,

Is it just me, or does anyone else find it ludicrous and misleading for the number function to return the number of images|fields|buttons|groups|controls of a CARD when asked for the number of images|fields|buttons|groups|controls of a STACK?

If I had wanted the number of images|fields|buttons|groups|controls of a card, I would have asked for it.

Wouldn't it make more sense for the Script Editor to flag a request for the number of images|fields|buttons|groups|controls of a stack as a syntax error and refuse to compile it?

Cheers!

Rob
Rob Cozens dba Serendipity Software Company
Manchester, CA USA

Each new generation gives more attention to the man-made world...
and less attention to the world that made man.

SparkOut
Posts: 2857
Joined: Sun Sep 23, 2007 4:58 pm

Re: LiveCode Can't Count Images

Post by SparkOut » Fri Apr 21, 2023 8:15 pm

Hi Rob
I don't personally think it is misleading, but then in my LiveCode learning stage (Runtime Revolution as it was then) I was immersed very early on in the paradigm of "granular referencing" which the engine would always do its best to fill in that which was not explicitly stated.

In other words, everything is implicitly identified as being relative to its local environment -> group > card > stack (with extended reference for substacks or nested groups. If you want to refer to a control outside the "current" environment you have to explicitly state where it is, while the engine will continue to fill in gaps for you in the vein of "nearest/simplest locality". (Field "abc" of card "xyz" implies of stack "the same stack as the one I'm currently in")

Each control has a long name which identifies it explicity "of group > of card > of stack" and if you think about it like that, you'll see that leaving out any part of that path will let the engine "autocomplete" it for you behind the scenes in the context of "this" (group/card/stack).

Simplistically, a stack doesn't contain controls, it contains cards. Cards contain groups and/or controls.

Post Reply

Return to “Talking LiveCode”