So there were lots of wars involving Ottoman Empire's rivals in Europe but why did the Ottomans not seize the opportunity to increase their European holdings? For example, why didn't they attack Austria during the Thirty Years' War? Why didn't they attack Austria and Russia during Napoleonic Wars? There are probably more examples of this but I can't remember right now.
When Russian troops retreated further into Russia when Napoleon invaded, couldn't Ottomans easily reclaim their lost Black Sea provinces? When Austria was weakened during Thirty Years' War, why didn't they invade Vienna? There wouldn't be a resistence as fierce as 1683 or why they didn't attack Austria to reclaim their lost Hungarian provinces during Napoleonic Wars?
When the Ottomans were themselves stable enough to take advantage of the weaknesses of their neighbors, they did so.
For example, why didn't they attack Austria during the Thirty Years' War?
For the Ottomans, 1618-1648 was mostly a period of weak rulers with short reigns, whose power was challenged internally by the Janissaries. Of the four sultans within the period, two rose to power very young (14 and 9), one stepped down to avoid being deposed in a revolt, and the last was mad and essentially controlled by his doctor. Of the child sultans, the 14-year old one was executed in a Janissary coup, the 9-year old one was the only competent ruler of the period but he had to fight a 16-year long war with the Safavids after which he died of cirrhosis at 29.
Why didn't they attack Austria --- during Napoleonic Wars?
Austrians had won their last war against the Ottomans, and after it declared that they were not contemplating further territorial expansion at the expense of the Ottomans. This made the Austrian front pretty much the only safe frontier of the Ottoman empire, and they probably wanted to keep it that way.
Why didn't they attack --- Russia during Napoleonic Wars?
They did. They attacked Russia after receiving news of the Battle of Austerlitz. They had some initial successes, but eventually Russia signed the peace of Tilsit, leaving them free to concentrate their forces against the Ottomans. This meant that the Ottomans had to face Bagration, the Kamensky brothers, and Kutuzov. This did not go well for the Ottomans. In the end, they lost Bessarabia and had to grant the Serbians autonomy.
One thing /u/Tuna-Fish2 didn't mention was logistics. Or, more accurately, how logistics and the timariot system conspired to effectively hamstring the Ottomans.
From the mid 14th century until they fell out of favor in the late 16th century, the Ottoman army revolved primarily around the timariots. These were men who had a fief (usually a timar, hence the name, but sancaks and ziamets were included in the system as well [to over-simplify, they were just really big timars]) and who were typically exempted from taxes in return for military service. When they were called to war they'd typically bring retainers of their own (cebelu) on campaign. They were not entirely unlike traditional European knights.
The benefits of the timariot system are pretty clear: lots and lots of manpower. In 1609, for instance, the timariot army had a theoretical strength of about 120,000 men. This was in addition to large numbers of auxiliaries (Diyarbekir eyalet could field 7,200 Kurdish auxiliaries; western Anatolia field 26,000 Turkmen auxiliaries), standing military forces (the kapu kulu had a strength of 75,868 in 1609: 37,627 Janissaries (plus 9,406 cadets), 20,869 Sipahis and related cavalry forces, and 7,966 members of the artillery corps), and a whole mess of forces available to the governors of local eyalets (Toygun Pasha of Buda eyalet fielded 8,000 local soldiers to capture Filakovo in 1554... and that wasn't even a particularly noteworthy campaign). These figures don't take into account troops further afield in places like Egypt, which based solely on the number of sanjaks could probably field another 20,000 men.
By the standards of the time, the Ottomans could field an outrageously large army. They very rarely did so, however, because of logistics. The Ottomans kept extensive records of how long it took them to move troops around. In 1663, for instance, the Ottoman army under Fazil Ahmed Pasha that left Edirne and marched to Hungary for one of the many Austro-Turkish wars took 119 days to get to Esztergom, of which it spent 52 actually marching. Fazil Ahmed Pasha would ultimately oversee the Ottoman disaster at Saint Gotthard later that year. That particular defeat (which was largely due to freak weather events and the river flooding at the least opportune time possible) ultimately resulted in the Ottomans being much more conservative in campaigns in the Balkans. As late as 1697 the commanders were regularly restricting how far the vanguard could advance.
But back to logistics. When the Thirty Years' War broke out, the Ottomans were still rather dependent upon the timariots. To compound matters, most of the timariots were in Asia. Ottoman auxiliaries also came from Asia (western Anatolia, eastern Anatolia, northern Iraq). Somewhere in the vicinity of 2/3rds of these non-professional soldiers were in Asia. Erzurum eyalet, for instance, could field 7,800 timariots. Sivas could field 9,000. Both of these places are in eastern Anatolia. In addition to the ~120 days it would take troops to march from Edirne to Hungary, they'd probably take another two months just to get to Edirne. Troops mustered from as far afield as Zulkadriye (southern Anatolia, near the modern Turko-Syrian border) and Van (southeast Anatolia, near Armenia and Iraq) would take even longer. Rakka (Syria), Mosul, and Baghdad would obviously take even longer. Assuming an average daily progress of 10km, it would take 181 days to get from Cairo to Constantinople. Then you'd need another 119 days to get to Hungary. If it's going to take you 300 days to get troops to the front, you may as well not even bother mustering them.
Compounding all of this is the fact that the Ottoman campaign season was only from April until October. That 600 day round trip would essentially be a four year commitment from those troops you've mustered. That's a non-starter when you're talking about what is essentially still a levied military. You wouldn't see that kind of long-term commitment from European knights unless you were talking about a crusade.
The Ottoman Empire expanded rapidly and gobbled up huge tracts of land. It did an exceptional job of integrating ethnically and religiously diverse people into its country (at least until the 18th century or so). And yet it could never really hope to concentrate its forces where it needed them simply because of how big the empire was. It faced threats everywhere: the Safavids out east, myriad rebellious rulers on the frontier (Egypt was notorious for this), the Austrians in the Balkans, and the Russians on the Black Sea after they conquered the Crimean Khanate. To make matters worse, by the late 17th century the Ottoman army was lagging behind its European rivals.
Oh, and the Thirty Years' War lasted from 1618 until 1648. The Ottomans, however, were actively engaged in a string of wars with the Safavids lasting from 1623 until 1639. These campaigns included some of the largest shows of force the Ottomans would ever muster (including deploying at least 100,000 men in 1635 and again in 1638). Picking a fight with Austria while that was going on (especially since the sultan went east with the armies) would have been a terrible idea.