Author Topic: Tag Data Methods and Properties  (Read 3489 times)

hluxem

  • Sr. Member
  • **
  • Posts: 312
Tag Data Methods and Properties
« on: September 25, 2016, 08:16:09 PM »
I'm reading up on the Metadata Classes in the help file to understand the different variables. To make it easy I copied the example under WriteBack and that successfully adds the values beach and summer to the keywords. Next I was trying to use the add function with the unique parameter and the replace function, but I can't make these 2 functions work correctly:

vals.Add ("summer",False)
vals.Replace ("beach","New Beach 1")
 
When I try to use the Add function with the Unique parameter, no matter what I do, if the value is already there it is not added again.
I thought reading the help that using vals.Add ("summer",False) should add the value even if it's already there. Do I understand something wrong or is my expression not correct?

The help says "This method replaces an existing value with a new value." for the Replace function. When I use the replace function like vals.Replace ("beach","New Beach 1"), the new value is added, but the original value is still there. Works like "if value is there add new value". Anything I can do to replace the value?

Below is the sample script. Depending on the function I test, I comment out the lines not needed.

Code: [Select]
'#Language "WWB-COM"

Option Explicit

Sub Main

Dim vals As TagValues
Dim v As TagValue
Dim data As TagData
Set data = Application.GetFileWindow(imfwtCurrent).FocusedFile.TagData

Set vals = data.Values("XMP::Lightroom\hierarchicalSubject\HierarchicalSubject")

'vals.Add ("beach",True)
'vals.Add ("summer",False)

vals.Replace ("beach","New Beach 1")

data.Update vals
data.WriteBack

End Sub






   

Mario

  • IMatch Developer
  • Administrator
  • *****
  • Posts: 27212
Re: Tag Data Methods and Properties
« Reply #1 on: September 25, 2016, 09:25:12 PM »
It seems you are working with repeatable tags (keywords), which means that the tag can have many values. This is different.

hluxem

  • Sr. Member
  • **
  • Posts: 312
Re: Tag Data Methods and Properties
« Reply #2 on: September 25, 2016, 10:28:03 PM »
All I want to do is replace one of the tag values with a different value, is this not possible for repeatable tags? I can print and display each value using "vals.Item (3)", so I thought there must be something to change that value as well. Not sure what else I could try. Please let me know if that is not possible.

Quote
you are working with repeatable tags (keywords), which means that the tag can have many values
I trying to learn and understand the tag values, so I read the help and used the example in the help with the same tag (Keywords) as in the help. It happened to be a repeatable tag. That being set, its simple for non repeatable tags, all you need is the set method, that adds or replaces the existing value, no other method would be needed.
The help says "Adds new value for repeatable tag." So that should be ok and it is, if I don't use the optional value. The optional value is not changing anything.
It does not mention anything in the replace method in regards to repeatable values. For non repeatable tags there is already the set method. The replace method works for repeatable tags, it is just doing something different than in the description. Should the description be modified?
Quote
This is different.
I can't find anything in the help about this, can you shortly explain what the difference is or where I can find more information? I went trough the sample scripts dealing with meta data but was not able to find anything similar.


Thanks a lot,

Heiner

sinus

  • Global Moderator
  • *****
  • Posts: 4228
  • IMatch-User since 2001 (IMatch 3.6)
Re: Tag Data Methods and Properties
« Reply #3 on: September 26, 2016, 07:35:25 AM »
All I want to do is replace one of the tag values with a different value, is this not possible for repeatable tags? I can print and display each value using "vals.Item (3)", so I thought there must be something to change that value as well. Not sure what else I could try. Please let me know if that is not possible.Thanks a lot,

Heiner

Try this one, taken from the script "Metadata".
Look at REPLACE in the code.

Code: [Select]
'#Language "WWB-COM"

Option Explicit

Sub Main

Debug.Clear

Dim selection As Files
Set selection = Application.GetFileWindow(imfwtCurrent).SelectedFiles
If selection.Count = 0 Then
MsgBox "Please select at least one file."
Exit Sub
End If

Dim f As File
Dim vals As TagValues
Dim v As TagValue
Dim data As TagData

For Each f In selection
' We *need* a local copy of the tag data
Set data = f.TagData

Set vals = data("XMP::Lightroom\hierarchicalSubject\HierarchicalSubject")
'For Each v In vals
' Debug.Print v
'Next

' Add a few keywords
'vals.Add "IMatch"
'vals.Add "Script"
'vals.Add "Location|Beach|IMatch Sunview"
 ' use REPLACE, the first word (here IMatch)must be there, it will be replaced by SUCCESS
vals.Replace "IMatch","SUCCESS"
data.Update vals

data.WriteBack
Next

