Tuesday, November 19, 2019

#1 2012-11-28 06:32:01 am

erwinvb
Member
Registered: 2009-05-15
Posts: 17

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


Filed under: Illustrator, cs5, grayscale

Offline

 

#2 2019-08-07 02:27:52 pm

KniazidisR
Member
Registered: 2019-03-03
Posts: 712

Re: Convert Illustrator CS5 document to grayscale

Applescript:


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

Last edited by KniazidisR (2019-08-07 02:33:05 pm)


Model: MacBook Pro
macOS Mojave -- version 10.14.4
Safari -- version 12.1
Firefox -- version 70.0

Offline

 

#3 2019-11-09 03:11:19 pm

chrisndeca
Member
From:: Oakland, CA
Registered: 2005-06-28
Posts: 23

Re: Convert Illustrator CS5 document to grayscale

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!

Offline

 

#4 2019-11-09 06:17:52 pm

KniazidisR
Member
Registered: 2019-03-03
Posts: 712

Re: Convert Illustrator CS5 document to grayscale

chrisndeca wrote:


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


Thanks - to Takaaki Naganoya.

Try to replace in code line

Applescript:

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

"png" with "tiff"

, and, in code line 

Applescript:

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

replace NSPNGFileType with NSTIFFFileType or with NSBitmapImageFileTypeTIFF.

Last edited by KniazidisR (2019-11-09 06:29:36 pm)


Model: MacBook Pro
macOS Mojave -- version 10.14.4
Safari -- version 12.1
Firefox -- version 70.0

Offline

 

#5 2019-11-13 03:58:58 pm

chrisndeca
Member
From:: Oakland, CA
Registered: 2005-06-28
Posts: 23

Re: Convert Illustrator CS5 document to grayscale

Thanks so much!

Offline

 

#6 2019-11-13 06:51:48 pm

Marc Anthony
Member
From:: Dallas, TX
Registered: 2006-04-27
Posts: 907

Re: Convert Illustrator CS5 document to grayscale

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:

Applescript:

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

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)