The last one crashed on invisible layers, and locked layers. This fixes those problems.
As well, i've recorded the properties of each item. You might want to use some properties, such as filled = true, to selectively process which items are greyscaled.
tell application "Adobe Illustrator"
activate
tell current document
set theCount to (count of layers) as text
set theProperties to {}
set theProps to {}
repeat with EachLayer from 1 to theCount
if EachLayer > 1 and EachLayer < theCount then
if visible of layer EachLayer = true then # if invisible layer existed, script would have crashed.
set locked of layer EachLayer to false # if you don't want to process locked layers, you might want an if statement here instead, such as...
# if not (locked of layer EachLayer) then
set theProperties to page items of layer EachLayer
repeat with EachItem from 1 to count of theProperties
set selection to item EachItem of page items of layer EachLayer
set end of theProps to properties of selection
set end of theProps to return & return & return
tell application "System Events" to tell process "Illustrator"
try
click menu item "Convert to Grayscale" of menu 1 of menu item "Edit Colors" of menu 1 of menu bar item "Edit" of menu bar 1
end try
end tell
end repeat
set theProperties to text frames of layer EachLayer
repeat with EachItem from 1 to count of theProperties
set selection to text range of item EachItem of text frames of layer EachLayer
set end of theProps to properties of item EachItem of text frames of layer EachLayer
set end of theProps to return & return & return
tell application "System Events" to tell process "Illustrator"
try
click menu item "Convert to Grayscale" of menu 1 of menu item "Edit Colors" of menu 1 of menu bar item "Edit" of menu bar 1
end try
end tell
end repeat
# end if # if not locked
end if
end if
end repeat
end tell
end tell
theProps