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
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.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 atell application "Pixelmator Pro"
block and it still opened the image in Preview.tell application "Image Events" to open theImageFile
. You could then follow withset theImage to the result
and hopefully be in the same place.