Author Topic: Testing if an attribute exists?  (Read 1593 times)

stahl

  • Jr. Member
  • *
  • Posts: 67
  • IMatch user since 2004 (Ver. 3.4)
Testing if an attribute exists?
« on: December 29, 2015, 10:52:43 PM »
Hello,
could someone tell me how I can have a script determine if a given attribute exists in a database?
I want a script to write the file name into an attribute but make sure it exists at all.

In IM3 this did it with properties:
If ActiveDatabase.PropertySets("Image").Properties(strPropName) Is Nothing Then
...

Any hints will be appreciated!
Thanks, Stef

Ferdinand

  • 100 years since I was shot and a war was started
  • Global Moderator
  • *****
  • Posts: 1670
Re: Testing if an attribute exists?
« Reply #1 on: December 30, 2015, 12:05:15 AM »
Code: [Select]
        Const SET_NAME As String = "Image"
        Dim img As File
        Dim r As IMAttributeItem
        If img.AttributeData(SET_NAME).Count > 0 Then
               Set r = img.AttributeData(SET_NAME)(1)
        Else
            Set r = img.AttributeCreateItem(SET_NAME)
        End If

        r.GetValue("some_attribute") = some_variable

        r.WriteBack

I agree it's not easy to work these things out on your own.  It's been a long time since I did it.  This is an extract from a workflow script of mine.  IIRC there is a catch that GetValue is used both to retrieve a value and set a value.  Don't use SetValue, which I seem to recall doesn't do what its name suggests, but it has been a while, and I don't recall the details.  A search would no doubt refresh my memory, but it's late.

Mario

  • IMatch Developer
  • Administrator
  • *****
  • Posts: 30082
Re: Testing if an attribute exists?
« Reply #2 on: December 30, 2015, 08:34:09 AM »
To dynamically find out all Attributes in the AttributeSet named "Notes", use

Code: [Select]
Dim aset As IMAttributeSet
Set aset = Database.AttributeSets("Notes")

Dim a As IMAttribute
For Each a In aset.Attributes
Debug.Print a.Name
Next

The following code sets found to true if the Attribute "Note" exists in the Attribute Set "Notes".  The variable a then contains a reference of the Attribute and can be used immediately:

Code: [Select]
Dim a As IMAttribute
For Each a In Database.AttributeSets("Notes").Attributes  ' We search in Attribute Set Notes

If a.Name = "Note" Then
found = True
Exit For
End If

Next


If Not found Then
Debug.Print "Attribute does not exist."
Else
Debug.Print a.Name
End If

sinus

  • Global Moderator
  • *****
  • Posts: 4489
  • IMatch-User since 2001 (IMatch 3.6)
Re: Testing if an attribute exists?
« Reply #3 on: December 30, 2015, 11:57:59 AM »
Thanks Stef, Ferdinand and Mario
Will be also helpful for me.
Best wishes from Switzerland! :-)
Markus

Ferdinand

  • 100 years since I was shot and a war was started
  • Global Moderator
  • *****
  • Posts: 1670
Re: Testing if an attribute exists?
« Reply #4 on: December 30, 2015, 01:26:05 PM »
I guess the question is whether you're writing for the general case or your own database.  In the general case, such as a script that you'd post here in the script repository, you need to check that an attribute exists, as Mario demonstrated.  There's a lot more checking and less assumption involved.

I was writing for my own database, so I know what the attribute set is called and what each attribute is called, and all I was looking to do was check that an attribute record had already been created for each image, and if not, then create one, and finally use it to either read or write an attribute.

stahl

  • Jr. Member
  • *
  • Posts: 67
  • IMatch user since 2004 (Ver. 3.4)
Re: Testing if an attribute exists?
« Reply #5 on: December 30, 2015, 02:44:22 PM »
Thanks, Mario and Ferdinand,
that was really helpful!
Stef