Search code examples
javascriptvbscriptphotoshop-script

Open all files in folder with JPG extension while scripting Photoshop


Does anyone know how one might open in Photoshop all files with a particular file extension rather than individual file names using VBScript? Alternatively, could I use a JS function to do this?


Solution

  • If I understand correctly, VBScript can be used to externally automate Photoshop, via the Windows Script Host.

    Targeting and Referencing the Application object

    Because you run your AppleScript and VBScript scripts from outside the Photoshop application, the first thing your script should do is indicate that the commands be executed in Photoshop

    Link to PDF, p. 22

    VBScript can access the FileSystemObject type, which is a part of the Microsoft Scripting Runtime library. FileSystemObject allows you to iterate over each file in a folder and check the extension.

    Option Explicit
    
    Dim app
    Set app = CreateObject("Photoshop.Application")
    
    Dim fso
    Set fso = CreateObject("Scripting.FileSystemObject")
    
    Dim fle
    For Each fle In fso.GetFolder("c:\path\to\folder").Files
        If fso.GetExtensionName(fle.Path) = ".jpeg" Then
    
            'Issue the command to open the file in the default format
            'This uses the Open method from the Photoshop object model
            app.Open fle.Path
    
        End If
    Next
    

    References:


    Note that the same code could be written in an externally controlling Javascript file:

    var app = new ActiveXObject('Photoshop.Application');
    var fso = new ActiveXObject('Scripting.FileSystemObject');
    
    var enumerator = new Enumerator(fso.GetFolder('c:\\path\\to\\folder').Files);
    while (!enumerator.atEnd()) {
        var filepath = enumerator.item().Path;
        if (fso.GetExtensionName(filepath) == '.jpeg') {
    
            app.Open(filepath);
    
        }
        enumerator.moveNext();
    }
    

    but the examples in the Scripting Guide are using internally executing Javascript files; they must be saved in a specific folder, and can only be run once the application is open, and within the context of the application.