He accompanied the monarch to 65 countries between 1978 and 1987, acting as her spokesman as well as her buffer against the prying media. The job, which he described years later as “a quasi spin doctor,” also entailed speaking on behalf of all the royals, including the Prince and Princess of Wales.

In his later years, Shea was a prominent figure in Edinburgh, a Commander of the Royal Victorian Order (CVO), Deputy Lieutenant (DL) for the city, a former chairman of the Royal Lyceum Theatre, a director of the Edinburgh Military Tattoo and a member of the Edinburgh University Court. He was also a trustee of the National Galleries of Scotland, a visiting professor of personal and corporate communications at the University of Strathclyde’s Graduate Business School, and director of public affairs for the Hanson Group.

It was during his years at Buckingham Palace, however, that his face became familiar to the nation. He dealt with the press during the hectic times of Charles and Diana’s romance and later their wedding and the early years of their marriage. It was he who had to put a royal “spin” on the embarrassing story of the Irish intruder who got into the palace and sat at the foot of the Queen’s bed. It was he, too, who had to calm the rumpus over an alleged palace mole who was supposed to have told the Sunday Times that the Queen was at odds with her Prime Minister, Margaret Thatcher.

It turned out later that he himself may have been the unwitting mole, but he said the newspaper had completely misinterpreted a comment he had made. He codenamed such media stories TGTC – too good to check – in which a newspaper would run an unchecked story and follow it with a palace denial, but by then the damage would already have been done.

Michael Shea was born in Carluke, Lanarkshire, grandson of an Irish-American from Boston and a woman from Orkney. He went to Lenzie Academy, where his mother was a teacher, and Gordonstoun. Shea attended Edinburgh University, leaving with a PhD in economics with the thesis “economic development in West Africa”. When he was 13, he flirted with the SNP – “I was a child. It’s typical of most Scots. There’s an emotional gut-instinct reaction,” he said years later. He led Edinburgh University’s Liberals in the early 1960s, taking over from David Steel, but would later eschew politics as a diplomatic career beckoned.

“Poached” by the Foreign Office, he was posted first to Ghana, then Bonn, where he met a Norwegian diplomat who would become his wife, and later Bucharest, while Romanian dictator Nicolae Ceausescu was at the height of his powers. One of his predecessors at the British embassy in Bonn had been David Cornwell, better known as John le Carré, and this gave Shea the idea to write novels. Like Cornwell, he diplomatically used a nom de plume – Michael Sinclair. His first thriller, Sonntag (Sunday), published in 1971, was translated into eight languages and he would eventually write more than two dozen books.

The Dollar Covenant, which appeared in 1973, was set in Scotland under a fictional independent government with the country paralysed, the economy in ruins and a giant American telecommunications company, Bondi, trying to seize control. Shea said BBC TV had planned to do a version of the book but, with the SNP on the rise at the time, felt it would be too politically sensitive.

Once he had left the Foreign Office, Shea was able to republish the book under his own name, with the new title Endgame, in 2000. He also used it as the basis for a later book, State of a Nation, which appeared in 1997 and was serialized in The Herald. His other books include The British Ambassador, Spin Doctor, A View from the Sidelines, about his time in Buckingham Palace, and To Lie Abroad: A View of Diplomacy.

He remained highly active in retirement. From 1993 he was a non-executive director of Caledonian Newspaper Publishing, resigning in 1996 to avoid conflict of interest when he was appointed representative for Scotland on the Independent Television Commission (ITC), which would later become part of Ofcom.

He was a past chairman of Scotland in Europe, the public sector body charged with assisting Scottish companies find development partners in Europe. He often liked to say that Scots were their own worst enemies. “We don’t need enemies when we have each other,” he once said. “We have a whingeing mentality in which it is always someone else’s fault. We need to talk ourselves up as a nation.”

Shea spent his latter years in an office and flat in Edinburgh’s Ramsay Gardens, next to the castle, where he was surrounded by murals painted by his godfather, the artist John Duncan.

Shea is survived by his wife, Mona, and two children.