Author Topic: Help with converting a category-assignment script, please.  (Read 4020 times)

DavidOfMA

  • Sr. Member
  • **
  • Posts: 253
  • Writer/Photographer/Therapist
Help with converting a category-assignment script, please.
« on: July 13, 2014, 04:16:43 AM »
I'm attempting to convert a script that parses the filenames, extension, and EXIF of image files and assigns categories to them. For now, I want to be able to do in IMatch 5 what I can easily do in IMatch 3 with a minimal learning curve. I'm attempting to learn only enough scripting to convert a few scripts because learning the whole scripting model (and the other capabilities of IMatch 5) is beyond my current available time.

Question one: How do I add a file to a category?
To add a file in the category "New_Photos," which contains newly imported files, to a camera type category, I'm stepping through a loop that searches both the filename and the EXIF for the model name using code that looks like this. It bombs on the category assignment line DMC_G1.AssignFiles i
Code: [Select]
Dim i As File
Dim iset As Files
Dim DMC_G1 As Category
Set DMC_G1=Database.Categories("Recording Device.Panasonic.DMC-G1")
Set db = Application.Database
Set c = db.Categories("New_Photos")
Set iset = c.Files

For Each i In iset
If (InStr (i.Name, "DMC-G1") Or GetModel (db, i) = "DMC-G1") Then
DMC_G1.AssignFiles i
ElseIf (and so on)
next i

Thanks. As far as I can tell from the help and a sample script, this should work, but clearly I'm doing something very basic wrong.

David

Mario

  • IMatch Developer
  • Administrator
  • *****
  • Posts: 29760
Re: Help with converting a category-assignment script, please.
« Reply #1 on: July 13, 2014, 09:19:17 AM »


This does not look as it would be working, because IMatch 5 uses | to separate categories, not .

Set DMC_G1=Database.Categories("Recording Device.Panasonic.DMC-G1")

Did you check that DMC_G1 is not nothing? If the category is not found, the variable will be nothing.

DavidOfMA

  • Sr. Member
  • **
  • Posts: 253
  • Writer/Photographer/Therapist
Re: Help with converting a category-assignment script, please.
« Reply #2 on: July 13, 2014, 03:32:53 PM »
Thanks. I didn't realize the category separator had changed in IMatch 5. Perhaps that could be added to the Categories section of the "IMatch 3 to 5 Scripting Migration Guide."

I substituted the "|" character for the "." character in category separators. When I use a direct assignment such as
Code: [Select]
Database.Categories("Recording Device|Panasonic|DMC-G1").AssignFiles iI don't get a syntax error. However, when I assign the above to a Category variable, I get a Type Mismatch error. Here is how I am defining and assigning the Category variable. Should I be doing this differently? For instance, should I no longer be using Category as the type? This is how it worked in IMatch 3, but apparently IMatch 5 wants me to do it differently.
Code: [Select]
Dim DMC_G1 As Category
Set DMC_G1=Database.Categories("Recording Device|Panasonic|DMC-G1")

Note that the variable name is "DMC_G1" and the actual category, which contains about 8,000 files, is DMC-G1.

This is the line that gives the error:
Code: [Select]
DMC_G1.AssignFiles i
Another question: I notice that the BeginTransaction and EndTransaction statements no longer work. Is there an IMatch 5 replacement? If so, perhaps that can also be added to the script migration guide. Suggestion: A full table of what has changed, what has been retired, and what replaces what was retired would make script conversion a (relative) breeze for people like me, who don't intend to go on to make a lot of new scripts.

Thanks,
David

Mario

  • IMatch Developer
  • Administrator
  • *****
  • Posts: 29760
Re: Help with converting a category-assignment script, please.
« Reply #3 on: July 13, 2014, 05:00:12 PM »
AssignFiles expects a Files instance. Your i is of type File (one file) not Files (many files).

Database.Categories("Recording Device|Panasonic|DMC-G1").AssignFile i

will work.

To work with database transactions, use the DatabaseTransaction class. If you have feedback for the help, or you volunteer to write some sections for the Migration Guide, please use the official way for help feedback: Click on the feedback link available at the bottom of each help topic.

DavidOfMA

  • Sr. Member
  • **
  • Posts: 253
  • Writer/Photographer/Therapist
Re: Help with converting a category-assignment script, please.
« Reply #4 on: July 13, 2014, 06:00:39 PM »
Thanks. After I posted the above suggestions for the script migration guide, I noticed the link at the bottom of the topic and incorporated them into an email to you via the link.

The script seems to be running in a basic way. I wasn't sure how best to rewrite the GetModel function, which previously used ParseVariables. Here's what I have, based on the VarContext script snippet in the Help. Is there a better way to do this? For instance, is it more efficient to DIM the "context" variable outside of the function?

Code: [Select]
'
' Get the model name from EXIF, if possible
'
Function GetModel (img As File) As String
Dim context As New VarContext
        context.File = img
GetModel = context.Parse("{Image.EXIF.Model}")
End Function

Thanks again,
David

Mario

  • IMatch Developer
  • Administrator
  • *****
  • Posts: 29760
Re: Help with converting a category-assignment script, please.
« Reply #5 on: July 13, 2014, 06:57:32 PM »
If you call the function often, it is better to dim the variable only once. Will save a few CPU cycles, but not much.

If performance is your concern, use the Metadata classes directly, instead of ParseVariables. This may be faster, because it avoids a number of extra layers. But ParseVariables is quite sufficient nevertheless.

If you do many updates, use a transaction.

DavidOfMA

  • Sr. Member
  • **
  • Posts: 253
  • Writer/Photographer/Therapist
Re: Help with converting a category-assignment script, please.
« Reply #6 on: July 13, 2014, 07:14:25 PM »
Thanks. I'll look into using Metadata (and define the context variable only once). I used Transactions in the IMatch 3 version of the script and planned to use them here, along with a Wait dialog, as soon as I figure out how to do those things. Meanwhile, I need to get the rest of my import script and other commonly used scripts at least basically working so I can retire IMatch 3.

David

Mario

  • IMatch Developer
  • Administrator
  • *****
  • Posts: 29760
Re: Help with converting a category-assignment script, please.
« Reply #7 on: July 13, 2014, 07:52:56 PM »
There are sample scripts which use transactions, and the aptly named Progress Dialog  ;) sample script shows how to use the progress dialog.

DavidOfMA

  • Sr. Member
  • **
  • Posts: 253
  • Writer/Photographer/Therapist
Re: Help with converting a category-assignment script, please.
« Reply #8 on: July 13, 2014, 09:44:24 PM »
Thanks. Saw that script and it looked straightforward. I assume the transaction stuff is, too. It's the stuff that has just changed a little that seems to be hanging me up do far. I'm sure it will come together eventually.