applescript-objccolor-profilenscolorspace

Using ApplescriptObjC to convert color spaces of an image using NSColorSpace and iccProfileData


I have some code that will change an images colorspace from RGB to a GenericCMYK profile. I would like to be able to use an ICC Profile to convert the image to a CMYK colorspace. There is a way to do this in Photoshop, but the process takes too much of a users time when you are dealing with 100's of images, I am trying to create an AppleScript droplet that will do this for them.

I have already looked at the page for NSColorspace and there looks like there is a way to do this. I just have no idea how to convert this Objective-C into ApplescriptObjC. Here are the two references to the ICC Profiles in NSColorSpace: init?(iccProfileData: Data) Initializes and returns an NSColorSpace object given an ICC profile. var iccProfileData: Data? The ICC profile data from which the receiver was created.

Here is the code I have thanks to Shane at Macscripter.net:

set theImage to (current application's NSImage's alloc()'s initWithContentsOfURL:theInput)
    set imageRep to (theImage's representations()'s objectAtIndex:0)
    set targetSpace to current application's NSColorSpace's genericCMYKColorSpace()
    set bitmapRep to (imageRep's bitmapImageRepByConvertingToColorSpace:targetSpace renderingIntent:(current application's NSColorRenderingIntentPerceptual))
    set theProps to (current application's NSDictionary's dictionaryWithObjects:{1.0, true} forKeys:{current application's NSImageCompressionFactor, current application's NSImageProgressive})
    set jpegData to (bitmapRep's representationUsingType:(current application's NSJPEGFileType) |properties|:theProps)
    set colorSpace to bitmapRep's colorSpaceName() as text
    (jpegData's writeToURL:theOutput atomically:true)

I just need to figure out how to include the ICC Profile as a part of the CMYK conversion process that happens I believe on this line of code:

set targetSpace to current application's NSColorSpace's genericCMYKColorSpace()

Can anyone give me some guidance on this?

Thanks!


Solution

  • You are looking at the NSColorSpace documentation in Swift. AppleScriptObjC is an Objective-C bridge, so it makes more sense viewing the documentation in Objective-C (the language can be set in the page header), where your snippet becomes

    set targetSpace to current application's NSColorSpace's alloc's initWithICCProfileData:iccData
    

    where iccData is the ICC profile data you are going to use.