Any OpenType code for Automatic alternate loop?

Hi there !
I’m back on a new font project ! :star_struck: and I’m glad to be back !

I’m dealing with a Opentype function question I can’t solve by myself.
In the font I’m working on, every glyph (A) would have an alternate (A.cv01)
My wish is that in every word using the font, if a letter appears twice, the second one would be the replaced by the corresponding alternate. If it appears 3 times, then the third one would be the normal glyph, fourth would be alternate, and so on…

So my question is : is there any script or code that could do it automatically for every glyph without having to write every glyph substitution by hand ?

This should help: https://glyphsapp.com/learn/features-part-3-advanced-contextual-alternates

Really interesting, indeed !
thanx @GeorgSeifert

There was also a script that created a calt function based on all ss0x included. Not sure it works in Glyphs 3 tho

I managed to code it thanks to the Georg Tutorial, thank you @elena !