Hey Everyone,
I'm working on making some of my scripts a little more helpful and wanted to ask two things.
First, here's what I'm doing. Our file names use a protocol which has four digits at the beginning of the name representing the item number (e.g. 1234-RestOfName.PSD), and I'm using a substring to read the first two digits to select a folder (in this case the folder is 1200s).
Code: Select allvar fileName = myDoc.name
if (fileName.substring (0, 2) ==12) {
var myHiResPrompt = Folder("/Volumes/mac share/Product Images/300 DPI/1200s")
myDoc.saveAs (new File (myHiResPrompt),jpgSaveOptions,false, Extension.LOWERCASE)
}
Question number one is, if I specify a folder for each two digit combination all the way through 99, is that going to impact performance? It would be 99 if statements in a row. I could break it down some, like do ten ifs that sort the first number, then ten subsequent for each secondary number. So, if 1 check if 10, 11, 12, 13, 14, etc. then assign the right folder.
Question number two is if it's possible to determine the next folder as well. So the actual path for saving is "/Volumes/mac share/Product Images/300 DPI/1200s/1234".
We have hundreds of products, so I don't want to specify each and every one, plus they get added to occasionally and I'd have to go in and edit the script to reflect that each time. Is it possible to:
• determine the full four digits and choose the corresponding folder without actually specifying each and every number within the script
• and if the folder doesn't exist, ask to create it
Using numbers in a file name to determine a folder location
-
Mike Hale
Using numbers in a file name to determine a folder location
If I understand what you want to do all you really need to test for is that the document name has four numbers at the start of the name and that the folder based on that name exists.
Something like this
Code: Select allvar fileName = app.activeDocument.name;
var saveName = fileName.match(/^(\d{2})(\d{2})/);
if(saveName != null ){// make sure name has 4 numbers at the start
var saveFolder = saveName[1]+'00s';// user first two nubmers for folder name
var myHiResPrompt = File("/Volumes/mac share/Product Images/300 DPI/"+saveFolder+'/'+saveName[1]+saveName[2]+'.jpg'); // use all four numbers as filename.
if(!myHiResPrompt.parent.exists) myHiResPrompt.parent.create();// test that the folder exists before trying to save
myDoc.saveAs (new File (myHiResPrompt),jpgSaveOptions,false, Extension.LOWERCASE);
}
Something like this
Code: Select allvar fileName = app.activeDocument.name;
var saveName = fileName.match(/^(\d{2})(\d{2})/);
if(saveName != null ){// make sure name has 4 numbers at the start
var saveFolder = saveName[1]+'00s';// user first two nubmers for folder name
var myHiResPrompt = File("/Volumes/mac share/Product Images/300 DPI/"+saveFolder+'/'+saveName[1]+saveName[2]+'.jpg'); // use all four numbers as filename.
if(!myHiResPrompt.parent.exists) myHiResPrompt.parent.create();// test that the folder exists before trying to save
myDoc.saveAs (new File (myHiResPrompt),jpgSaveOptions,false, Extension.LOWERCASE);
}
-
essejesse
Using numbers in a file name to determine a folder location
this is so sweet, I can't even explain. I had to make
var myHiResPrompt = File("/Volumes/mac share/Product Images/300 DPI/"+saveFolder+'/'+saveName[1]+saveName[2]+'.jpg');
var myHiResPrompt = File("/Volumes/mac share/Product Images/300 DPI/"+saveFolder+'/'+saveName[1]+saveName[2]);
to get it to select the folder I wanted, but after that, this thing is performing perfectly over the times I've tested it.
When the final folder doesn't exist, it gets you as close as possible. At first I thought it would be cool if it created that folder for you, but I've kind of gone back and forth on that. Occasionally our server runs really slowly and I'd hate for it to not see it and create a folder that overwrites information.
However, if I did want it to make the folder for you if it didn't exist, how would I do that?
And also, I can understand almost everything you've got there for code, except this stuff
(/^(\d{2})(\d{2})/)
Would you be so kind as to explain what these magical symbols are doing to make this script work?
If I had to guess, I'd say the first / is to indicate another level deep in the folder hierarchy. No clue on the ^, but then the (\d{2}) somehow means numbers, two of them. Am I close?
var myHiResPrompt = File("/Volumes/mac share/Product Images/300 DPI/"+saveFolder+'/'+saveName[1]+saveName[2]+'.jpg');
var myHiResPrompt = File("/Volumes/mac share/Product Images/300 DPI/"+saveFolder+'/'+saveName[1]+saveName[2]);
to get it to select the folder I wanted, but after that, this thing is performing perfectly over the times I've tested it.
When the final folder doesn't exist, it gets you as close as possible. At first I thought it would be cool if it created that folder for you, but I've kind of gone back and forth on that. Occasionally our server runs really slowly and I'd hate for it to not see it and create a folder that overwrites information.
However, if I did want it to make the folder for you if it didn't exist, how would I do that?
And also, I can understand almost everything you've got there for code, except this stuff
(/^(\d{2})(\d{2})/)
Would you be so kind as to explain what these magical symbols are doing to make this script work?
If I had to guess, I'd say the first / is to indicate another level deep in the folder hierarchy. No clue on the ^, but then the (\d{2}) somehow means numbers, two of them. Am I close?
-
essejesse
Using numbers in a file name to determine a folder location
I thought this part was trying to create a folder actually, but it doesn't, so now I'm not sure what it does.
if(!myHiResPrompt.parent.exists) myHiResPrompt.parent.create();// test that the folder exists before trying to save
also, I'm picking up that ! right before something mean it's not true. like != means not equal, and in this case !myHiResPrompt.parent.exists would mean the parent of the prompt exists. Right?
if(!myHiResPrompt.parent.exists) myHiResPrompt.parent.create();// test that the folder exists before trying to save
also, I'm picking up that ! right before something mean it's not true. like != means not equal, and in this case !myHiResPrompt.parent.exists would mean the parent of the prompt exists. Right?
-
Mike Hale
Using numbers in a file name to determine a folder location
exists is a file and folder property that is true if the file or folder exists on the disc. create() will create a file or folder object on the drive but only for one level. That is to say if your path is "/Volumes/mac share/Product Images/300 DPI/1200s/1234" then the folder "/Volumes/mac share/Product Images/300 DPI/1200s/" has to exist before you can create the '1234' folder.
/^(\d{2})(\d{2})/ is a Regular Expression. The // mark the start and end of the expression. ^ means the start of the string \d means any number. {2} means exactly twice. The () means remember the match for use later. So if it matches you have an array something like this. 1234,12,34. If is doesn't find a match it returns null.
/^(\d{2})(\d{2})/ is a Regular Expression. The // mark the start and end of the expression. ^ means the start of the string \d means any number. {2} means exactly twice. The () means remember the match for use later. So if it matches you have an array something like this. 1234,12,34. If is doesn't find a match it returns null.
-
essejesse
Using numbers in a file name to determine a folder location
Ok, thank you. I'll have to look into the types of variables you can use in regular expressions. They are obviously pretty powerful tools.
So I modified a bit to make it prompt if the user wants to make the folder, given that it doesn't exist. I want people to be aware in case something crazy happens. Either way, it checks twice before you can anyway, once to let you know it's not there, and then in the folder create part, again. And it lets them say no, and still access the save as dialog, so options abound.
Code: Select allvar fileName = myDoc.name;
var saveName = fileName.match(/^(\d{2})(\d{2})/);
if(saveName != null ){// make sure name has 4 numbers at the start
var saveFolder = saveName[1]+'00s';// user first two nubmers for folder name
var myHiResPrompt = File("/Volumes/mac share/Product Images/300 DPI/"+saveFolder+'/'+saveName[1]+saveName[2]); // use all four numbers as filename.
if (myHiResPrompt.exists){
myDoc.saveAs (new File (myHiResPrompt),jpgSaveOptions,false, Extension.LOWERCASE);
}
else var createFolder = confirm ("The folder 300 DPI/"+saveName[1]+saveName[2]+" does not exist, do you wish to create it?") {;
if (createFolder) {
var hiResFolder = new Folder("Volumes/mac share/Product IMages/300 DPI/"+saveFolder+'/'+saveName[1]+saveName[2])
if(!hiResFolder.exists) hiResFolder.create();
myDoc.saveAs (new File (myHiResPrompt),jpgSaveOptions,false, Extension.LOWERCASE);
}
else {
myDoc.saveAs (new File (myHiResPrompt),jpgSaveOptions,false, Extension.LOWERCASE);
}
}
}
So I modified a bit to make it prompt if the user wants to make the folder, given that it doesn't exist. I want people to be aware in case something crazy happens. Either way, it checks twice before you can anyway, once to let you know it's not there, and then in the folder create part, again. And it lets them say no, and still access the save as dialog, so options abound.
Code: Select allvar fileName = myDoc.name;
var saveName = fileName.match(/^(\d{2})(\d{2})/);
if(saveName != null ){// make sure name has 4 numbers at the start
var saveFolder = saveName[1]+'00s';// user first two nubmers for folder name
var myHiResPrompt = File("/Volumes/mac share/Product Images/300 DPI/"+saveFolder+'/'+saveName[1]+saveName[2]); // use all four numbers as filename.
if (myHiResPrompt.exists){
myDoc.saveAs (new File (myHiResPrompt),jpgSaveOptions,false, Extension.LOWERCASE);
}
else var createFolder = confirm ("The folder 300 DPI/"+saveName[1]+saveName[2]+" does not exist, do you wish to create it?") {;
if (createFolder) {
var hiResFolder = new Folder("Volumes/mac share/Product IMages/300 DPI/"+saveFolder+'/'+saveName[1]+saveName[2])
if(!hiResFolder.exists) hiResFolder.create();
myDoc.saveAs (new File (myHiResPrompt),jpgSaveOptions,false, Extension.LOWERCASE);
}
else {
myDoc.saveAs (new File (myHiResPrompt),jpgSaveOptions,false, Extension.LOWERCASE);
}
}
}
-
essejesse
Using numbers in a file name to determine a folder location
for some reason, I had to change this at the end
Code: Select allelse {
myDoc.saveAs (new File (myHiResPrompt),jpgSaveOptions,false, Extension.LOWERCASE);
}
to this
Code: Select allif (!createFolder) {
myDoc.saveAs (new File (myHiResPrompt),jpgSaveOptions,false, Extension.LOWERCASE);
}
Otherwise, if the folder didn't exist, it would ask me to save the document twice. Seems to me like the old way and the new way are nearly identical, but for some reason, this way works and the old one doesn't
Code: Select allelse {
myDoc.saveAs (new File (myHiResPrompt),jpgSaveOptions,false, Extension.LOWERCASE);
}
to this
Code: Select allif (!createFolder) {
myDoc.saveAs (new File (myHiResPrompt),jpgSaveOptions,false, Extension.LOWERCASE);
}
Otherwise, if the folder didn't exist, it would ask me to save the document twice. Seems to me like the old way and the new way are nearly identical, but for some reason, this way works and the old one doesn't
-
essejesse
Using numbers in a file name to determine a folder location
nope, for some reason, I can't get it to stop asking me to save to the same place twice. Here's the relevant section of code.
Code: Select allvar fileName = myDoc.name;
var saveName = fileName.match(/^(\d{2})(\d{2})/);
if(saveName != null ){// make sure name has 4 numbers at the start
var saveFolder = saveName[1]+'00s';// use first two nubmers for folder name
var myHiResPrompt = Folder("/Volumes/mac share/Product Images/300 DPI/"+saveFolder+'/'+saveName[1]+saveName[2]); //assigning the proper folder
if (myHiResPrompt.exists){ // use all four numbers of filename to check if the proper folder exists.
myDoc.saveAs (new File (myHiResPrompt),jpgSaveOptions,false, Extension.LOWERCASE);
}
else var createFolder = confirm ("The folder 300 DPI/"+saveName[1]+saveName[2]+" does not exist, do you wish to create it?") {;
if (createFolder) {
var hiResFolder = new Folder("/Volumes/mac share/Product Images/300 DPI/"+saveFolder+'/'+saveName[1]+saveName[2])
if(!hiResFolder.exists) hiResFolder.create();
myDoc.saveAs (new File (myHiResPrompt),jpgSaveOptions,false, Extension.LOWERCASE);
}
/*The next three lines are what's doing it. I don't get it though, aren't they a part of the else just above? I tried first doing an if (createFolder) and then an else, but that did the same thing. So I changed it to the if (createFolder) then if (!createFolder) shown here, but that's still not working.*/
if (!createFolder) { /*I commented the next three lines because they are what's doing it. I don't get it though, aren't they a part of the else just above?*/
myDoc.saveAs (new File (myHiResPrompt),jpgSaveOptions,false, Extension.LOWERCASE);
}
}
}
Code: Select allvar fileName = myDoc.name;
var saveName = fileName.match(/^(\d{2})(\d{2})/);
if(saveName != null ){// make sure name has 4 numbers at the start
var saveFolder = saveName[1]+'00s';// use first two nubmers for folder name
var myHiResPrompt = Folder("/Volumes/mac share/Product Images/300 DPI/"+saveFolder+'/'+saveName[1]+saveName[2]); //assigning the proper folder
if (myHiResPrompt.exists){ // use all four numbers of filename to check if the proper folder exists.
myDoc.saveAs (new File (myHiResPrompt),jpgSaveOptions,false, Extension.LOWERCASE);
}
else var createFolder = confirm ("The folder 300 DPI/"+saveName[1]+saveName[2]+" does not exist, do you wish to create it?") {;
if (createFolder) {
var hiResFolder = new Folder("/Volumes/mac share/Product Images/300 DPI/"+saveFolder+'/'+saveName[1]+saveName[2])
if(!hiResFolder.exists) hiResFolder.create();
myDoc.saveAs (new File (myHiResPrompt),jpgSaveOptions,false, Extension.LOWERCASE);
}
/*The next three lines are what's doing it. I don't get it though, aren't they a part of the else just above? I tried first doing an if (createFolder) and then an else, but that did the same thing. So I changed it to the if (createFolder) then if (!createFolder) shown here, but that's still not working.*/
if (!createFolder) { /*I commented the next three lines because they are what's doing it. I don't get it though, aren't they a part of the else just above?*/
myDoc.saveAs (new File (myHiResPrompt),jpgSaveOptions,false, Extension.LOWERCASE);
}
}
}
-
Mike Hale
Using numbers in a file name to determine a folder location
I think it is the logic of your nested if statements that is causing the problem. Here is a different way to write that code.
Code: Select allvar fileName = myDoc.name;
var saveName = fileName.match(/^(\d{2})(\d{2})/);
if(saveName != null ){// make sure name has 4 numbers at the start
var saveFolder = saveName[1]+'00s';// use first two nubmers for folder name
var myHiResPrompt = Folder("/Volumes/mac share/Product Images/300 DPI/"+saveFolder+'/'+saveName[1]+saveName[2]); //assigning the proper folder
if (!myHiResPrompt.exists){// if folder doesn't exists ask it they want to create it.
var createFolder = confirm ("The folder 300 DPI/"+saveName[1]+saveName[2]+" does not exist, do you wish to create it?");
if (createFolder==true) {// if they say yes then go ahead and create
myHiResPrompt.create();
}
}
if (myHiResPrompt.exists) {// the folder either already existed or user just created so save
myDoc.saveAs (new File (myHiResPrompt),jpgSaveOptions,false, Extension.LOWERCASE);
}// folder doesn't exists so user must have said no - do nothing.
}// end of not null
Code: Select allvar fileName = myDoc.name;
var saveName = fileName.match(/^(\d{2})(\d{2})/);
if(saveName != null ){// make sure name has 4 numbers at the start
var saveFolder = saveName[1]+'00s';// use first two nubmers for folder name
var myHiResPrompt = Folder("/Volumes/mac share/Product Images/300 DPI/"+saveFolder+'/'+saveName[1]+saveName[2]); //assigning the proper folder
if (!myHiResPrompt.exists){// if folder doesn't exists ask it they want to create it.
var createFolder = confirm ("The folder 300 DPI/"+saveName[1]+saveName[2]+" does not exist, do you wish to create it?");
if (createFolder==true) {// if they say yes then go ahead and create
myHiResPrompt.create();
}
}
if (myHiResPrompt.exists) {// the folder either already existed or user just created so save
myDoc.saveAs (new File (myHiResPrompt),jpgSaveOptions,false, Extension.LOWERCASE);
}// folder doesn't exists so user must have said no - do nothing.
}// end of not null
-
essejesse
Using numbers in a file name to determine a folder location
That's a lot simpler too. I guess I'm over-complicating things a little bit. I'll try to keep it as concise as possible in the future.