Open file if filename contain specific text

Engaged ,
Apr 28, 2022 Apr 28, 2022

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.

TOPICS
Actions and scripting , macOS

Views

199

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

LEGEND , Apr 28, 2022 Apr 28, 2022
File('~/documents/psd') .getFiles(function(v){v.name .indexOf('Italy') + 1 && open(v)})

Likes

Translate

Translate
LEGEND ,
Apr 28, 2022 Apr 28, 2022

Copy link to clipboard

Copied

 

File('~/documents/psd')
.getFiles(function(v){v.name
.indexOf('Italy') + 1 && open(v)})

 

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Apr 29, 2022 Apr 29, 2022

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?

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Apr 29, 2022 Apr 29, 2022

Copy link to clipboard

Copied

 

File('~/documents/psd').getFiles(function(v){v.name
.split(/^Poland_\d{3}/).length - 1 && alert(v.fsName)})

 

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Apr 29, 2022 Apr 29, 2022

Copy link to clipboard

Copied

Perfectly. Thanks again

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Apr 29, 2022 Apr 29, 2022

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)})

 

 

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Apr 29, 2022 Apr 29, 2022

Copy link to clipboard

Copied

Start with double slash.

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Community Professional ,
Apr 30, 2022 Apr 30, 2022

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).

 

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
May 01, 2022 May 01, 2022

Copy link to clipboard

Copied

.IndexOf is fastest, then .split, if used without RegEx in this case.

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
May 04, 2022 May 04, 2022

Copy link to clipboard

Copied

Thank you guys

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
May 18, 2022 May 18, 2022

Copy link to clipboard

Copied

Could you help me to improve this script with alert if there is no such file?

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
May 18, 2022 May 18, 2022

Copy link to clipboard

Copied

LATEST

 

alert(v.name.split(/^Poland_\d{3}/).length-1?v.fsName:'No such file')

 

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines