Only two lines of the dance-song are quoted, so the rest must have come from somewhere else (the trial transcripts, according to "Chantan"). It may have been a well-known thing at the time. According to Sands' Sketches of Tranent in the Olden Time (1881), James actually had poor Gellie (sic) Duncan play the tune for him at Holyrood, not that it did her much good.
It seems that Murray is misleading over the number of people tried, so best disregard my earlier remark on that point. It isn't relevant to the song or its tune anyway.