Film star James McAvoy would play Jimmy Savile if Trainspotting author Irvine Welsh ever penned a script about the sex predator, the Scottish novelist said in an interview.

The pair worked together on the new film Filth, which was released last week and also stars Jim Broadbent and Jamie Bell. It is based on Welsh's "unfilmable" third book.

Welsh told the Radio Times that while making Filth, he discussed the subject of sexual abuse with X-Men star McAvoy, who apparently told him: "If you ever write a script about it, I'd love to play Jimmy Savile."

In Welsh's 1996 collection Ecstasy, one of the stories, Lorraine Goes to Livingston, concerns Freddy Royle, a fictional children's TV presenter described as the nation's "favourite caring, laconic uncle", who turns out to be a child molester and necrophiliac, raising millions for the hospital where he commits his crimes.

The story was written more than 15 years before Savile's crimes became public.

Asked whether the ­similarities were based on inside knowledge, Welsh told the magazine: "I had nothing to do with the ­hospital services, or NHS trusts, or the BBC.

"So how come I knew this rumour about Jimmy Savile, this eccentric British institution? There must have been so much stuff on the grapevine. But there was a whole culture then of not addressing these issues."

Welsh said that he was groped by middle-aged women when, as a student in the late 70s, he worked in a bingo hall.

He said he believed such abusive behaviour was "all about power".