Copy link to clipboard
Copied
How its possible, using javascript, to open file in PS if i know that file name start with some specific text?
For example I have folder /User/Documents/PSD/ with files:
Italy_215.psd
Italy_216.psd
Poland_725.psd
Germany_325.psd
My needs are to open all files which contain word Italy in filename, using javascript. Thanks in advance.
File('~/documents/psd')
.getFiles(function(v){v.name
.indexOf('Italy') + 1 && open(v)})
findAndOpen('~/Documents/PSD', 'Italy')
function findAndOpen(folder, text) {
var fls = Folder(folder).getFiles(),
r = RegExp('^'+text, 'i');
for (var i = 0; i < fls.length; i++) {
var cur = fls[i];
if (cur instanceof File) {
if (r.test(decodeURI(cur.name))) open(cur)
} else findAndOpen(cur, text)
}
}
Copy link to clipboard
Copied
File('~/documents/psd')
.getFiles(function(v){v.name
.indexOf('Italy') + 1 && open(v)})
Copy link to clipboard
Copied
Great, as always. Thanks @Kukurykus
Just one more thing, is it possible to get full pathname of single psd in case that numbers at the end are variable. To be more precise, I know that in folder /User/Documents/PSD/ exist psd file which start with word Poland_ and three digits at the end, but i dont know which digits will be. Is it possible somehow to get pathname of that file?
Copy link to clipboard
Copied
File('~/documents/psd').getFiles(function(v){v.name
.split(/^Poland_\d{3}/).length - 1 && alert(v.fsName)})
Copy link to clipboard
Copied
Perfectly. Thanks again
Copy link to clipboard
Copied
I know i'm boring but i but i'm run into a new problem, scripts works perfectly when i deal with files on my MAC, but original files are on some local server PC and when i type pathname script does not work even i have access to that files.
For example my server is called rsgm and i try on this way because i got this pathname /rsgm/documets/psd when i copy pathname of some file from that folder. What could be a problem?
File('/rsgm/documents/psd').getFiles(function(v){v.name
.split(/^Poland_\d{3}/).length - 1 && alert(v.fsName)})
Copy link to clipboard
Copied
Start with double slash.
Copy link to clipboard
Copied
As a learning exercise, I wanted to see if I could do this too... These three scripts are slightly different but the same in the end result:
var inputFiles = Folder('~/Documents/PSD').getFiles();
for (var a = 0; a < inputFiles.length; a++) {
if (inputFiles[a].name.indexOf('Italy_') !== -1) {
open(inputFiles[a]);
}
}
var inputFiles = Folder('~/Documents/PSD').getFiles();
for (var a = 0; a < inputFiles.length; a++) {
if (/^Italy_/.test(inputFiles[a].name)) {
open(inputFiles[a]);
}
}
var inputFiles = Folder('~/Documents/PSD').getFiles();
for (var a = 0; a < inputFiles.length; a++) {
if (inputFiles[a].name.match(/^Italy_/)) {
open(inputFiles[a]);
}
}
I personally prefer the second (faster) and third (slower) option as they offer more flexibility via Regular Expressions (which may not be needed in this particular use case where indexOf suffices).
Copy link to clipboard
Copied
.IndexOf is fastest, then .split, if used without RegEx in this case.
Copy link to clipboard
Copied
Thank you guys
Copy link to clipboard
Copied
Could you help me to improve this script with alert if there is no such file?
Copy link to clipboard
Copied
alert(v.name.split(/^Poland_\d{3}/).length-1?v.fsName:'No such file')
Copy link to clipboard
Copied
Dear @Kukurykus and @Stephen_A_Marsh i have an additional question regarding this issue, for example in PSD folder i have some subfolders, is it posible to go true all files of root folder and all subfolders? Thanks
Copy link to clipboard
Copied
@milevic ā Recursive processing into sub-directories is not easy for me, I know others here could share some code (or you could go searching yourself).
Copy link to clipboard
Copied
findAndOpen('~/Documents/PSD', 'Italy')
function findAndOpen(folder, text) {
var fls = Folder(folder).getFiles(),
r = RegExp('^'+text, 'i');
for (var i = 0; i < fls.length; i++) {
var cur = fls[i];
if (cur instanceof File) {
if (r.test(decodeURI(cur.name))) open(cur)
} else findAndOpen(cur, text)
}
}
Copy link to clipboard
Copied
Thanks