I am having a very weird problem with the tell application command. Admittedly, I am fairly new to applescript and constantly need to look up references and such, but google is being an absolute monster and not helping me. So here is my problem…
I am writing a script that will basically control and poll iTunes for information when run from osascript. The thing is, I ONLY want it to tell app “iTunes” when iTunes is already running, and perform some other action if it isn’t.
However for some reason, even though the “tell application” block is inside an if block, it executes regardless of the if’s condition. At first i thought maybe it was iTunes bug, so i tried it on iPhoto and it did the same thing. So then i considered that maybe tell blocks are always executed regardless of conditionals, despite all the snippets of applescript I have seen everywhere. To test this, i changed the app yet again to Terminal and lo and behold it started behaving correctly. When i changed the app to terminal, it closed the front window if it Terminal was running and returned false if it wasn’t.
It completely stumps me, no amount of fiddling has gotten itunes to STAY closed if it is already closed (since ‘tell application “name” to do something’ always opens the application first, it seems).
Here is the snippets I’ve been working with:
tell application "System Events"
set myList to (name of every process)
end tell
set shouldRun to (myList contains "iTunes")
-- shouldRun correctly represents whether or not iTunes is active as illustrated by returning it with itunes closed and open:
--return shouldRun
if shouldRun then
tell application "iTunes" to play
else
return "closed"
end if
If itunes is open, then it starts playing. If itunes is closed, then it opens and then starts playing. iPhoto behaves similarly, but Terminal obeys the if statement and does not listen to the tell statement if shouldRun is false.
Anyone have any hints?
by the way, i couldn’t find any “if running tell application ‘blah’ to …” equivalent … and i know the way I’ve done it still isn’t the best, but i’ve rearranged it so many times to see if it would make a difference to the itunes segment (originally the if statement wasn’t a variable, it was ‘if (myList containts "iTunes) is true’
If there are any other ways to check itunes is running, feel free to suggest, though that is not the problem i am having. For the life of me, i cannot get itunes to keep its open/quitted state.
thanks,
Bo
Model: iMac Core Duo 20"
AppleScript: 1.10.7
Browser: Safari 419.3
Operating System: Mac OS X (10.4)