Automatically rename objects.

Deploying to Mac OS? Ask Mac OS specific questions here.

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Fermin
Posts: 142
Joined: Fri Jun 05, 2015 10:44 pm

Automatically rename objects.

Post by Fermin » Sat Jan 30, 2021 10:17 pm

I want to rename objects (32 images) based on their ID but...

I have created an image object and copied it 31 times. I check that its ID numbers are correlative (from 3383 to 3414).
Ok, so based on those IDs I want to rename the objects but it doesn't seem to work right.
Curious: I get it if I run the code several times because each time it renames one more object.

Where am I going wrong?
Thanks for help.

The code:

Code: Select all

on mouseUp
   put 3382 into idbase /* the first object has the id 3383*/
   repeat with a = 1 to 32
      put idbase + a into identi
      put "ima" & a into nombre 
      set the name of control id identi to nombre
   end repeat
end mouseUp

bogs
Posts: 5435
Joined: Sat Feb 25, 2017 10:45 pm

Re: Automatically rename objects.

Post by bogs » Sat Jan 30, 2021 10:50 pm

set the name of control id identi to nombre
The name of a control is actually made up of the object type, followed by the name you gave it (or that it has assigned to it).

So, if you add an image object to the card, open the message box, and type

Code: Select all

put the name of image 1

you will see in the bottom field of the message box
image "Image"
What you need to change is
set the name of control id identi to nombre

to

set the short name of control id identi to nombre

or another way would be

set item 2 of the name of control id identi to nombre

*Disclaimer - I didn't actually check the code, but that is what I see wrong in the example you gave immediately jumping out at me.
Image

Fermin
Posts: 142
Joined: Fri Jun 05, 2015 10:44 pm

Re: Automatically rename objects.

Post by Fermin » Sat Jan 30, 2021 11:02 pm

Thank you, bogs, but

set the short name of control id identi to nombre

doesn't work. It gives error:

button "renom1": execution error at line n/a (Object: can't set this property)

bogs
Posts: 5435
Joined: Sat Feb 25, 2017 10:45 pm

Re: Automatically rename objects.

Post by bogs » Sat Jan 30, 2021 11:16 pm

Which OS and version of Lc are you using?

I also noticed you don't have quotes around the final name, for example, try changing -

Code: Select all

set the name of control id identi to nombre
to

Code: Select all

set the name of control id identi to quote & nombre & quote
Image

Fermin
Posts: 142
Joined: Fri Jun 05, 2015 10:44 pm

Re: Automatically rename objects.

Post by Fermin » Sat Jan 30, 2021 11:27 pm

Mac OS 10.12.6
LC 9.0.0
I have another computer with Mojave... I've tried it on Mojave (OS 10.14.6) but it doesn't work either.
Have you tested if it works for you on any OS?

bogs
Posts: 5435
Joined: Sat Feb 25, 2017 10:45 pm

Re: Automatically rename objects.

Post by bogs » Sat Jan 30, 2021 11:45 pm

I actually did test it while waiting to see what you are running.

The code works as it sets in your example, with only one thing wrong.

I limited the images to 5 images for the test, then stepped through your code example in the script editor (set up the same thing on something other than big sur, if that is what 10.12 is).

The one problem is that your images start with (in this test, 1004), but you start the repeat loop adding 1 to the id, so your code actually starts renaming image 1005 to ima1.
I'll have to run it a few times to see where the issue is on that part, though.
Image

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

Re: Automatically rename objects.

Post by SparkOut » Sun Jan 31, 2021 12:01 am

It must be something to do with losing track of the reference to the object id number.
Hard coding values can be pretty awkward, you might need to verify with something like a check

Code: Select all

if there is an image id ...
I would tackle it more by identifying the images by reference to image rather than control, and loop through the images on the card

Code: Select all

repeat with i = 1 to the number of images of this card
      put the id of image i into tId
      set the name of image i of this card to ("ima" & i)
end repeat
You can do it all sorts of ways, but this way does automatic tracking of the images on the card without having to manage whether there is an erroneous id number or some other control inserted in the list you have.

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9567
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: Automatically rename objects.

Post by dunbarx » Sun Jan 31, 2021 12:35 am

I never use ID's as references. They are assigned mysteriously, cannot be reused or reassigned once in play, and are monotonous in their structure.

Far better to use the name. These can be ordered, outfitted with mnemonics, and parsed into "groups". My advice: don't even think about using ID's. Change your current project; never look back.

So there.

Craig

Davidv
Posts: 77
Joined: Sun Apr 09, 2006 1:51 am
Location: Australia

Re: Automatically rename objects.

Post by Davidv » Sun Jan 31, 2021 12:48 am

bogs wrote:
Sat Jan 30, 2021 11:45 pm
... (set up the same thing on something other than big sur, if that is what 10.12 is).
fyi, 10.12 is Sierra, from 2016, after which there were 10.13/14/15. Big Sur changes the major part, to be 11.x

You were dying to know that... 8)

