Now regarding the other --I will re–post a former post of mine and add a little.
All sorts of thoughts can happen to us out of the blue…*
the fact that a though happened to one- does not mean per se there was any sin.
And for mortal sin one needs not only grave matter, but full knowledge and deliberate (complete) consent…
And some need some medical/professional assistance in this area…such as medication for OCD etc and often a regular confessor --they are in an even more “particular boat” - with their suffering of involuntary thoughts.
For the next few moments --try real hard NOT to think of Apple.
then scroll down.
What did you think of?
Even though your will was against it -you did not want to think of an apple.
Now back to unwanted thoughts…
The best thing to do with such thoughts is not to fight them directly --not fear that they will come…do not consent and simply and calmly turn to something else…(or it may be best to keep doing the good thing one is doing…like they come out of the blue when your at work …keep working …etc)
Ignore such unwanted thoughts like one would ignore a hissing goose or a barking dog. One does not stop to argue with a hissing goose or a barking dog does one? No one keeps on walking…and at most laughs at them…
(as noted above the image there comes from a Carthusian Monk from centuries ago…)