Getting Image Info with AppleScriptObjC

Hey Folks,

I’m wanting to yank available information from an image with AppleScriptObjC.

--------------------------------------------------------
use scripting additions
use framework "Foundation"
use framework "AppKit" -- for image stuff
--------------------------------------------------------

tell application "Finder" to set finderSelectionList to selection as alias list
if length of finderSelectionList = 0 then error "No files were selected in the Finder!"
set posixPath to POSIX path of (item 1 of finderSelectionList)

set imageInfo to its getImageInfo:posixPath

--------------------------------------------------------
on getImageInfo:posixPath
   set imageInfo to current application's NSBitmapImageRep's imageRepWithContentsOfFile:posixPath
   return imageInfo
end getImageInfo:
--------------------------------------------------------

This produces an object that looks like so:

NSBitmapImageRep 0x6080008af9c0 Size={726, 1000} ColorSpace=(not yet loaded) BPS=8 BPP=(not yet loaded) Pixels=726x1000 Alpha=NO Planar=NO Format=(not yet loaded) CurrentBacking=nil (faulting) CGImageSource=0x6080012275a0

Firstly – I don’t understand how to pull the pieces out of the record, and I’d appreciate it if someone would educate me.

Secondly – this info doesn’t include the dpi of the image.

Is there a more comprehensive means of getting info from an image?

TIA.

-Chris

What you’re seeing there is just a description of an object — it’s not something you would normally parse. Rather, you get its properties. So if you want the dpi, for example, you would use this:

	set bitmapImage to current application's NSBitmapImageRep's imageRepWithContentsOfFile:posixPath
	-- assuming square pixels
	set pixelWidth to bitmapImage's pixelsWide()
	set theSize to bitmapImage's |size|()
	set theWidth to width of theSize
	set theDpi to pixelWidth / theWidth * 72

Hey Shane,

Thanks.

So…

The information available from NSImageRep would appear to be:

--------------------------------------------------------
# https://developer.apple.com/documentation/appkit/nsimagerep?language=objc
--------------------------------------------------------
use scripting additions
use framework "Foundation"
use framework "AppKit" -- for image stuff
--------------------------------------------------------

tell application "Finder" to set finderSelectionList to selection as alias list
if length of finderSelectionList = 0 then error "No files were selected in the Finder!"
set posixPath to POSIX path of (item 1 of finderSelectionList)

set bitmapImage to current application's NSBitmapImageRep's imageRepWithContentsOfFile:posixPath

tell bitmapImage
   
   bitsPerSample()
   colorSpaceName()
   # alpha()
   # opaque()
   pixelsHigh()
   pixelsWide()
   layoutDirection()
   |size|()
   
end tell

alpha() and opaque() both give me an unrecognized selector error.

Am I missing the syntax somewhere?

-Chris

If you look closely in the docs, you’ll see this:

@property(getter=hasAlpha) BOOL alpha;
@property(getter=isOpaque) BOOL opaque;

That’s telling you to use hasAlpha() and isOpaque().

The other useful method is -valueForProperty:, as in:

bitmapImage's valueForProperty:(current application's NSImageEXIFData)

Is there a way to list the available properties for the object? Or do you have to look them up in the documentation?

You have to look them up (NSBitmapImageRepPropertyKey in this case).

1 Like

As Shane say the getter method has the same name as the property.
You could find more info here
https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/ProgrammingWithObjectiveC/EncapsulatingData/EncapsulatingData.html

use scripting additions
use framework "Foundation"
use framework "AppKit" -- for image stuff
--------------------------------------------------------

tell application "Finder" to set finderSelectionList to selection as alias list
if length of finderSelectionList = 0 then error "No files were selected in the Finder!"
set posixPath to POSIX path of (item 1 of finderSelectionList)

set bitmapImage to current application's NSBitmapImageRep's imageRepWithContentsOfFile:posixPath

set theAlpha to (bitmapImage's valueForKey:"hasAlpha") as boolean
set theOpaque to (bitmapImage's valueForKey:"isOpaque") as boolean

return {alpha:theAlpha, opaque:theOpaque}

but a more simple way be of course to use the properties key and return it to AS record.

use scripting additions
use framework "Foundation"
use framework "AppKit" -- for image stuff
--------------------------------------------------------

tell application "Finder" to set finderSelectionList to selection as alias list
if length of finderSelectionList = 0 then error "No files were selected in the Finder!"
set posixPath to POSIX path of (item 1 of finderSelectionList)

set bitmapImage to current application's NSBitmapImageRep's imageRepWithContentsOfFile:posixPath
(bitmapImage's valueForKey:"properties") as record

1 Like