End Sub


Best wishes from Switzerland! :-)
Markus

hluxem

  • Sr. Member
  • **
  • Posts: 312
Re: Tag Data Methods and Properties
« Reply #4 on: September 26, 2016, 05:07:37 PM »
Thank you so much for looking that up Sinus.

I run the script you attached. Using a file without any keywords, I first enabled adding the keywords and I did get the following result:

IMatch; Script; Location|Beach|IMatch Sunview

So far so good. 2nd I run the script with disabling adding the keywords and enabling the replace function. My result is as follows:

IMatch; Script; Location|Beach|IMatch Sunview; SUCCESS

The original value is still there. I tried this with writing back and reloading the meta data and without. The result is always the same. The way it works for me is "add value if expression exists".

Do you get different results on your computer or do I just have the wrong expectation for the replace method?

Thanks a lot,

Heiner

sinus

  • Global Moderator
  • *****
  • Posts: 4228
  • IMatch-User since 2001 (IMatch 3.6)
Re: Tag Data Methods and Properties
« Reply #5 on: September 26, 2016, 07:57:58 PM »
Hello Heiner

Thanks, please not, I am not a very good scripter, really not.

Yes, hmm, Mario is correct.
I have not seen this.

In the helpfile is replace and remove not for repeatable tags.

This means, we cannot take remove or replace for the keywords.
Add works, this is also mentioned in the helpfile.

And this is, I guess, what Mario means above, that repeatable tags are different.

If I read the helpfile correct, then you could use Set. In the help:
Sets a new value. This method is to be used for tags which can have only one value. If this method is used for a repeatable tag, all existing values are removed and the new value set by this method is added.

Maybe this helps you not, I am not sure.
At the moment I do not know, how we can replace a keyword with another by a script.

I am quite sure, for a real scripter this would be doable. But not for me very easy.

For what must you have this?
I guess, you cannot select simply some files and do this replace-stuff "by hand" with the keyword-panel?

Must you do this often?

Hmmm, if we cannot do this directly with replace or remove/add, then one possibilty could be (I am not sure) to read the keys out as a string and then replace it by string-replacings and write it back.
But, I hope, you find a solution or another scripter can give you a hint.

Sorry, that I cannot help you more and that I misleaded you with replace.  :-\






Best wishes from Switzerland! :-)
Markus

hluxem

  • Sr. Member
  • **
  • Posts: 312
Re: Tag Data Methods and Properties
« Reply #6 on: September 26, 2016, 09:56:22 PM »
Hello Sinus,

I guess we lost some of the real good scripters in the forum.
Unfortunately the method set is only for non repeatable tags, it will assign a value only to the first tag in the group and delete the others.
My issue is that there seems to be no method to assign a value to a specific tag in the group. I can read, assign and print the value, just not change it.

I may try to read out and assign all tags to a variable, clear all values, replace the value I want to be changed and then use the add method to add them all back. Not quite straight forward, but may work.

Quote
For what must you have this?  Must you do this often?

I tried to write a script for changing the annotations name on a set of pictures. https://www.photools.com/community/index.php?topic=5934.0
There are 2 problems, the tag region name is part of a complex structure, and I'm not even sure if what I want to do would work. It did work with a script when I had only one value in the tag. The 2nd problem is that I did not understand the difference between the different "variables" (most scripts use vals and v). I went back to the help and did some more reading and testing on this, unfortunately it does not quite do what I thought its supposed to be doing. I definitely have a better idea now what the different things mean in the script.

Thanks again,

Heiner

sinus

  • Global Moderator
  • *****
  • Posts: 4228
  • IMatch-User since 2001 (IMatch 3.6)
Re: Tag Data Methods and Properties
« Reply #7 on: September 27, 2016, 09:26:58 AM »
Hello Sinus,

I guess we lost some of the real good scripters in the forum.

 :-\ You are completely right. We have still some very good scripters here, but of course they have not always the time.

I may try to read out and assign all tags to a variable, clear all values, replace the value I want to be changed and then use the add method to add them all back. Not quite straight forward, but may work.
I think, this would be a good way to achieve, what you want.


I tried to write a script for changing the annotations name on a set of pictures. https://www.photools.com/community/index.php?topic=5934.0
There are 2 problems, the tag region name is part of a complex structure, and I'm not even sure if what I want to do would work. It did work with a script when I had only one value in the tag. The 2nd problem is that I did not understand the difference between the different "variables" (most scripts use vals and v). I went back to the help and did some more reading and testing on this, unfortunately it does not quite do what I thought its supposed to be doing. I definitely have a better idea now what the different things mean in the script.
Thanks again,
Heiner

