﻿{

#include "../omino_adobe_script_suite/src/shared/ominoDialogMaker.jsx"

    function readFile(filePath)
    {
        var f = new File(filePath);

        f.open ("r");
        var length = f.length;
        var result = f.read(length);
        f.close();
        return result;
    }


    function removeComments(st)
    {
        var parts = st.split("\n");
        var result = "";
        for(var ix = 0; ix < parts.length; ix++)
        {
            var part = parts[ix];
            if(part[0] == "#")
                continue;
            if(result.length > 0)
                result += "\n";
            if(part.length > 0)
                result += part;
        }
        return result;
    }

    function doOneEntry(doc, itemName, replacementText)
    {
        if(replacementText.split("\n").length < 1)
        {
            return;
        }
        var replacements = 0;
        for(var layerIx = 0; layerIx < doc.layers.length; layerIx++)
        {
            var layer = doc.layers[layerIx];
            var textFrames = layer.textFrames;
            for(var textFrameIx = 0; textFrameIx < textFrames.length; textFrameIx++)
            {                
                var textFrame = textFrames[textFrameIx];
                if(textFrame.name == itemName)
                {
                    var z = 23;
                    var zz = z+1;
                    textFrame.contents = replacementText;
                    replacements++;
                }
            }
        }
        if(replacements == 0)
        {
            alert("No items named " + itemName);
        }
        else
        {
//~             alert("would print\n" + replacementText);
            doc.print();
       }
 
    }

    function main()
    {
        var listData="you\naddress\n\nme\naddress\n\nthem\naddress";
        listData = readFile("/Volumes/reflections_iii/projects_5_2015/2015-12_holidayOutgo/list.txt");
        var itemToAlter = "Addie";
        
        var docRef = documents[0];
        var dataItems = listData.split("\n\n");
        
        for(var ix = 0; ix < dataItems.length; ix++)
        {
            var replacementText = dataItems[ix];
            replacementText = removeComments(replacementText);
            doOneEntry(docRef, itemToAlter, replacementText);
        }
        
        

    }
    




    main();
}