Saving an Image to a PNG file

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

Some enums represent strings and some represent numbers. Rather than look them up, you can just prepend them with "current application’s ", because they’re all automagically added as properties at the application level. That will fix the NSPNGFileType problem. It’s also not going to like an empty list for a dictionary; better to supply missing value.

You’re doing more work than you need using NSData – you already have NSData returned from the relevant methods, so there’s no need to make new NSData objects. And I’m not sure why you’re adding the myNsBitmapImageRepObj back to the image, unless you intend to use it again later.

I also get an error with your test to see if the file was written – the result needs to be coerced to a boolean.

So you could write it like this:

	on insertImage_(sender)
		set newImage to imageView's image
		set myNsDataTifData to newImage's TIFFRepresentation()
		set myNsBitmapImageRepObj to NSBitmapImageRep's imageRepWithData_(myNsDataTifData)
		set myNewImageData to (myNsBitmapImageRepObj's representationUsingType_properties_(current application's NSPNGFileType, missing value))
		
		if not (myNewImageData's writeToFile_atomically_(FILE_PATH, true)) as boolean then
			set messageText to "There was an error writing to file"
			display dialog messageText buttons {"Ok"}
		end if
		
	end insertImage_

Thank you that is exactly what I needed and it works now. I should have known I was overly complicating it.