I see. To be honest, I did not try out until now really annotations. Because at the moment I see no benefit for me. Maybe I must read more about this theme, to see the real advantages to use them.

Heiner, I hope, you can get what you want in the near future. If I could, I would love to help you.
Best wishes from Switzerland! :-)
Markus

Mario

  • IMatch Developer
  • Administrator
  • *****
  • Posts: 27212
Re: Tag Data Methods and Properties
« Reply #8 on: September 27, 2016, 09:32:11 AM »
The "Metadata" sample script shows how to add keywords to a file:

Code: [Select]
Set vals = data("XMP::Lightroom\hierarchicalSubject\HierarchicalSubject")
For Each v In vals
Debug.Print v
Next

' Add a few keywords

vals.Add "IMatch"
vals.Add "Script"
vals.Add "Location|Beach|IMatch Sunview"
data.Update vals

To remove, swap, exchange etc. individual values you would process the keywords in the vals, remove what you don't need anymore, add what you want. There is no "replace" function to replace individual elements in a repeatable tag. You remove the one you don't want anymore, then add the new one.

Regarding XMP region updates via scripting: Very difficult. You can easily access the region names via something like

Code: [Select]
Dim f As File
Dim vals As TagValues
Dim v As TagValue
Dim data As TagData

Set f = Application.GetFileWindow(imfwtCurrent).FocusedFile

Set vals = f.TagData("MWG::Regions\RegionsRegionListName\RegionName\0")
For Each v In vals
Debug.Print v
Next


But you have to keep in mind that a file can have any number of regions (faces) and each region has a name. etc.
The problem is that, because regions are a complex structure, ExifTool does not allow to update individual parts of it. IMatch instead processes the the tag data via an XML structure and always writes it completely via the {File.MD.MWG::Regions\Regions\RegionInfo\0} tag. This is a XML record and when IMatch later send this to ExifTool, ExifTool does its job.

I had to put a lot into all this to get it to work, and it really cannot be simplified enough to make it work in scripting. I could create and provide a special "Region" scripting class which does the work behind the curtain. But there are just not enough users out there who understand or write scripts. IMatch 5 has become so powerful that writing scripts is barely needed.

And Basic scripting will be removed from IMatch in the forseeable future anyway. I have explained that on several occasions. I will replace the historic Basic scripting with JavaScript and technologies developed for IMatch Anywhere.  Hence I put no work anymore into the current scripting system.






« Last Edit: September 27, 2016, 09:52:09 AM by Mario »

sinus

  • Global Moderator
  • *****
  • Posts: 4228
  • IMatch-User since 2001 (IMatch 3.6)
Re: Tag Data Methods and Properties
« Reply #9 on: September 27, 2016, 10:29:23 AM »
Thanks to Mario for this input.

Heiner, in the same time I tried to do something, but it is surely not good, but works somehow.

Maybe it helps you also, with the explanations of Mario. Bear really in mind, what Mario says about Basic scripts and its replacement (I personally like this replacement not, but it is like it will be, and I understand Mario, because the costs are high).

Attachement 1: before the script
Attachement 2: after the script
 
and here the rough script, maybe you can enhance it or take some parts, but it is from a bad scripter ;)

Code: [Select]
Sub Main

Dim selection As Files
Set selection = Application.GetFileWindow(imfwtCurrent).SelectedFiles
If selection.Count = 0 Then
MsgBox "Please select at least one file."
Exit Sub
End If

Debug.Clear
Dim f As File
Dim vals As TagValues
Dim v As TagValue
Dim data As TagData

For Each f In selection
' We *need* a local copy of the tag data
Set data = f.TagData
Set vals = data("XMP::Lightroom\hierarchicalSubject\HierarchicalSubject")

Dim oldval As Variant
oldval = vals(1)

For Each v In vals
oldval = vals(1)
Next
Debug.Print "oldval: " & oldval

Dim newkeys As Variant
newkeys = Replace(oldval,"Duck","Mickey")
Debug.Print "newkeys: " & newkeys

vals.Set newkeys
data.Update vals
data.WriteBack
Next

End Sub
Best wishes from Switzerland! :-)
Markus

Erik

  • Sr. Member
  • **
  • Posts: 286
Re: Tag Data Methods and Properties
« Reply #10 on: September 29, 2016, 09:55:42 PM »
I think what Sinus did there was what I would probably do.

A long while ago, when first moving to IMatch 5, I had a lot of issues with keywords and my HierarchicalSubject having both hierarchical and flat keywords...  I wrote a script at the time to do something like what I think you are trying to do or at least check for things.

In pseudo-code (I don't have the sript anymore):

I used the "for each x in values" to march through each individual keyword.

Within each keyword I used a function to check if the keyword contained a specific value (i.e. for a replacement).  This gave flexibility as I only wanted to look for specific levels of the keyword hierarchy not the whole hierarchy. 

If the result was true, I would then delete the keyword and then add the new one.  I was never able to use the actual replace function.  I did have to make sure I used the data.update command because I didn't realize I needed it, and that made all the difference in getting everything to work, especially after removing keywords.

As far as ease of use... scripting is never as easy as we can hope, and arrays (which are what our keywords end up being) always pose a challenge that can be overcome with a little creativity.

hluxem

  • Sr. Member
  • **
  • Posts: 312
Re: Tag Data Methods and Properties
« Reply #11 on: October 01, 2016, 08:56:43 PM »
Thank you all for your input.
I certainly learned more about scripting, and got the script to change the keyword values as I wanted to.

Going back to the original goal of changing face annotations, I decided that it's better to change them in the region info tag as that seems to be the source for the region name tag. It actually turned out easier to change that value. I'm able to change the names of face annotations with a script and the tests I did so far seem to work. The region info is updated and the annotations are displayed correctly. What I don't like is that the Region Name tag does not update, even that I do use the data.Update vals and data.WriteBack statements. There is no yellow pencil on the image after the script finished.
The region name tag will only update when I do another metadata change like the rating. Then the yellow pencil pops up and the tip says that there are unwritten tags. It's the rating and the region info tag, even that the tag is already displayed with the updates. After clicking the yellow pencil, the region name tag is updated as well.
Is the data.WriteBack command only writing back the tag to the database. When the script runs a yellow pencil appears on the picture and disappears after write back, so I assumed the data is written back to the file.
Is there another command I can force a second write back just like clicking on the yellow pencil?

Thanks,

Heiner

Mario

  • IMatch Developer
  • Administrator
  • *****
  • Posts: 27212
Re: Tag Data Methods and Properties
« Reply #12 on: October 02, 2016, 08:49:54 AM »
I'm not exactly sure what you are doing. Do you update the XML data in the Region in your script? This would mean that you have to use an XML parser in your script, load the data data, navigate the XML DOM and replace the region data for the tag. Then save the modified XML data back to the region tag. Doable but complicated.

As I said, IMatch only works internally with the native region XML data. When IMatch changes face data, it re-generates this region XML data. And when IMatch writes back face data, it writes it via this XML data block. All the other region tags you can access and see in the Metadata panel or via the tag scripting classes are just copies of this data, produced by ExifTool when it delivers the data to IMatch. Changing these tags has no effect during the write back.

hluxem

  • Sr. Member
  • **
  • Posts: 312
Re: Tag Data Methods and Properties
« Reply #13 on: October 03, 2016, 03:30:18 PM »
Solved it  :)

I did not have "Write back changes to metadata immediately" enabled. I want to do more testing, but it looks like I'm able to change the annotations with a script.

Quote
I'm not exactly sure what you are doing.
I basically do the following:

Set vals = data("MWG::Regions\Regions\RegionInfo\0")

When I print the tag, the xml data are displayed.
Next I assign this value to a variable and replace the Name I want to change with this statement:

Replace(variable, Old_Name, New_Name)

then I write back the new value to the tag using the vals.set statement.



Mario

  • IMatch Developer
  • Administrator
  • *****
  • Posts: 27212
Re: Tag Data Methods and Properties
« Reply #14 on: October 03, 2016, 03:37:05 PM »
This should work. because you only replace something but not need to change the structure of the XML itself.

The WriteBack method has a parameter to control immediate write-back to the physical file. You should be able to see your changes as soon as IMatch has re-imported the file. Or in the ExifTool Command Processor.

Erik

  • Sr. Member
  • **
  • Posts: 286
Re: Tag Data Methods and Properties
« Reply #15 on: October 05, 2016, 04:34:58 PM »
To reinforce what Mario stated and address the issue of writeback you describe, it's been my finding that IMatch doesn't activate the yellow pencil and metadata writeback after such a script because all the writeback would be happening within the script.  When the script is done, everything should be ok.  If it isn't, I think you need to look closer at the stuff Mario posted above to get everything written and check the ExifTool Processor for verification.

hluxem

  • Sr. Member
  • **
  • Posts: 312
Re: Tag Data Methods and Properties
« Reply #16 on: October 05, 2016, 06:24:04 PM »
Thanks Erik,

That's exactly what I did. I didn't know that a parameter can be used for the write back function (I'm a "copy and paste" scripter :>) and all the examples I saw were without the parameter. After reading the help and trying to add the parameter with and without brackets, I got it right. Just added True to the write back function.

data.WriteBack True

All my issues are gone now, the script works as intended and I set my preferences back to not write back the changes immediately.

Heiner