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

Open a file using wildcards

Community Beginner ,
Apr 15, 2018 Apr 15, 2018

Copy link to clipboard

Copied

Hi All,

I am somewhat familiar with scripting but have never scripted in photoshop (forgive my newbie ignorance).  I prefer to use VBA if possible.  I have run into a few problems getting started.  I have quite a few images inside a folder.  I want to open only the images that contain a certain string ("*shadow").  I've successfully opened the file using the exact path and filename, but have not been able to figure out how to open the file using widcards or directories.  I tested my directory structure by creating a dummy folder called "Renders_Edited" and the folder is created in the location I would expect.  But I can't seem to get it to open a file unless I use the exact path.

I've tried a number of different approaches.  I'm including some of them below.  Thanks in advance to anyone who takes the time to help a beginner.

Sub Main()

    LayerFiles "C:\Rendermation\" 

End Sub

Sub LayerFiles(strFolder)

Dim ObjPhotoshop

Dim fso, folder, files, folderIdx

Dim arrFileType

    Set fso = CreateObject("Scripting.FileSystemObject")

    Set folder = fso.GetFolder(strFolder)

    Set files = folder.files

Set ObjPhotoshop = CreateObject("Photoshop.Application")

If Not FolderExists(strFolder & "\" & "Renders_Edited") Then

       MkDir strFolder & "\" & "Renders_Edited"

End If

For Each folderIdx In files

arrFileType = Split(folderIdx.Name, ".")

If IsArray(arrFileType) Then

       If arrFileType(UBound(arrFileType)) = "png" Then

'ObjPhotoshop.Open "C:\Rendermation\bluestone_Flair_Channel_Cushion_R_3-4X3-4.3dm_Shadow.png"

' ObjPhotoshop.Open & Chr(34) & strFolder & folderidx.name & Chr(34)

                 ObjPhotoshop.Open "& Chr(34) & strFolder & folderIdx.Name & Chr(34) "

' ObjPhotoshop.Open "C:\Rendermation\Renders\" & "*Shadow"

End If

End If

End Sub

TOPICS
Actions and scripting

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 1 Correct answer

Community Expert , Apr 20, 2018 Apr 20, 2018

Here's an example of a recursive script to get all folders, just change the folder path name to where you want to start. You can then either put your code to do the processing in the recursive function, or you can run a new loop from the folder array, once you know how it get the folders in order to then process images.

#target photoshop

var scrFolder = new Folder('/c/')//gets all the folders on your C drive - change this, as there are a lot of folders!

var fList = new Array()

getFolders (scrFolder)

...

Votes

Translate

Translate
Adobe
Community Expert ,
Apr 15, 2018 Apr 15, 2018

Copy link to clipboard

Copied

Not sure about VB but with JS you define a mask for selecting files:

//scrFolder defined as the source folder in code

var searchMask = '*shadow*';

var fileList = srcFolder.getFiles(searchMask);

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 Beginner ,
Apr 15, 2018 Apr 15, 2018

Copy link to clipboard

Copied

Hi Chuck,

Thanks for helping.

I may be in over my head.

I went back to the most basic and still cant figure out how to access the file in photoshop.

Sub LoopThroughFiles()

Dim ObjPhotoshop

Dim file As Variant

   file = Dir("c:\Rendermation\renders\")

   While (file <> "")

      If InStr(file, "Shadow") > 0 Then

         Set ObjPhotoshop = CreateObject("Photoshop.Application")

        ' ObjPhotoshop.Open ("C:\Rendermation\renders\" & "*Shadow*")

         MsgBox "found " & file

         Exit Sub

      End If

     file = Dir

  Wend

End Sub

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 15, 2018 Apr 15, 2018

Copy link to clipboard

Copied

Most here use javascript to be able to use Mac as well Windows.    I think you got the right Idea from  Chuck javascript getting a list of files in a folder.   That you process and open files that match your wild card. I do not know visual basic so can not tell if the code you posted is correct.   My gut tell me its not  for it looks like you used Dir() as a function to get the list and  later use Dir as a variable in both cases setting variable file.

JJMack

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 15, 2018 Apr 15, 2018

Copy link to clipboard

Copied

You might want to check this link. It talks about getting certain files in VB.

DirectoryInfo.GetFiles Method (String, SearchOption) (System.IO)

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 Beginner ,
Apr 20, 2018 Apr 20, 2018

Copy link to clipboard

Copied

Thanks for the guidance.  With the abundance of information on this forum in Javascript, I gave up on VBA and switched to Javascript.  I'm still wrapping my head around the syntax, but managed to hack my way through and pieced together a script that works for my project. 

However I’m still struggling with looping through files in a directory. 

Currently my script, opens a folder, locates the appropriate files and processes them.
(folder titled “Output_Files” has the required png files. The script successfully processes the appropriate png files.)

What I need it to do is open a folder, open the first subfolder, locate the appropriate png files and process.  Move to the next subfolder, locate the appropriate png files and process. Proceed until all the files within all the subfolder have been processed. (folder located at “C:\Rendermation\Renders” has subfolders with random names like “View_ItemNumber_1234” and within this subfolder are the required pngs to process.)

Hope this makes sense.

Anybody have ideas?  Thanks so much in advance.

BTW, how to i iplace code in a thread and have it view like code (see Chuck Uebeles example above)? 

cTID = function(s) { return app.charIDToTypeID(s); };

sTID = function(s) { return app.stringIDToTypeID(s); };

var srcFolder = Folder( Folder.desktop + '/Output_Files' ); // Make sure this is correct for your hard coded path???

//scrFolder defined as the source folder in code 

//Search folder for VIEW Bluestone & CenterGem png 

var str1 = '*bluestone**CenterGem.png*'  ;

var fileList = srcFolder.getFiles(str1); 

for ( var i = 0; i < fileList.length; i++ )

// Open VIEW Bluestone & CenterGem png

app.open( fileList ) ;

//Search folder for VIEW Bluestone & Shadow png 

var str2 = '*bluestone**Shadow.png*'  ;

var fileList = srcFolder.getFiles(str2);

for ( var i = 0; i < fileList.length; i++ ) 

// Open VIEW Bluestone & Shadow png Copy Then Close and Paste In Place in original file

file = app.open( fileList ) ;

//prepare active document

   activeFile= app.activeDocument;

//  activeFile.resizeImage(width,height); //resize image into given size i.e 640x480

   activeFile.selection.selectAll();

   activeFile.selection.copy(); //copy image into clipboard

   activeFile.close(SaveOptions.DONOTSAVECHANGES); //close image without saving changes

  //activeate function to paste in place

  pasteInPlace();

 

  createSmartObject(app.activeDocument.activeLayer);

 

app.load(File('~/Desktop/TrimGem.atn'));

app.doAction('TrimGem','Set 1')

// reference open doc

var doc = app.activeDocument;

// set save options

var opts = new ExportOptionsSaveForWeb();

opts.PNG8 = false;

opts.transparency = true;

opts.interlaced = false;

opts.quality = 100;

opts.includeProfile = false;

opts.format = SaveDocumentType.PNG; // Document Type

// save png file in same folder as open doc

activeDocument.exportDocument(doc.path, ExportType.SAVEFORWEB, opts);

activeDocument.close(SaveOptions.DONOTSAVECHANGES); //close image without saving changes

function pasteInPlace(enabled, withDialog) {

    if (enabled != undefined && !enabled)

      return;

    var dialogMode = (withDialog ? DialogModes.ALL : DialogModes.NO);

    var desc1 = new ActionDescriptor();

    desc1.putBoolean(sTID("inPlace"), true);

    desc1.putEnumerated(cTID('AntA'), cTID('Annt'), cTID('Anno'));

    executeAction(cTID('past'), desc1, dialogMode);

  };

function createSmartObject(layer)

{

    var idnewPlacedLayer = stringIDToTypeID( 'newPlacedLayer' );

    executeAction(idnewPlacedLayer, undefined, DialogModes.NO);

}

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 20, 2018 Apr 20, 2018

Copy link to clipboard

Copied

Photoshop Preset\Scripts folder contains some of the Scripts Installed with Photoshop and the are programmed with JavaScript and saved readable.  They are very good Photoshop Scripts to look at to see how to code some things.  The Image Processor script can do what  you want to do Process a tree of images files.  I would suggest you study that script.

JJMack

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 20, 2018 Apr 20, 2018

Copy link to clipboard

Copied

If you want to go through sub folders, you need to use a recursive function. So what you do is make a function to process all the files within a folder, and you want to put the name of the folder in a call statement. In the loop within the function, you do a check to see if any files are folders. If so, you call the same function, but with the new folder' name and path. Not on my computer, so can't really give an example.

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 20, 2018 Apr 20, 2018

Copy link to clipboard

Copied

Here's an example of a recursive script to get all folders, just change the folder path name to where you want to start. You can then either put your code to do the processing in the recursive function, or you can run a new loop from the folder array, once you know how it get the folders in order to then process images.

#target photoshop

var scrFolder = new Folder('/c/')//gets all the folders on your C drive - change this, as there are a lot of folders!

var fList = new Array()

getFolders (scrFolder)

function getFolders(fold){

    fList.push(fold)

    var fileList = fold.getFiles()

    for (var i=0;i<fileList.length;i++){

        if(fileList instanceof Folder){

            getFolders (fileList);

            }

         else{}//you can enter code here to check for the file types you want or to process the images. You can get all the file here with a search mask.

        }

    }

for(var i=0;i<fList.length;i++){//write a list of all the folders.

    $.writeln(fList.name)

    }

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 Beginner ,
Apr 22, 2018 Apr 22, 2018

Copy link to clipboard

Copied

LATEST

Thank you both very much for taking the time to help a newbie. 

Chuck, I really appreciate the script you sent. It worked perfectly. You are very kind. Much appreciated!

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