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:
--------------------------------------------------------
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
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.
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