Fermin
Posts: 142
Joined: Fri Jun 05, 2015 10:44 pm

Re: Automatically rename objects.

Post by Fermin » Sun Jan 31, 2021 1:25 am

Thanks, Davidv but, as I said in a previous message, I have also tested it on another computer with Mojave (OS 10.14.6) and the same thing happens.

Fermin
Posts: 142
Joined: Fri Jun 05, 2015 10:44 pm

Re: Automatically rename objects.

Post by Fermin » Sun Jan 31, 2021 1:30 am

The routine works perfectly with other properties, e.g.

Code: Select all

on mouseUp
   put 3382 into idbase /* the first object has the id 3383*/
   repeat with a = 1 to 32
      put idbase + a into identi
      put "image" & a into nombre 
      -------
      set the loc of control id control identi to 500+a*5 ,500+ a*5
      set the borderwidth of control id identi to 3
      set the bordercolor of control id identi to random (200)
      -- set the name of control id control identi to nombre      
   end repeat
end mouseUp
but as soon as I try to assign a name,

Code: Select all

 set the name of control id identi to nombre    


something funny happens: the first time I run the code, it only acts on the first image, as if the loop only had one turn, and then, every time I activate it again, it acts on the next image...
Anyway, never mind, I'll look for another method to rename them.
Thank you very much for your help.

Davidv
Posts: 77
Joined: Sun Apr 09, 2006 1:51 am
Location: Australia

Re: Automatically rename objects.

Post by Davidv » Sun Jan 31, 2021 7:58 am

Fermin, my earlier, slightly frivolous, comment was addressed to bogs.

Regarding your code, having set the base ID to one below the first object (as noted by others above) and removed the extraneous second "control" in your [commented out] crucial line, I ran your code without difficulty on 9.6.1 under MacOS 11.1, naming four objects where the latter three were copied from the first, so consecutively numbered for ID. Creating them that way means also they will also have consecutive layer numbers. You can rename them from that with equal facility. I never use ID (see Craig / dunbarx' comment) and see no obvious reason to do so here.

This code was applied in a fresh stack with four new objects:

Code: Select all

on mouseUp
   put zero into idbase
   repeat with i = 1 to 4
      put idbase + i into identi
      put "image" && i into nombre 
      -- other loc stuff
      set the name of control identi to nombre      
   end repeat
end mouseUp

bogs
Posts: 5435
Joined: Sat Feb 25, 2017 10:45 pm

Re: Automatically rename objects.

Post by bogs » Sun Jan 31, 2021 10:00 am

Davidv wrote:
Sun Jan 31, 2021 12:48 am
bogs wrote:
Sat Jan 30, 2021 11:45 pm
... (set up the same thing on something other than big sur, if that is what 10.12 is).
fyi, 10.12 is Sierra, from 2016, after which there were 10.13/14/15. Big Sur changes the major part, to be 11.x

You were dying to know that... 8)
True that. The only set of numbers I'm really familiar with for OSX are 10.6 (snow leopard), which I actually had to learn hee hee.

As I was testing Fermin's routine to see where it was tripping up (if it was), I suddenly thought the most problems reported for the last little while were on Big Surly, so I was tired, so what :twisted:

I woke up this morning and realized why that loop was starting with the 2nd object, it was because I should have put 1003 as the first number, so that adding 1 to it would have brought me to the first ID Image

But as SparkOut says, I rarely if ever hardcode a value into anything, I guess I must be out of practice :oops:
Image

OttoM
Posts: 1
Joined: Sun Sep 24, 2023 10:37 am

Re: Automatically rename objects.

Post by OttoM » Sun Sep 24, 2023 10:54 am

Hi,
to automatically rename all images of a group, simply code this:

repeat with i = 1 to the number of images of group "mygroup"
put short name of image i of group "mygroup" into tempvariable
replace "myoldname" with "mynewname" in tempvariable
set name of image i of group "mygroup" to tempvariable
end repeat

Similar code works fine for all sorts of controls, too.

Best regards,
Otto from Austria

stam
Posts: 2599
Joined: Sun Jun 04, 2006 9:39 pm
Location: London, UK

Re: Automatically rename objects.

Post by stam » Sun Sep 24, 2023 1:08 pm

Hi Otto,

Just curious why you go through a replace and not set the name directly? i.e:

Code: Select all

repeat with i = 1 to the number of images of group "mygroup"
   set name of image i of group "mygroup" to “myNewNamw”
end repeat
I’ve not specifically tested but this should work, no?

Stam

PS: always use [ code] tags when posting so it’s easier for others to read… (the 5th button counting right from the “bold” button when editing posts.

Post Reply

Return to “Mac OS”