I have found this in the documentation.
https://developer.apple.com/documentation/safariservices/sfsafaritoolbaritem
which lead me to be able to use the window variable in the toolbarItemClicked function to get a reference to the toolbar item like so.
var toolbaritemretrieved = false
window.getToolbarItem { (item) in
toolbaritem = item! as SFSafariToolbarItem;
toolbaritemretrieved = true;
}
once I had a reference to the toolbar item I could then run this code and supposedly change the image of the icon.
let path = Bundle.main.path(forResource: "newIcon", ofType: "png")!;
let icon = NSImage(path);
while(!toolbaritemretrieved){
//wait for toolbar item to be retrieved
}
toolbaritem?.setImage(icon);
SFSafariApplication.setToolbarItemsNeedUpdate();
However this doesn't work. There are no visible errors but for some reason the icon does not display the new image.