pythonmacosapplescriptfindersourceforge-appscript

How does one feed a list of files into an app with appscript and Python?


Get your newb-shields up, I'm about to sprinkle you with some.
I'm trying to get Photoshop CS4 to open a folderful of JPEG images with AppScript+Python, which could be described like so in BASH:

#!/bin/bash
for F in `ls ~/Desktop/test`; do
    open -a "Adobe Photoshop CS4" $F
    # proceed to mutilate the image appearance
done

I'm failing at the ls ~/Desktop/test stage. I'd really like to ask Finder to list a folder for me, and feed the result into Photoshop one at a time to process them.
A JavaScript equivalent with Adobe's ExtendScript bridge would be:

#target photoshop
var folder = Folder("~/Desktop/test");
var images = folder.getFiles();
for (var i=0; i < images.length; i++) {
    if (images[i] instanceof File && images[i].hidden == false) {
        var doc = app.open(images[i]);
        // do something to the image here
        doc.close(SaveOptions.DONOTSAVECHANGES);
    }
}

I can get me a document_file object with a horribly silly contraption like x = app('Finder').home.folders['Desktop']().folders['test']().items()[0], but that turns out to be a really silly object. Trying to app('Adobe Photoshop CS4').open(x) this object will throw an OSERROR: 1230, with a MESSAGE: File/Folder expected.

(Uh-oh, this document_file actually responds to URL(), so File.makewithurl(x.URL()) can be fed into open())

Ehm, having solved that, is there a way to actually do this by asking Finder for the list of files in a given folder, specified by a UNIX path?


Solution

  • Photoshop's open command requires an alias object (or list of alias objects).

    from appscript import *
    
    folder = '/path/to/folder'
    
    ps = app('Adobe Photoshop CS4')
    
    for f in app('Finder').items[mactypes.Alias(folder)].files.get(resulttype=k.alias):
        ps.open(f)
        # do stuff here...