This is my first AppleScript Studio app so please be gentle.
part of my script checks to see what applications are running. If there are applications running that shouldn’t be then I want an error window to appear. If the check is OK I want a different window to appear. The problem I’ve got is BOTH windows appear no matter what. I tink I’m heading in the right direction but can some help??
Here’s my code:
on clicked theObject
set goodProcs to {"Finder", "Disc Eraser"}
tell application "System Events" to set theProcs to name of every process whose background only is false
repeat with myProc in theProcs
if myProc is not in goodProcs then
-- Display Warning Window
show window "WARNING"
else
show window "Erasing Disc"
end if
end repeat
end clicked
The problem is that you’re displaying the window inside the repeat loop, which will make the window pop up every time it evaluates myProcs. In fact, if you put a log statement into your original code right before you show each window, you’ll see that it’s actually ‘showing’ each window MANY times, not just once or twice. You need to evaluate the existence of the apps first, and then act only once accordingly.
If you only need to test for the two apps, then something as simple as this will work…
tell application "System Events" to set theProcs to name of every process whose background only is false
if (("Finder" is in theProcs) and ("Disc Eraser" is in theProcs)) then
show window "Erasing Disc"
else
show window "WARNING"
end if
Because you’ve used a list of apps, rather than hardcoding only the two, you may possibly be leaving some scalability in your code… but this also adds another element that you must account for. You’ll need to create a method of finding out if all of the apps were found. One way to handle this scenario is using a simple counter that increments every time an app is found and then comparing the count of the found apps to that of the original list.
set goodProcs to {"Finder", "Disc Eraser"}
set goodCount to (count goodProcs)
tell application "System Events" to set theProcs to name of every process whose background only is false
set foundCount to 0
repeat with myProc in theProcs
if myProc is in goodProcs then
set foundCount to (foundCount + 1)
end if
end repeat
if foundCount = goodCount then
show window "Erasing Disc"
else
show window "WARNING"
end if
Thanks for the reply but I’m get some weird results.
If I run your scripts from Script Editor (Changing the “Show Window” to Display Dialog") I get the correct results.
But if I run I compile the scripts and run them from my Application the first window mentioned always opens no matter what.