• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

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

1.0K

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 2 Correct answers

LEGEND , Apr 28, 2022 Apr 28, 2022

 

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

 

Votes

Translate

Translate
Guide , Jun 30, 2022 Jun 30, 2022

 

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

 

Votes

Translate

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

 

Votes

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?

Votes

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

 

Votes

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

Votes

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

 

 

Votes

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.

Votes

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
Community Expert ,
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).

 

Votes

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.

Votes

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

Votes

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?

Votes

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

 

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

 

Votes

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 ,
Jun 30, 2022 Jun 30, 2022

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

Votes

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
Community Expert ,
Jun 30, 2022 Jun 30, 2022

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

Votes

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
Guide ,
Jun 30, 2022 Jun 30, 2022

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

 

Votes

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 ,
Jul 01, 2022 Jul 01, 2022

Copy link to clipboard

Copied

LATEST

Thanks

Votes

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