I’m just starting to learn Applescript so please excuse the basic nature of this question.
I want to use Applescript to get Photoshop to process all the images in a folder. My guess on how to do this would be to load all the file names into an array variable then use this variable within a set of photoshop commands which loops through all the values in the array variable. For a start, I’m not sure if this is the best way to do it - is there an easier way? Secondly I can’t get past first base of loading the file names into a variable. I’m trying variations of the following code but can’t seem to get it to work:
tell application “Finder”
set myFileList to (files of entire contents of “disk1:jobs:testjob”) as alias list
end tell
Where “testjob” is a directory containing the images in directory “jobs” on disk “disk1”
Hi,
Finder’s entire contents could be quite slow. Traversing subfolders with Spotlight is much faster
set inputFolder to quoted form of POSIX path of "disk1:jobs:testjob:"
set foundImages to paragraphs of (do shell script "/usr/bin/mdfind -onlyin " & inputFolder & " 'kMDItemContentTypeTree == \"*public.image*\"'")
repeat with i in foundImages
set oneImage to POSIX file i as alias
-- process file
end repeat
Thanks Stefan
That’s great and I really appreciate your help but it seems a bit complicated - I’d like to be able to understand the code and I don’t understand shell scripts. Maybe Applescript is too complicated for me but I would have thought you would only need basic Applescript to automate the processing of files in a directory without having to go into shell scripts. I would have thought this sort of thing would be Applescript’s bread and butter.
I ran your script replacing the process file line with:
tell application “Adobe Photoshop CS4”
open file oneImage
end tell
and got the error message “can’t make some data into the expected type”. The files in the directory are all photoshop files and open fine in photoshop so I don’t know what’s going wrong.
omit file
open oneImage
The shell script searches for the metadata tag “public.image” and returns a list of matching POSIX paths
In the repeat loop each POSIX path will be coerced to alias before processing
Thanks again Stefan but isn’t there an easier way to do this? As I said in my first post, processing files in a folder has to be a common requirement. I can’t believe you have to resort to shell scripting - I’m afraid I still don’t understanding what you are doing with the shell script.
Spotlight (the shell script) is the easiest and fastest way to search recursively in folders.
Using Metadata can filter the appropriate files at the same time.
The Finder equivalent is
set inputFolder to "disk1:jobs:testjob:"
tell application "Finder" to set foundImages to files of entire contents of folder inputFolder
repeat with i in foundImages
set oneImage to i as alias
-- process file
end repeat
which considers all files
Thanks Stefan, that works fine.
Incidently, in answer to my problem of getting the error message “can’t make some data into the expected type” - for anyone reading this - that was due to my line
open file oneImage
which should have been
open oneImage
because I think oneImage is not a string but a reference (quoting something I found in another forum on this).
Also, for my purposes - processing files in photoshop - my initial tests seem to show the non shell script method just as quick as the shell script method. Correct me if I’m wrong but I think this is because the time taken to run this part of the script is minute when compared to the time taken to open and process the images (about 4 seconds for each image)