Problem with a simple script

I’m new to this (an hour or so). I started off with a few simple iTunes scripts which went without problem. I’ve moved on to quicktime, because the motivation for learning this in the first place is a script having to do with quicktime.

This is my script:



tell application "QuickTime Player"
	if document is playing then
		pause document
	end if
end tell


This returns an error that it can’t get playing of the document. I also tried running JUST



tell application "QuickTime Player"
		pause document
end tell


Which simply did nothing at all. I feel like I’m misunderstanding how AppleScript interacts with Quicktime.

Hi,

you have to specify the document you want to control e.g.

tell application "QuickTime Player"
	if playing of front document then
		pause front document
	end if
end tell

or with a tell block

tell application "QuickTime Player"
	tell document 1
		if playing then pause
	end tell
end tell

Note: if playing is the same as if playing is true

would there be a way to, say, loop through all the documents and perform the check?

somrthing like this

tell application "QuickTime Player"
	repeat with oneDoc in documents
		tell oneDoc
			if playing then
				pause
				-- do something else
			end if
		end tell
	end repeat
end tell

Thanks, that’s exactly what I need.

edit Is there a break command in AppleScript? (for exiting a loop)

exit repeat

Okay, this is my script. It does what it’s supposed to when it runs, but I’d like to save it as an application that will continue checking over and over again for the conditions. Possible?



tell application "QuickTime Player"
	set mytun to true
	repeat with oneDoc in documents
		tell oneDoc
			if playing then
				set mytun to false
				exit repeat
			end if
		end tell
	end repeat
	if mytun is true then
		tell application "iTunes" to play
	else
		tell application "iTunes" to pause
	end if
end tell


You can do this with an idle handler.
Then you must save the script as application with option stay open.

To check if at least one movie is currently playing can be done easier
the number in the return line specifies the recurrence of the script in seconds

on idle
	tell application "QuickTime Player" to (get playing of documents) contains true
	if result then
		tell application "iTunes" to pause
	else
		tell application "iTunes" to play
	end if
	return 10
end idle

Oddly enough, much as I would prefer the short script the longer one seems neccesary to my purposes. Thanks, though.

Is there a similar script to your short one to check if any Quicktime windows are OPEN, rather than playing?

tell application "QuickTime Player" to (count windows) > 0

result is true, if at least one window is currently open

I implemented that, and it now crashes with error “can’t make false into type number”

Fixed that…

Is there any way to implement a global variable that is maintained each cycle?

the line

tell application "QuickTime Player" to (count windows) > 0

results a boolean value, not a list, so delete contains true

a “shortcut” for

set qtinfluence to false
       tell application "QuickTime Player" to (count windows) > 0 contains true
       if result then
           set qtinfluence to true
       end if

is

tell application "QuickTime Player" to set qtinfluence to (count windows) > 0

and the short version of your script:

on idle
    tell application "QuickTime Player"
        if (count windows) > 0 and (get playing of documents) contains true then
                tell application "iTunes" to pause
            else
                tell application "iTunes" to play
            end if
        return 0.5
    end tell
end idle

Note: Consider the consumption of resources while setting the recurrence interval too short

Excellent, thanks once more for your continued assistance.

The script works perfectly like this:



on idle
	tell application "QuickTime Player" to set qtinfluence to (count windows) > 0
	if qtinfluence is true then
		tell application "QuickTime Player" to (get playing of documents) contains true
		if result then
			tell application "iTunes" to pause
		else
			tell application "iTunes" to play
		end if
	end if
	return 0.5
end idle


When quicktime is open pausing it ahs the inverse effect on iTunes.

I’d like to add it so that when Quicktime closes all windows, it starts playing again ONCE (rather than each cycle of the program). My idea would be to use a global variable, but I can’t find any information on this for AppleScirpt

you can define a property to keep the value

property myVariable : false -- or any other class 

on idle
.