post/reporter/reporter_collate_items.js

collate_report_contents();

/**
 * Collates items and creates a list of report contents for processing.
 */
function collate_report_contents() {
    let templ = Template.GetCurrent();

    /* this is set to true when at least one image is found
    if no images are found the generation is terminated (i.e. job control == `Abort`) 
    and a warning message pops up
    */
    let found_automotive_assessment_image = false;

    /* Abort this item if JOB_CONTROL is set to `Abort` */
    let job_control = templ.GetVariableValue(`JOB_CONTROL`);
    if (job_control != null && job_control == `Abort`) {
        LogPrint(`Generation aborted (job control).`);
        Exit();
    }

    /* The report_contents.lst will be written to the output directory */
    let output_dir = templ.GetVariableValue(`OUTPUT_DIR`);
    if (output_dir == null) {
        LogError(`Variable %OUTPUT_DIR% does not exist.`);
        return;
    }
    output_dir = templ.ExpandVariablesInString(output_dir);
    output_dir = output_dir.replace("\\", "/");

    /* Create the output directory if it does not already exist */
    if (!File.IsDirectory(output_dir)) {
        let success = File.Mkdir(output_dir);
        if (!success) {
            LogError(`Unable to create output directory: ${output_dir}`);
            return;
        }
    }

    /* Create report_contents.lst, which will contain a list of all of the
     * automotive assessment contents that will need to be processed. */
    let f_contents_name = `${output_dir}/report_contents.lst`;
    let f_contents = new File(f_contents_name, File.WRITE);

    /* We will search for Image File items with names beginning with an identifiable string */
    let image_item_str = `automotive_assessment_image`;

    /* Loop through all pages in the template, searching for relevant items */
    let pages = templ.GetAllPages();
    for (let p = 0; p < pages.length; p++) {
        let items = pages[p].GetAllItems();
        for (let item of items) {
            if (item.name.substring(0, image_item_str.length) == image_item_str) {
                /* Report any items erroneously given the identifier name */
                if (item.type != Item.IMAGE_FILE) {
                    LogError(`Item "${item.name}" on page ${p + 1} is not an Image File item.`);
                    continue;
                }

                /* Add the image filenames to the report_contents.lst. These
                 * filenames will be parsed in PRIMER to enable the required
                 * automotive assessments to be performed. */
                LogPrint(`Found item "${item.name}" on page ${p + 1} with filename: ${item.file}`);
                found_automotive_assessment_image = true;
                f_contents.Write(`${item.file}\n`);
            }
        }
    }
    f_contents.Close();

    if (!found_automotive_assessment_image) {
        var v_job_control = Variable.GetFromName(templ, `JOB_CONTROL`);
        v_job_control.value = `Abort`;
        LogPrint(`Generation aborted because no automotive assessment images were found.`);
        Exit();
    }
}