Convert Illustrator CS5 document to grayscale

Can anyone tell me how to script to convert an open document to grayscale.
By hand it is realy easy but I can’t seem to find any information how to script this.

TIA


-- Created 2019-07-09 by Takaaki Naganoya
-- 2019 Piyomaru Software
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"
use framework "AppKit"

property NSUUID : a reference to current application's NSUUID
property NSString : a reference to current application's NSString
property NSImage : a reference to current application's NSImage
property NSColorSpace : a reference to current application's NSColorSpace
property NSPNGFileType : a reference to current application's NSPNGFileType
property NSBitmapImageRep : a reference to current application's NSBitmapImageRep
property NSColorRenderingIntentPerceptual : a reference to current application's NSColorRenderingIntentPerceptual

set aFile to POSIX path of (choose file of type {"public.image"} with prompt "Select Image A")
set aImage to NSImage's alloc()'s initWithContentsOfFile:aFile

set fRes to retUUIDfilePath(aFile, "png") of me
set sRes to saveNSImageAtPathAsGreyPNG(aImage, fRes) of me

-- Create and return the file path with the file name UUID and the specified extension based on the specified file path.
on retUUIDfilePath(aPath, aEXT)
	set aUUIDstr to (NSUUID's UUID()'s UUIDString()) as string
	set aPath to ((NSString's stringWithString:aPath)'s stringByDeletingLastPathComponent()'s stringByAppendingPathComponent:aUUIDstr)'s stringByAppendingPathExtension:aEXT
	return aPath
end retUUIDfilePath

--NSImage Save as PNG in the specified path
on saveNSImageAtPathAsGreyPNG(anImage, outPath)
	set imageRep to anImage's TIFFRepresentation()
	set aRawimg to NSBitmapImageRep's imageRepWithData:imageRep
	
	-- Convert the Image to Grayscale
	set bRawimg to convBitMapToDeviceGrey(aRawimg) of me
	
	set pathString to NSString's stringWithString:outPath
	set newPath to pathString's stringByExpandingTildeInPath()
	
	set myNewImageData to (bRawimg's representationUsingType:(NSPNGFileType) |properties|:(missing value))
	set aRes to (myNewImageData's writeToFile:newPath atomically:true) as boolean
	return aRes --true/false
end saveNSImageAtPathAsGreyPNG

--NSBitmapImageRep Convert to Grayscale
on convBitMapToDeviceGrey(aBitmap)
	set aSpace to NSColorSpace's deviceGrayColorSpace()
	set bRawimg to aBitmap's bitmapImageRepByConvertingToColorSpace:aSpace renderingIntent:(NSColorRenderingIntentPerceptual)
	return bRawimg
end convBitMapToDeviceGrey

Excellent Script!

Any chance you can tell me what bits needs to be changed for the output format to be TIFF instead of PNG?

Thanks!

Thanks - to Takaaki Naganoya.

Try to replace in code line

set fRes to retUUIDfilePath(aFile, "png") of me 

“png” with “tiff”

, and, in code line

set myNewImageData to (bRawimg's representationUsingType:(NSPNGFileType) |properties|:(missing value))

replace NSPNGFileType with NSTIFFFileType or with NSBitmapImageFileTypeTIFF.

Thanks so much!

While useful for creating composites, Takaaki’s script doesn’t answer the OP’s now years-old question, which is how to convert colors in situ in Illustrator. Actions can be called with AppleScript, including the ‘convert to grayscale’ menu item. This works for paths:

tell application "Adobe Illustrator"'s document 1
	set page items's selected to 1 --assumes unlocked
	do script "makeGray" from "Default Actions" dialogs 0 --assumes Action exists at location
end tell