2 Replies Latest reply on Aug 8, 2015 4:46 AM by c.pfaffenbichler

    Read multiple vars from file + blendMode question.

    Nyx702

      I only have basic knowledge of js and have two questions. The script I am trying to create is a toggle. It does something on the 1st run and then something else on the 2nd. The only way I could figure out how to do this is by saving a number to a file and then checking it again when I run it. If there is a better way please let me know!

       

      1) Right now I'm just creating a new file for other settings I want to read because I cant figure out how to get other lines in the settings file to var X. How do you put each line of a text file in a new var?

       

      2) I seem to have some trouble setting the blending mode with a var?

      var prevBlend = BlendMode.NORMAL;

      app.activeDocument.activeLayer.blendMode = BlendMode.NORMAL; works

      app.activeDocument.activeLayer.blendMode = prevBlend; does not work

       

      Error 1226: Enumerated value expected

       

      Full Script:

       

      var currentLayer = app.activeDocument.activeLayer;
      var currentName = app.activeDocument.activeLayer.name;
      var settingsPath=new Folder(Folder.userData+"/NMtools");
      var blendFile=new File(settingsPath+"/blendMode.data");
      var togFile=new File(settingsPath+"/tog.data");
      var tog = 0;
      var curBlend = activeDocument.activeLayer.blendMode;
      var prevBlend = "BlendMode.NORMAL";
      
      
      if (!settingsPath.exists)
        settingsPath.create();
      
      loadSettings();
      
      
      if( tog == 0){
        alert("frist");
        writeTog(1);
        writeBlend(curBlend);
        alert(curBlend);
        app.activeDocument.activeLayer.blendMode = BlendMode.NORMAL;
        //curBlend=BlendMode.NORMAL;
      }else{
        alert("second");
        writeTog(0);
        loadSettings();
        alert(prevBlend)
        app.activeDocument.activeLayer.blendMode = prevBlend;
      }
      
      function loadSettings()
      {
        if (blendFile.exists)
      {
        //open the preference file
        blendFile.open("r");
        prevBlend=blendFile.read();
        //split the resulting string into an array
        prevBlend=prevBlend.toString();
        //eval(prevBlend);
        blendFile.close();
      }
      
        if (togFile.exists)
      {
        //open the preference file
        togFile.open("r");
          tog=togFile.read();
        //split the resulting string into an array
        //eval(tog);
        togFile.close();
      }
      
      }
      
      
      function writeBlend(inputString)
      {
        blendFile.open("w");
        blendFile.writeln(inputString);
        blendFile.close();
      }
      
      
      function writeTog(inputString)
      {
        togFile.open("w");
        togFile.writeln(inputString);
        togFile.close();
      }
      
        • 1. Re: Read multiple vars from file + blendMode question.
          c.pfaffenbichler Level 9

          I seem to have some trouble setting the blending mode with a var?

          You defined the variable as a String.

          Edit: Try using the line

          1. var prevBlend = "BlendMode.NORMAL"

          without the inverted commas.

          • 2. Re: Read multiple vars from file + blendMode question.
            c.pfaffenbichler Level 9

            You can separate the Strings, Numbers, … by some signifier that you later use to split the String.

            Hopefully this example can help illustrate (starting with an Array, writing a txt file, reading it and splitting it to get an Array again):

            var anArray = ["text 1", "text 2", 3];
            writePref (anArray.join(";"), "~/Desktop/test.txt");
            var theResult = readPref ("~/Desktop/test.txt").split(";");
            alert (theResult.join("\n"));
            ////// read prefs file //////
            function readPref (thePath) {
              if (File(thePath).exists == true) {
                var file = File(thePath);
                file.open("r");
                file.encoding= 'BINARY';
                var theText = new String;
                for (var m = 0; m < file.length; m ++) {
                  theText = theText.concat(file.readch());
                  };
                file.close();
                return String(theText)
                }
              };
            ////// function to write a preference-file storing a text //////
            function writePref (theText, thePath) {
              try {
                var thePrefFile = new File(thePath);
                thePrefFile.open("w");
                for (var m = 0; m < theText.length; m ++) {
                  thePrefFile.write(theText[m])
                  };
                thePrefFile.close()
                }
              catch (e) {};
              };