I would highly recommend against @Jeanpaul's precast rule, as it won't allow preemptive shadow casting. If you don't care about distinguishing between Utsusemi: Ichi and Utsusemi: Ni, the following will do:
Code
function midcast(spell)
if spell.name == 'Utsusemi: Ichi' then
send_command('cancel Copy Image|Copy Image (2)')
end
end
Otherwise you will need a variable like he has, although personally I'd use a bool for it and call it something else:
Code
function midcast(spell)
if spell.name == 'Utsusemi: Ichi' and overwrite then
send_command('cancel Copy Image|Copy Image (2)')
end
end
function aftercast(spell)
if not spell.interrupted then
if spell.name == 'Utsusemi: Ichi' then
overwrite = false
elseif spell.name == 'Utsusemi: Ni' then
overwrite = true
end
end
end
However, this is not perfect either. If, for some reason, Utsusemi: Ichi has no effect, spell.interrupted will still be false, and overwrite will be set to true. In that case it will not cancel the shadows on the next cast, despite Utsusemi: Ni being up. (The same is true with @Jeanpaul's version, we only use slightly different semantics.)
Those are basically the two minimalistic versions, each one with their own drawback. Up to you to decide which one is more worth it to you. It would probably be possible to implement a perfect version, but it may require more effort (analyzing action message packets), unless GS exposes some other features I'm unaware of.
Two further general notes:
1. You don't need to check for buffactive before canceling, the cancel addon does that itself, so you can just send the cancel command and the addon won't send the packet if the buff isn't active.
2. Do not use the cancel plugin and addon both, choose one. I recommend the addon. In general, don't use the plugin and addon version of the same script, it rarely leads to good results and more often than not brings issues with it. When you have a choice between the two, the addon is almost always the way to go. The only addon with less functionality than its plugin counterpart is TParty, although that may change soon as well.