In many cases, academic institutions will be legally obliged to fill positions according to the procedure you described. This usually requires publishing a call for applications in a suitable medium, and putting together a short list of selected candidates based on qualification for the position and fit to the announcement.
Now there may be the "problem" that the person or group that will select the candidate already has a specific candidate in mind and want to "assign" the position to that candidate. However, simply hiring that candidate without going through the formal process would in fact be illegal. So the typical workaround in this case is to make the announcement so specific that the desired candidate will be the best qualified person for the position. It may be ethically questionable, but I fear it is totally legal.
Still, even for other candidates, this approach seems to be better than allowing the institution to just fill the position without the formal process. Especially if there is a committee deciding on the short list, this may even be different from the group that made up the announcement, and there may be people pushing for candidates they deem more qualified than the originally desired candidate. Chances for other candidates will clearly be rather low, but still higher than zero as in the case without announcement.
As to the question whether it is possible to find out about such a situation in advance, I fear it is not that easy. It works best when one has good contacts to people in the committee, and can ask informally about the situation. Otherwise, a reasonable approach would be to phone a contact given in the announcement and discuss details of the position. In many cases, one should be able to figure out whether the other person is open to applications or not.