Gordon Crozier, 44, was secretly followed from his Edinburgh home to a retail park in the shadow of Aintree Racecourse near Liverpool and then back to the Capital by several unmarked police cars.
An armed police unit then swooped on Crozier's Jaguar XJ6 and discovered a gun in the boot of the car, the High Court in Perth was told.
The court was told that Crozier, of St Katherine's Crescent, Edinburgh, was being watched as part of a major operation by drugs squad officers which had lasted for six months prior to his arrest.
The jury heard that Crozier's car was searched after his arrest and a pistol was found in a Tesco carrier bag, tucked under a compartment in the boot.
Det Con Fiona Harrison, 38, of Lothian & Borders Police said they followed Crozier to the Aintree Racecourse Retail Park and watched as he put a bag in the boot.
She said Crozier, whose partner was in the car with him, could be seen on his mobile phone and looking around as if he was trying to locate somebody.
Det Con Alan Young, 35, said: "The operation lasted a number of months. I saw the subject driving on the M6 and on the M57 towards Bootle.
"I saw them enter B & Q and walk towards the cafe area. He was using a mobile phone. It was perceived that he was arranging a meeting with the person he was talking to on the phone, and trying to clarify a time."
When Crozier left the retail park he drove off, turned around a short time later, and stopped at a pub where he spoke to the driver of a parked Ford Fiesta.
Inspector Darren Faulds, 38, described how he and other colleagues were called in to assist when Crozier's vehicle was stopped by a specialist firearms squad.
"We were asked to assist with the search of the vehicle. We opened the boot. We observed a plastic bag protruding below the battery holder in the boot.
"It was a white Tesco bag. I removed it from the boot area and clearly see a handgun within. It was a silver gun with a black handle. It had a magazine within it so I dropped it out for safety purposes."
Crozier denies that on various roads between Edinburgh and Liverpool he had a self-loading pistol without a firearms certificate.
He also denies that on 24 August 2008 he possessed a prohibited weapon without the authority of the Secretary of State or Scottish Ministers.
Crozier denies a third charge relating to the possession of ammunition – namely five centrefire cartridges – without having a firearms certificate.