The Spanish held Oran until 1796 when they gave it away to the Ottomans for some reason. I could see Spain having a window to conquer Morocco during the War of Spanish Succession with the help of France, but Spain would have to share the Mahgreb in that case with France.
The Spanish certainly tried to take North Africa in the 16th Century and took the Barbary Coast as far as Tunis. But Barbarossa, the Ottoman Admiral was able to drive them from North Africa in the middle of the 16th Century. And that was when Spain was at it's height.
I suspect that the 16th Century experience soured the Spanish on attempting to rule large numbers of Muslim subjects. Even if they shipped them off to the New World as slaves, Muslims, particularly educated Muslims seemed to be behind every slave revolt in Spanish America, so the Spanish and Portuguese finally banned the sale of Muslims as slaves in the Americas. And why would Spanish seeking their fortunes immigrate to Morocco or Algeria when the whole New World was open to them. So the Spanish lost interest in North Africa until the 19th Century, when greater powers, particularly Great Britain and France refused to allow Spain to have Morocco (the French already had Algeria).