Check if Asset exists in PR project

This ExtendScript code checks whether an asset, based on its path, has already been imported or if it is a new one.

Depending on the result, the script returns either "new asset" or "already exists."

// moovIT GmbH
// Check if an asset has (already) been imported into PR project

// B Dimmel
// Version 1.0
// Date: Dec 9th 2024
// Changelog: Dec 9th 2024
// Initial release

// Define the asset variable, this should be the file path of the asset you want to check
var assetFilePath = "{stream.variable.asset_extendscript}";

// Function to check if the asset already exists in the project
function checkAssetExists(assetPath) {
    // Get the current project
    var project = app.project;

    // Iterate through all the root items in the project
    for (var i = 0; i < project.rootItem.children.numItems; i++) {
        var currentItem = project.rootItem.children[i];

        // Check if the current item is a bin (folder)
        if (currentItem.type == ProjectItemType.BIN) {
            // Check recursively inside the bin
            if (checkAssetInBin(currentItem, assetPath)) {
                respond2Helmut4 = "already exists";
                return "already exists";
            }
        } else if (currentItem.type == ProjectItemType.CLIP || currentItem.type == ProjectItemType.FILE) {
            // Check if the current item matches the asset path
            if (currentItem.getMediaPath() == assetPath) {
                respond2Helmut4 = "already exists";
                return "already exists";
            }
        }
    }
    respond2Helmut4 = "new asset";
    return "new asset";
}

// Recursive function to check inside bins
function checkAssetInBin(bin, assetPath) {
    for (var j = 0; j < bin.children.numItems; j++) {
        var childItem = bin.children[j];

        if (childItem.type == ProjectItemType.BIN) {
            // Recursively check inside the bin
            if (checkAssetInBin(childItem, assetPath)) {
                return true;
            }
        } else if (childItem.type == ProjectItemType.CLIP || childItem.type == ProjectItemType.FILE) {
            // Check if the child item matches the asset path
            if (childItem.getMediaPath() == assetPath) {
                return true;
            }
        }
    }
    return false;
}

// Call the function and alert the result
var result = checkAssetExists(assetFilePath);

//alert(result);
respond2Helmut4;

Last updated