Skip to main content
Participant
October 1, 2021
Answered

Scripting help

  • October 1, 2021
  • 1 reply
  • 359 views

Hi,

I am writing a script to move photoshop files into a specific folder but I have to press the button several times for it to move all the photoshop files. I want the function to move all the photoshop files into the folder in one click and not several. Any help is much appreciated. Below is the code - 

movepsd.onClick = function (){
var psdFolder = null;
  for (var i = 1; i <= app.project.numItems; i++){
    if ((app.project.item(i) instanceof FolderItem) && (app.project.item(i).name == "psd")){
        
      psdFolder = app.project.item(i);
      break;
    }
  }

  if (psdFolder == null) return;
for(var i = 1; i <= app.project.numItems; i++){
    var splitName = app.project.item(i).name.split(".");
    if((app.project.item(i) instanceof FootageItem) && (splitName[splitName.length-1].toLowerCase() == "psd") || (splitName[splitName.length-1].toLowerCase() == "PSD"))
      app.project.item(i).parentFolder = psdFolder;
}
}

 
Thanks,

This topic has been closed for replies.
Correct answer Dan Ebberts

I think the issue is probably that as you are looping through the project bin by index, you're moving the items, which will change the indices. I'd first put all the psd files in an array, then move the items in the array to the folder. Like this:

movepsd.onClick = function (){
var psdFolder = null;
  for (var i = 1; i <= app.project.numItems; i++){
    if ((app.project.item(i) instanceof FolderItem) && (app.project.item(i).name == "psd")){
      psdFolder = app.project.item(i);
      break;
    }
  }
  if (psdFolder == null) return;
    var psds = [];
    for(var i = 1; i <= app.project.numItems; i++){
      var splitName = app.project.item(i).name.split(".");
      if((app.project.item(i) instanceof FootageItem) && (splitName[splitName.length-1].toLowerCase() == "psd"))
        psds.push(app.project.item(i));
    }
    for (var i = 0; i < psds.length; i++)
      psds[i].parentFolder = psdFolder;
}

 

1 reply

Dan Ebberts
Community Expert
Dan EbbertsCommunity ExpertCorrect answer
Community Expert
October 1, 2021

I think the issue is probably that as you are looping through the project bin by index, you're moving the items, which will change the indices. I'd first put all the psd files in an array, then move the items in the array to the folder. Like this:

movepsd.onClick = function (){
var psdFolder = null;
  for (var i = 1; i <= app.project.numItems; i++){
    if ((app.project.item(i) instanceof FolderItem) && (app.project.item(i).name == "psd")){
      psdFolder = app.project.item(i);
      break;
    }
  }
  if (psdFolder == null) return;
    var psds = [];
    for(var i = 1; i <= app.project.numItems; i++){
      var splitName = app.project.item(i).name.split(".");
      if((app.project.item(i) instanceof FootageItem) && (splitName[splitName.length-1].toLowerCase() == "psd"))
        psds.push(app.project.item(i));
    }
    for (var i = 0; i < psds.length; i++)
      psds[i].parentFolder = psdFolder;
}

 

Participant
October 5, 2021

Thanks Dan,

You were right, Array works like a charm !

Cheers,