Alex Salmond has more than enough support in the Scottish parliament for a referendum on independence. He also has the support of David Cameron, who said this week: "If the Scottish parliament votes to have an independence referendum, that's a vote that we would have to respect and we would have to allow that and enable that to happen." And so there is ostensibly nothing stopping Salmond from having the referendum he wants. Except, that is, the UK supreme court.

The court will almost certainly have to decide whether the Scottish parliament has the power to legislate for an independence referendum – and will almost certainly rule that it does not. This is because the Scotland Act 1998, which conferred powers to Holyrood, contains a list of matters that are "reserved" for Westminster – one of which is the future of the union between England and Scotland.

In 2007, the late Sir Neil MacCormick – an eminent jurist and senior member of the SNP – argued that although a straightforward referendum on independence would be unlawful, a "consultative referendum" would be within Holyrood's power. Everything depends on the question, he argued: it would be lawful if the Scottish people are asked whether the Scottish executive should embark on discussions with the UK government with a view to securing independence.

Salmond knows his preferred referendum risks being struck down by the supreme court. That might explain why, over the last few weeks, he and his colleagues have been attacking the supreme court. His outbursts were prompted by two criminal cases, in which the supreme court overruled the appeal court in Edinburgh on human rights grounds. He complained – wrongly – that the supreme court was usurping its power. When that argument failed, he reverted to his familiar complaint about being ruled by London. He would prefer issues concerning convention rights to go straight to the European court of human rights, without passing through London – even though the supreme court has far more experience in Scottish law than Strasbourg, and always has two Scottish justices.

Earlier this week, Adam Tomkins, a law professor at Glasgow University, accused Salmond of trying to "delegitimise" the supreme court, ahead of the referendum issue coming before it. Salmond dismissed the accusation as "hooey", but his attacks will certainly assist any future argument – should he need to deploy it – that the justices in London are preventing the Scottish people from having their say.

Cameron may respect a vote on the issue in the Scottish parliament, but others in the government recognise the legal requirements. "You cannot unilaterally negotiate the terms of membership of the UK," said Scotland Office minister David Mundell. "You have to have the support of other parts of the UK through the UK parliament to do that."

And here is the underlying reason why the union is a reserved matter. It is something that affects everyone in the UK, not just the Scottish people. Of course, the Scottish nationalists will claim that a UK-wide referendum, in which Scottish voters are a minority, is just another way of denying the Scottish people their right to decide. But that is to accept the contemptible nationalist idea that there is a single "will of the people". A UK-wide referendum would not only allow Scottish people to decide; it would allow everyone else affected to decide, too.

Regardless of this, the Westminster government should pre-empt the looming battle between the SNP and the supreme court. Westminster will have to legislate for a referendum sooner or later. Cameron may wish to wait, but he should do it before the justices are drawn into the controversy.