3 Replies Latest reply on Jan 17, 2017 2:08 PM by Kukurykus

    Script to save in a different location base on file path - Slice Error

    diegobxh Level 1

      Hey all! How are you doing?

       

      First of all, let me thank you in advance for the huge content that is this forum, It's really nice and helped me a lot in creating some simple scripts that evolved my workflow to another level. I don't have lots of programming skills, but I'm good at putting some things together, and this is where I got on this new script that I'm working:

       

      var doc = app.activeDocument;
      var docName = doc.name;
      docName = docName.match(/(.*)(\.[^\.]+)/) ? docName = docName.match(/(.*)(\.[^\.]+)/):docName = [docName, docName];
      var server = "/c/SERVER";
      var caminho = doc.path;
      
      // PEGANDO OS E TIPO DE ARQUIVO PELO CAMINHO
      var OS = caminho.slice(-5, -1);
      var tipo = caminho.slice(-1);
      
      // CRIANDO A VARIÁVEL TIPOCOMPLETO QUE VAI SER O NOME DA PASTA AONDE VAI SER SALVO
      tipo = tipo.toLowerCase();
      if (tipo == "c") {
        var tipocompleto = "Conceito"
      }
      else if (tipo == "l") {
        var tipocompleto = "Lookbook"
      }
      else if (tipo == "s") {
        var tipocompleto = "Still"
      }
      else {
        throw new Error("Não foi encontrado o tipo de imagem (C/L/S) no nome da pasta base.");
      }
      
      // CRIANDO O CAMINHO COMPLETO
      var caminhorede = server + "/" + OS + "/" + tipocompleto
      
      //FUNÇÃO PARA CRIAR AS PASTAS
      function mkdir(path) {  
        var folder = new Folder(path);  
           
        if (!folder.exists) {  
          var parts = path.split('/');  
          parts.pop();  
          mkdir(parts.join('/'));  
          folder.create();  
        }  
      }  
      mkdir(caminhorede);  
      
      //SALVANDO O ARQUIVO
      main();
      function main(){
      var fileName =app.activeDocument.name.replace(/\.[^\.]+$/, ''); 
      var saveFile = File(caminhorede + "/" + fileName + ".tif");
      if(saveFile.exists) saveFile = File(caminhorede + "/" + fileName +".tif");
      SaveTIFF(saveFile);
      };
      function SaveTIFF(saveFile){
      tiffSaveOptions = new TiffSaveOptions();
      tiffSaveOptions.embedColorProfile = true;
      tiffSaveOptions.alphaChannels = true;
      tiffSaveOptions.layers = true;
      tiffSaveOptions.imageCompression = TIFFEncoding.TIFFLZW;
      activeDocument.saveAs(saveFile, tiffSaveOptions, true, Extension.LOWERCASE);
      };
      

       

      Basically what I'm trying to do is to save the file on a different path based on the path that he is saved now. So I'm using the slice to get the portion of the file path that contains the info that I need to choose the destination folder, create it and then save the file.

       

      But I'm getting an error on line 8, on the slice thing:

      Error 24: doc.path.slice is not a function.

      Line: 8

      ->  var OS = doc.path.slice(-5, -1);

      I tested the slice logic in other cases online, and seems to work just fine, but PS is'nt accepting it. What am I doing wrong? Can you guys help me out?

       

      Ah, and I manually inserted the "OS" and "tipo" variables, without slicing them from the file path and It work's just fine.

       

      Thanks again. And have a wonderful 2017.