GP SoftwareTwitter
Opus FAQsManualCommandsObjects

Scripts (JS & VBS) Snippet: Enumerating files and metadata tags


#21

Awesome, thank you!


#22

This one also looks for "test" (possibly followed or preceded by any other characters like "dotest" or "testing") followed by exactly 10 digits:

[code]Option Explicit
Function OnClick(ByRef clickData)
Dim countFiles, countFoundTags, countTags, file, files, reg, strReg, tag
Set files = clickData.func.sourcetab.selected_files
Set reg = new RegExp
reg.Pattern = "^(.test\D\d{10})$"
reg.IgnoreCase = True

For Each file in files
	countFiles = countFiles  +1
	countTags = 0
	countFoundTags = 0
	DOpus.Output "File " & countFiles & ": " & file.name

	For Each tag In file.metadata.tags
		countTags = countTags + 1
		If InStr(Lcase(tag),"test") <> 0 Then
			countFoundTags = countFoundTags + 1
			DOpus.Output tag
		End If

		If (reg.Test(tag)) Then
			strReg = strReg & countTags & " and tag "
		End If
	Next

	If Not strReg = "" Then
		strReg = StrReverse(Replace(StrReverse(strReg)," gat dna ","",1,1))
		DOpus.Output "File " & countFiles & " contains ""test and 10 digits"" in tag " & strReg &"."
	End If        

    	DOpus.Output "All Tags: " & countTags & ", Tags with searchstring: " & countFoundTags   & vbCrLf & "--------"
	strReg = ""
Next

End Function[/code]


#23

I used a combination of all the input here and got something that seems to be working well (goo.gl/Nwc3jq).

Thanks!

BTW, here is the final version so far:

[code]Option Explicit
Function OnClick(ByRef clickData)

Dim file, files, timeStamp, countFiles, countUpdate, countSkip, countReview, regex0, liveStatus
Set files = clickData.func.sourcetab.selected_files 
timeStamp = DOpus.Create.Date(now()).Format("D#yyyyMMddT#HHmmss")
countFiles = 0
countUpdate = 0
countSkip = 0 
countReview = 0

Set regex0 = new RegExp
regex0.Pattern = "^(Name\()(.*)(\)\,\s)(Created\()(\d*)(\)\,\s)(Modified\()(\d*).*(\)\,\s)(Tagged\()(\d*)\)"
regex0.IgnoreCase = True
	
If (InStr(clickData.func.qualifiers, "shift") > 0) Then
	liveStatus = " - SHIFT held during operation: All Changes are LIVE"
End If

DOpus.OutputString "Number; File Name; Old Tag; New Tag; File Type; Status; Reason"

For Each file in files
	Dim fileName, fileType, createdDate, modifiedDate, imgTaken, imgDigitized, tag, newTag, tagString, countAllTags, countSearchTags
	fileName = file.name
	fileType = file.metadata
	createdDate = file.create.Format("D#yyyyMMddT#HHmmss")
	modifiedDate = file.modify.Format("D#yyyyMMddT#HHmmss")
	imgTaken = Empty
	imgDigitized = Empty
	tagString = Empty
	newtag = Empty
	countFiles = countFiles + 1
	countAllTags = 0
	countSearchTags = 0


 	If file.metadata <> "none" Then
		For Each tag In file.metadata.tags
			countAllTags = countAllTags + 1
			If tagString = Empty Then
				tagString = tag
			Else
				tagString = tagString & " | " & tag
			End If
			If (regex0.Test(tag)) Then
				countSearchTags = countSearchTags + 1
			End If
			'If InStr(LCase(tag),"name(") <> 0 Then
			'	countSearchTags = countSearchTags + 1
			'End If
		Next
	End If

	If (tagString = Empty) Then
		If file.metadata = "image" Then
			imgTaken = file.metadata.image.datetaken
			If (Not IsEmpty(imgTaken)) Then
				imgTaken = DOpus.Create.Date(imgTaken).Format("D#yyyyMMddT#HHmmss")
			End If
			imgDigitized = file.metadata.image.datedigitized
			If (Not IsEmpty(imgDigitized)) Then
				imgDigitized = DOpus.Create.Date(imgDigitized).Format("D#yyyyMMddT#HHmmss")
			End If
		 	newTag = "Name(" & fileName & "), Created(" & createdDate & "), Modified(" & modifiedDate & "), Taken(" & imgTaken & "), Digitized(" & imgDigitized & "), Tagged(" & timeStamp & ")"	
			'Image, NO Tags, Update with newTag
			DOpus.OutputString countFiles & "; " & fileName & "; " & tagString & "; " & newTag & "; " & fileType & "; Update; 1"
			countUpdate = countUpdate + 1
			If (InStr(clickData.func.qualifiers, "shift") > 0) Then
				clickData.func.command.ClearFiles
				clickData.func.command.AddFile file
				clickData.func.command.RunCommand "SetAttr META ""tags: " & newTag & """"
			End If
		Else
			newTag = "Name(" & fileName & "), Created(" & createdDate & "), Modified(" & modifiedDate & "), Tagged(" & timeStamp & ")"
			'Other, NO Tags, Update with newTag 
			DOpus.OutputString countFiles & "; " & fileName & "; " & tagString & "; " & newTag & "; " & fileType & "; Update; 2"
			countUpdate = countUpdate + 1
			If (InStr(clickData.func.qualifiers, "shift") > 0) Then
				clickData.func.command.ClearFiles
				clickData.func.command.AddFile file
				clickData.func.command.RunCommand "SetAttr META ""tags: " & newTag & """"
			End If
		End If	
	Elseif (countAllTags = 1 AND countSearchTags = 1) Then
		'Skip, Tag has already been updated with expected Tag string
		DOpus.OutputString countFiles & "; " & fileName & "; " & tagString & "; " & newTag & "; " & fileType & "; Skip; 3"
		countSkip = countSkip + 1
	Else
		'Review, Contains multiple Tags
		DOpus.OutputString countFiles & "; " & fileName & "; " & tagString & "; " & newTag & "; " & fileType & "; Review; 4"
		countReview = countReview + 1
	End If

Next

DOpus.OutputString "-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------"
DOpus.OutputString ""
DOpus.OutputString "Total Files: (" & countFiles & ") - Update: (" & countUpdate & "), Skip: (" & countSkip & "), Review: (" & countReview & ")" & liveStatus

End Function
[/code]


#24

I've updated the root post of this thread to include an example of reading and writing image metadata, which is probably more common than needing to work with the free-form "tags" field. Similar techniques work with MP3 metadata like Album Artist etc.