I am sure I am missing something obvious but I can’t seem to get representationUsingType_properties_(NSPNGFileType, {})) to work. The error I get is: “[SetImageAppDelegate insertImage:]: The variable NSPNGFileType is not defined. (error -2753)”. I have tried everything from putting NSPNGFileType in quotes to specifying that it belongs to the NSBitmapImageRep class or the instance thereof (i.e myNsBitmapImageRepObj’s NSPNGFileType). None of that makes any difference. What am I doing wrong?
Sample code:
script SetImageAppDelegate
-- Inheritance
property parent : class "NSObject"
-- IBOutlets
property imageView : missing value
-- Useful aliases for Cocoa Classes
property NSImage : class "NSImage" of current application
property NSData : class "NSData" of current application
property NSBitmapImageRep : class "NSBitmapImageRep" of current application
property FILE_PATH : POSIX path of ((path to desktop as string) & "myimage")
-- IBActions ( button clicks )
on insertImage_(sender)
set newImage to imageView's image
set myNsDataTifData to NSData's alloc()'s initWithData_(newImage's TIFFRepresentation)
set myNsBitmapImageRepObj to NSBitmapImageRep's alloc()'s initWithData_(myNsDataTifData)
newImage's addRepresentation_(myNsBitmapImageRepObj)
-- This works
set myNewImageData to NSData's alloc()'s initWithData_(myNsBitmapImageRepObj's TIFFRepresentation)
--This does not
--set myNewImageData to NSData's alloc()'s initWithData_(myNsBitmapImageRepObj's representationUsingType_properties_(NSPNGFileType, {}))
if not myNewImageData's writeToFile_atomically_(FILE_PATH, true) then
set messageText to "There was an error writing to file"
display dialog messageText buttons {"Ok"}
end if
end insertImage_
on applicationWillFinishLaunching_(aNotification)
-- Insert code here to initialize your application before any files are opened
imageView's registerForDraggedTypes_({"public.png"})
end applicationWillFinishLaunching_
on applicationShouldTerminate_(sender)
return true
end applicationShouldTerminate_
end script