Below are scripts which demonstrate how to use DOpus.FSUtil.ReadDir and the FolderEnum object, for reading directory listings, as well as the Metadata.tags collection for listing any user-defined tags set on files.
The two scripts below do the same thing: Print the metadata tags (if any) for each file in C:. (For my test, I created a "test.txt" and tagged it with "cat" and "dog".)
One script in JScript and the other in VBScript. The behaviour of the two should be identical.
Note that sometimes metadata is not available for a file at all. For example, when I run it this happens with hiberfil.sys and pagefile.sys, because Opus is unable to inspect those files (they are locked by Windows). The files could have tags, but Opus can't access them, so this is distinct from an empty list of tags.
JScript:
function PadSpaceRight(str, len)
{
str = str + ""; // Force to string.
if (str.length < len)
str = str + Array(len + 1 - str.length).join(" ");
return str;
}
var tagString;
var folderEnum = DOpus.FSUtil.ReadDir("C:\\", false);
while (!folderEnum.complete)
{
var folderItem = folderEnum.next;
if (!folderItem.is_dir)
{
if (folderItem.metadata == "none")
{
tagString = "<no metadata available>";
}
else
{
tagString = "";
for (var tagEnum = new Enumerator(folderItem.metadata.tags);
!tagEnum.atEnd();
tagEnum.moveNext() )
{
if (tagString != "")
{
tagString += ", ";
tagString += tagEnum.item();
}
else
{
tagString = tagEnum.item();
}
}
if (tagString == "")
{
tagString = "<no tags>";
}
}
DOpus.Output(PadSpaceRight(folderItem + ": ",25) + tagString);
}
}
VBScript:
Option Explicit
Function PadSpaceRight(str, slen)
str = CStr(Str) ' Force To String.
If (Len(str) < slen) Then
str = str & Space(slen - Len(str))
End If
PadSpaceRight = str
End Function
Dim folderItem
Dim folderEnum
Dim tag
Dim tagString
Set folderEnum = DOpus.FSUtil.ReadDir("C:\", False)
Do While (Not folderEnum.complete)
Set folderItem = folderEnum.Next
If (Not folderItem.is_dir) Then
If (folderItem.metadata = "none") Then
tagString = "<no metadata available>"
Else
tagString = ""
For Each tag In folderItem.metadata.tags
If (tagString <> "") Then
tagString = tagString & ", "
tagString = tagString & tag
Else
tagString = tag
End If
Next
If (tagString = "") Then
tagString = "<no tags>"
End If
End If
DOpus.Output(PadSpaceRight(folderItem + ": ",25) & tagString)
End If
Loop
Other types of metadata tags:
The post above is about the actual "tags" metadata, which is just a free-text collection of strings you can associate with a file (e.g. cat;dog;parrot).
If you were looking for "metadata tags" as in MP3 Album Artist, or EXIF Focal Length, those are actually more simple to access. You can use named properties in the various metadata objects Opus populates for you.
Here is an example script which:
- Loops through all selected files
- Skips folders, and files which do not have image metadata
- For files with a "Focal Length" value but no "Focal Length 35mm" value, the former value is doubled and written into the latter. (Whether this is a valid conversion depends on the camera but is not important for the code example.)
JScript:
function OnClick(clickData)
{
var cmd = clickData.func.command;
// cmd.deselect = false; // Prevent automatic deselection
for (var eSel = new Enumerator(clickData.func.sourcetab.selected); !eSel.atEnd(); eSel.moveNext())
{
var fileItem = eSel.item();
if (!fileItem.is_dir)
{
var fileMeta = fileItem.metadata;
if (fileMeta == "image")
{
var imageMeta = fileMeta.image;
var focalLength = imageMeta["focallength"];
var focalLength35mm = imageMeta["35mmfocallength"];
if (typeof focalLength == "number"
&& typeof focalLength35mm == "undefined")
{
focalLength35mm = focalLength * 2;
var cmdString = "SetAttr META \"35mmfocallength:" + focalLength35mm + "\"";
cmd.ClearFiles();
cmd.AddFile(fileItem);
cmd.RunCommand(cmdString);
}
}
}
}
}
In the above JScript example, you could use both imageMeta.focallength and imageMeta["focallength"] and either would work. However, imageMeta.35focallength would not work because JScript does not allow property name to start with digits; imageMeta["35focallength"] must be used instead to work around this.
The example above also demonstrates how to check for missing metadata fields using the JScript typeof technique.