An Ayr man threw food at his former partner and threatened her in the street before hurling racist abuse at police who were called to deal with the incident.
Paul Thompson pleaded guilty to four charges when he appeared at the town's sheriff court.
The charges stated that on January 12 this year, the 40-year-old assaulted his former partner in Alloway Street and threw a quantity of food at her.
He also shouted and uttered offensive remarks at her, and damaged the window of a nearby takeaway business.
Thompson, of Princes Court, then shouted, swore and uttered offensive remarks at police officers who attended, calling them "f****** c****" and "black b*******".
The court was told he had been in a relationship with the woman for 22 years, which had ended six months before the incident.
Thompson's solicitor said his client had thought there might be a chance of the relationship rekindling, and that the incident happened after he discovered the woman was seeing someone else.
The sheriff told Thompson that he had come very close to custody, and told him to change his behaviour.
He was put on a community payback order with two years of social work supervision and ordered to carry out 180 hours of unpaid work.
As part of the payback order he will also have to attend the Caledonian Men's Programme, which aims to help men address behaviours that lead to domestic abuse offending.
Comments: Our rules
We want our comments to be a lively and valuable part of our community - a place where readers can debate and engage with the most important local issues. The ability to comment on our stories is a privilege, not a right, however, and that privilege may be withdrawn if it is abused or misused.
Please report any comments that break our rules.
Read the rules here