Great to hear that the simple test script is running successfully! That means the issue is more likely with the specific functionality in your original metadata export script, rather than with Bridge's ability to run scripts in general.
Let's reintegrate the export functionality step by step, starting from the simple script that is working. We will add components back carefully and test after each step.
- Add Menu Item for Export Metadata Function
Replace the test alert in the script with the command for exporting metadata, but don't add the full functionality just yet. Check if the menu item is correctly added to Bridge:
#target bridge
if (BridgeTalk.appName == "bridge") {
var exportMetadataMenu = MenuElement.create("command", "Export Metadata to Text File", "at the end of Tools");
exportMetadataMenu.onSelect = function () {
alert("Export Metadata command selected. Functionality not implemented yet.");
};
}
Add this to your startup scripts and restart Bridge. Confirm that the "Export Metadata to Text File" menu item appears and that selecting it shows the alert message.
- Implement File Selection and Metadata Reading
Now, let's reintegrate the selection check and metadata extraction into the script. We still won’t write to a file in this step – instead, we will display the metadata we are trying to export in an alert.
#target bridge
if (BridgeTalk.appName == "bridge") {
var exportMetadataMenu = MenuElement.create("command", "Export Metadata to Text File", "at the end of Tools");
exportMetadataMenu.onSelect = function () {
var selection = app.document.selectedThumbnails;
if (selection.length == 1 && selection[0].type == "file") {
var thumb = selection[0];
var metadata = thumb.synchronousMetadata;
if (metadata && metadata.read("iptc")) {
var keywords = metadata.read("iptc:Keywords");
var description = metadata.read("iptc:Caption-Abstract");
var title = metadata.read("iptc:Headline");
var metadataString = "Title: " + (title || "Not available") + "\n" +
"Description: " + (description || "Not available") + "\n" +
"Keywords: " + (keywords || "Not available") + "\n";
// For now, just display the metadata string in an alert
alert(metadataString);
} else {
alert("The selected file does not have any readable IPTC metadata.");
}
} else {
alert("Please select only one file to export metadata.");
}
};
}
Run this script to make sure that the metadata can be read and displayed via an alert.
- Implement File Writing
If step 2 succeeds, and metadata is displayed, it means metadata reading is correct, and you can proceed with the file writing functionality. Here is where we may encounter file path or permissions errors, but since we know the rest of the code works up to this point, it isolates any potential issues:
#target bridge
if (BridgeTalk.appName == "bridge") {
var exportMetadataMenu = MenuElement.create("command", "Export Metadata to Text File", "at the end of Tools");
exportMetadataMenu.onSelect = function () {
var selection = app.document.selectedThumbnails;
if (selection.length == 1 && selection[0].type == "file") {
var thumb = selection[0];
var metadata = thumb.synchronousMetadata;
if (metadata && metadata.read("iptc")) {
var fileInfo = new File(thumb.spec.fsName);
var baseName = fileInfo.name.replace(/\.[^\.]+$/, '');
var textFilePath = fileInfo.path + "/" + baseName + ".txt";
var textFile = new File(textFilePath);
var keywords = metadata.read("iptc:Keywords");
var description = metadata.read("iptc:Caption-Abstract");
var title = metadata.read("iptc:Headline");
var metadataString = "Title: " + (title || "Not available") + "\n" +
"Description: " + (description || "Not available") + "\n" +
"Keywords: " + (keywords || "Not available") + "\n";
if (textFile.open('w')) {
textFile.encoding = "UTF8";
textFile.write(metadataString);
textFile.close();
alert("Metadata saved to text file:\n" + textFilePath);
} else {
alert("Failed to open text file for writing.");
}
} else {
alert("The selected file does not have any readable IPTC metadata.");
}
} else {
alert("Please select only one file to export metadata.");
}
};
}
Try running the final script and watch for whether it writes the metadata to a text file. If it fails at this point, the most likely reasons are problems with the file path or permission issues in the file system.