The answer is yes, it is a sin.
As David said, there are three elements to every act: the act itself, knowledge of the act, and will to perform the act.
All actions have a moral value: good or evil. Evil actions offend God, and are therefore sinful. The degree to which they are sinful varies by degree of knowledge of the act and the will to perform it.
All acts, considered independently of intention, are morally neutral. However, when intention is brought into the picture, no act is morally neutral. Consider your example, you’re asking about violating the Second Commandment. If a parrot said the phrase, “Oh my God,” it is a morally neutral act. This is because parrots don’t have intention. They have instinct.
If a saint, praying to God, said the phrase, “Oh my God,” this act is meant to be one of honor, or praise. It is thus good. If Satanist said, in a mocking tone, perhaps mocking the aforementioned Saint, “Oh my God” the act would be evil, as the intent is mockery, or disdain.
The murky question that you’ve asked is one where the speaker’s intention is unclear. As David pointed out, if there is no knowledge, there is no sin, or if there’s no ill will (or the will is being forced) there is no sin. However, it’s not exactly as simple as that.
If, let’s say, we were talking about a Native American, when settlers first came to America, and neither understood the others’ language, and a Native heard the name of Jesus Christ, and then said His name without any knowledge of what he was saying, then an evil act has occurred, but sin cannot be imputed to him because he literally had zero knowledge (and even perhaps a negative knowledge as he might have seen the settlers as a threat).
You may wonder why I call it an evil act. It is so because it is still a word spoken in vain. When that Native dies and goes before God and the full truth of Jesus is revealed, it is likely that he will recall His use of our Lord’s name and feel a sorrow at his own ignorant blasphemy. Maybe not guilt, but sorrow at least.
But that’s not who we’re talking about. We’re talking about an actor on TV, who would have had to have lead an extraordinarily sheltered life to have never heard about Jesus, or God for that matter. He may have no belief, but the Church teaches that God’s moral law is written in our hearts. Ultimately, the actor knows deep down that it is wrong, but has perhaps conditioned his/her conscience to ignore it. It is a sin, though perhaps little guilt is imputed to the actor, depending on his/her knowledge.
If we’re talking about a Catholic actor, then that’s a different story entirely, and assuming he/she’s been properly catechised, it is a mortal sin. Being “forced” to read the script doesn’t mitigate that. You can refuse. Maybe lose the part, but would you consider a person to be a worse sinner if the part called for a sex scene? You shouldn’t, because the Commandments are given in order of importance. Taking God’s name in vain is a worse sin than adultery (and other related sins), given full knowledge and will, of course.
A related question you should be asking is, does the viewer of the movie/show also sin if they watch the movie/show knowing in advance of the profanity? The answer to that question is also yes.