City status in the United Kingdom is granted by the British monarch. It is not, and never has been, something which can be claimed as a right by towns which meet certain conditions, though historically in England and Wales (not in Scotland) there was a tradition that city status was given to towns with diocesan cathedrals. The granting of city status gives no special rights other than allowing the settlement in question to call itself a "city": it is nonetheless seen as highly prestigious.
While the current position is very clear cut, things start to become extremely muddy if you look at the history of city status in Scotland. Until the 1800s, the most important settlements in Scotland were the Royal Burghs. Between the early 1100s and 1700, when William III made Campbeltown a Royal Burgh, not far short of 100 of them were created across Scotland.
City status was not formally granted to any Scottish settlements before the 1800s, but by then a number were regularly referring to themselves as cities, most notably Glasgow, Edinburgh, Aberdeen, Perth and Elgin. Dunfermline decided to refer to itself as a city in 1856 in recognition of its historical role as a royal capital, but the idea never really caught on at the time. Meanwhile, Brechin has a football club whose name refers to it as a city: this is a term said to date back to 970 and may refer to the town's possible role as a Pictish capital.
In 1889, Dundee was granted city status by letters patent and so became, from some points of view, Scotland's first officially recognised city. Two years later, in 1891, Aberdeen had its city status confirmed when the burgh was enlarged by an Act of Parliament. The Royal Burgh of Inverness had its application to be granted city status as part of Queen Victoria's 1897 Diamond Jubilee celebrations rejected: by some accounts because it was thought it would draw attention to the lack of any formal city status held by Glasgow and Edinburgh.
The situation in Scotland was not fully clarified until the passing of the Local Government (Scotland) Act 1929, which designated Glasgow, Edinburgh, Aberdeen, and Dundee as "counties of cities". The strong implication of this was that there were no other cities in Scotland and this was confirmed when Local Government (Scotland) Act 1947 explicitly listed Dunfermline and Perth as "large burghs" and Elgin as a "small burgh".
Despite this, the then Home Secretary, James Callaghan, talked about there being six cities in Scotland in 1969, though he did not name them; and Glasgow, Edinburgh, Aberdeen, Dundee, Perth, and Elgin were listed as burghs that were also cities in the 1972 "Municipal Year Book". This all served to inject more muddy water into the picture, but the implication has to be that, whatever the prior position, from 1929 Scotland had just four cities: Glasgow, Edinburgh, Aberdeen and Dundee.
Between 1905 and 1994 fourteen UK towns were officially designated as cities, though none of them were in Scotland. Inverness became Scotland's fifth city in 2000 when it was one of three new UK cities granted their status to mark the Millennium. Two years later, in 2002, Stirling became Scotland's sixth city when it was one of five created across the UK to mark the 50th Anniversary of the Queen's accession to the throne. In 2012 Perth became Scotland's seventh city when it was one of three created across the UK to mark the Queen's Diamond Jubilee. And in 2022 Dunfermline became Scotland's eighth and latest city when it was one of eight created across the UK and overseas territories to mark the Queen's Platinum Jubilee.