0

According to the dictionary for Image Events, the open verb is supposed to return a document object. This is also how it's used in multiple online examples.

However, when I try to use it, the script correctly opens the file in Preview, and does not return anything. Even when I try the following example, from Apple's own documentation, I get a "Can’t get properties of missing value." number -1728 from properties of missing value.

How are you meant to get an image object from an existing file in order to manipulate it with other actions from Image Events?

I'm on Big Sur 11.6.2 in case this doesn't affect older/newer releases.

-- Prompt for an image
set theImageFile to choose file of type "public.image" with prompt ""

-- Launch Image Events
tell application "Image Events"
    launch
    
    -- Open the image
    set theImage to open theImageFile
    
    -- Read the image's properties
    tell theImage
        properties
        --> {color space:RGB, image file:file "Macintosh HD:Users:YourUserName:Desktop:My Image.png" of application "Image Events", bit depth:millions of colors, dimensions:{293, 252}, location:folder "Macintosh HD:Users:YourUserName:Desktop:" of application "Image Events", embedded profile:profile "Thunderbolt Display" of image "My Image.png" of application "Image Events", file type:PNG, class:image, name:"My Image.png", resolution:{72.0, 72.0}}
        
        -- Read the image's resolution
        resolution
        --> {72.0, 72.0}
        
        -- Read the image's type
        file type
        --> PNG
        
        -- Read the name of the image's embedded profile
        name of embedded profile
        --> "Thunderbolt Display"
    end tell
end tell
6
  • 1
    Using 'open' in Image Events doesn't open a file in a GUI app (e.g. Preview). It opens the image that's stored within the file. As the documentation you reference suggests, you shouldn't see anything as Image Events is a background app. Your code above works fine in Sierra (including returning an image object) so maybe it's a Big Sur thing.
    – Mockman
    Commented Dec 25, 2021 at 5:26
  • 1
    Something to try… insert this immediately below your 'set theImageFile…' line: set theImagefile to POSIX path of theImagefile. What it does is get a posix path reference to the alias that the first line generates. Image Events can use either format, and there have been some issues around aliases in Big Sur.
    – Mockman
    Commented Dec 25, 2021 at 5:58
  • @Mockman, thank you for checking. It seems like the open verb infuriatingly disregards the tell block that it's inside, and always opens a document in Preview. To test this, I placed it inside a tell application "Pixelmator Pro" block and it still opened the image in Preview.
    – pikappa
    Commented Dec 25, 2021 at 8:49
  • @Mockman, I just tried the workaround you suggested and it worked! Please add it as an answer so that I can mark it as being correct :)
    – pikappa
    Commented Dec 25, 2021 at 8:57
  • Interesting, but not in a good way. I wonder how you are supposed to know some of these outcomes. Out of curiosity, what happens if you combine 'tell' and 'open' like this: tell application "Image Events" to open theImageFile. You could then follow with set theImage to the result and hopefully be in the same place.
    – Mockman
    Commented Dec 26, 2021 at 0:29

1 Answer 1

1

Insert this immediately below your set theImageFile… line:

set theImagefile to POSIX path of theImagefile

This gets a posix path reference to the alias that the first line generated. Image Events can use either format, and there have been some issues around aliases in Big Sur.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .