PRESIDENT TRUMP, ever prone to seek out scapegoats, fastened on a new target in the wake of the terrorist attack in New York: the state's senior Democratic senator, along with a 27-year-old visa program that offers applicants from dozens of countries a shot at immigrating to the United States.

Mr. Trump singled out Sen. Charles E. Schumer, who, in 1990, sponsored the diversity visa program, through which the alleged attacker in New York, Sayfullo Saipov, is reported to have immigrated to the United States from his native Uzbekistan. In a tweet, the president derided the program as "a Chuck Schumer beauty."

Never mind that Mr. Schumer's legislation establishing the program attracted bipartisan support; or that it was signed into law by President George H.W. Bush, a Republican; or even that Mr. Schumer himself unsuccessfully bargained to end the program, in 2013, in return for a bill granting legal residence to millions of undocumented immigrants already in the United States. Neither the facts nor the normal political imperative to avoid partisanship in the wake of a terrorist attack appeared to move Mr. Trump.

His tweet made it appear that his overriding interest in an assault allegedly backed by the Islamic State is to use it to assail immigration — in this instance, a legal program whose beneficiaries represent a speck in the overall number of immigrants. Managed by the State Department since 1995, the program now grants up to 50,000 visas annually, via a random lottery, to citizens of dozens of countries who would otherwise be mostly overlooked in the annual influx of green-card recipients. In recent years, many of the winners have been from Africa and Eastern Europe.

Having reaped political advantage as a candidate in vilifying illegal immigrants, Mr. Trump has set his sights in office on legal migrants, including refugees, from a handful of mostly Muslim countries, whom he'd like Americans to see as an undifferentiated mass of potentially violent interlopers. Gradually, he is chipping away at what was once a national consensus that immigrants are a critical source of vitality, invention and international appeal.

Like almost any immigration program, the diversity visa lottery is imperfect and susceptible to abuse. The fortunate winners, who represent less than 1 percent of those who have applied annually in recent years, are not uniformly equipped to thrive in this country; many lack an education beyond high school. As Mr. Saipov may turn out to prove, even the extensive vetting required of all who immigrate through the program does not provide an ironclad guarantee that it is impervious to applicants who might seek to harm the United States.

The lottery program might be improved. Still, the fact that more than 11 million people applied for it in fiscal 2016 reflects the magnetic appeal the United States continues to exert around the world. Satisfying a small fraction of that demand, through the lottery or some other legal means, is a powerful tool of public diplomacy in countries whose citizens might otherwise have no hope of coming here.