Search code examples
applescriptimage-events

AppleScript Image Events cant get class asDB


Wanting to learn more about Mac's Image Events I am trying to learn how to save a file from one type into another type. For example, if I have a BMP image named foobar.bmp I wanted to learn how to save it out as foobar.jpg but in my handler I get an error:

Image Events got an error: Can’t get «class asDB» id (application "Image Events") of image "foobar.bmp".

My code inside my handler:

tell application "Finder"
    set directory to (choose folder with prompt "Please select a directory.")
    set someImages to (files of folder directory where name extension is "bmp") as alias list
    repeat with someImage in someImages
        tell application "Image Events"
            launch
            set workingImage to open someImage
            save workingImage as JPEG in directory
            close workingImage
        end tell
    end repeat
end tell

I did test to see if the save may need the POSIX Path instead of the Alias Path with:

set directoryPosix to (POSIX path of directory)

and changed the save:

save workingImage as JPEG in directoryPosix

but I am still producing the same error and I dont understand why. The code works but just throws an error and after searching I am unable to find a resolution. I already know how to do this with Bash using ImageMagick and I could do this using AppleScript and SIPs but I would like to learn more about Image Events. What am I doing wrong to throw the error? If it helps my OS is up-to-date and running Yosemite version 10.10.5.


Solution

  • You need to specify the full (HFS or POSIX) path of the new file rather than the alias specifier to the destination folder:

    set directory to (choose folder with prompt "Please select a directory.") as text
    tell application "Finder" to set someImages to (files of folder directory where name extension is "bmp") as alias list
    
    tell application "Image Events"
        launch
        repeat with someImage in someImages
            set workingImage to open someImage
            set fileName to name of workingImage
            set newFilePath to directory & text 1 thru -4 of fileName & "jpg"
            save workingImage as JPEG in newFilePath
            close workingImage
        end repeat
    end tell