Apple Event: May 7th at 7 am PT

Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

controlling quicktime with Applescript

Working with OS 10.6.3 and QTP 10 and FileMaker Pro 11

Hardly any experience with AppleScript; I got my knowledge from various forums.It may be a simple script, but at least it works!

I tried to make an AppleScript to play just one song in a Filemaker Pro environment; everything else is handled by FMP; it only needs to know whether to stop or move to the next song.
You can guess the meaning of the few Dutch words.

###############################################################
set url_path to cell "bestandspad" of current record

tell application "QuickTime Player"
activate
open URL url_path
play document 1
end tell

tell application "System Events"
set visible of process "QuickTime Player" to false
end tell

Set tekst to ""
set the clipboard to tekst

--repeat while song is playing
--repeat with loopvariable from startvalue to stopvalue --- definition in AppleScript Language Guide
--set loopvariable to 0
--repeat with loopvariable from 0 to duration of document 1
-- 'current time' and 'duration' are properties of document 1 according to library AppleScript Editor

repeat
display dialog "Interrupt" with title "" buttons {"Stop", "Pauze", "Next song"}
if button returned of result is "Stop" then
set tekst to "Stop"
exit repeat
else if button returned of result is "Next song" then
set tekst to "Next song"
exit repeat
else
tell application "QuickTime Player" to pause document 1
display dialog "Verder gaan ?" with title "" buttons {"Stop", "Hervat"} default button 2
if button returned of result is "Hervat" then
tell application "QuickTime Player" to play document 1
--resume doesn't work here, play does.
else
set tekst to "Stop"
exit repeat
end if
end if

--set loopvariable to current time of document 1
end repeat

tell application "QuickTime Player" to quit

set the clipboard to tekst
-- can this script return a value (to FMP) ?
###################################################################

The program works fine mostly, but for two things.

First of all, the line 'tell QTP to resume" doesn't respond; pause works fine, but the playing is not resumed. It does work however with 'to play', strange.
Secondly, I make use of the clipboard for transferring data between programs (in this case from the Applescript back to FMP), but it must be possible for the Applescript to return a value to FMP in a less childish way; I haven't found that way.

Is it possible to set the position of the dialog screens?

Lastly, I found the "repeat with"-command in the AppleScript Language Guide, but I can't figure out how it exactly works. I want the repeat loop to last while the song is being played.
According to the dictionary in the AppleScript editor the properties 'current time' and 'duration' must be available for each document.
Most probably I am messing up with adressing the properties; the concept is not very clear to me.
This script does not include 'repeat with', but I have indicated in the comment lines what I have tried out, be it without success.
I still wonder what'll happen if no button is touched during the song's playing. Does QTP give a signal, when the song's playing has ended? Can I use the property 'Playing' to that purpose?

Any other suggestions to make the script simpler and/or more efficient are also welcome.

iMac 3.06, Mac OS X (10.6.3)

Posted on May 24, 2010 5:01 AM

Reply
Question marked as Best reply

Posted on May 26, 2010 1:36 PM

Hello

If I understand correctly what you're trying to do, you may try something like the following script. It is presumed that a global field named 'exit status' is defined in FMP document by which the script's return value is passed.

As for the position of dialogue presented by 'display dialog' command, it is not contollable by command's parameter.

If you're using this script in FMP's 'Perform AppleScript' step and 'tell application "FileMaker Pro"' statement is redundant, please remove it.

A variant of this script has been tested with FMP 4.0v3, QT 5.0.2, AppleScript 1.8.3 under OS9.1 at hand.
Hope this may help,
H



--SCRIPT
main()
on main()
script o
property input : "bestandspad"
property output : "exit status"

property mss1 : "Interrupt"
property btt1 : {"Stop", "Pauze", "Next song"}
property mss2 : "Verder gaan ?"
property btt2 : {"Stop", "Hervat"}
-- (0) get file url from the FMP's field of current record
tell application "FileMaker Pro"
set url_path to cell input of current record
end tell

-- (1) check whether QTP is already running
tell application "System Events"
set qt_wasrunning to process "QuickTime Player" exists
end tell

-- (2) open the file and play it in QTP
tell application "QuickTime Player"
--set qt_wasrunning to running -- OSX 10.5 or later only
open URL url_path
play document 1
end tell

-- (3) hide QTP
tell application "System Events"
set visible of process "QuickTime Player" to false
end tell

-- (4) present primitive playback control via dialogues
set s to "" -- exit status
repeat
display dialog mss1 buttons btt1 giving up after (my secLeft())
set {b, fin} to {button returned, gave up} of result
if fin then exit repeat
if b = btt1's item 1 then -- Stop
set s to b
exit repeat
else if b = btt1's item 3 then -- Next song
set s to b
exit repeat
else -- Pauze
--tell application "QuickTime Player" to stop document 1
tell application "QuickTime Player" to pause document 1
display dialog mss2 buttons btt2 default button 2
set b to button returned of result
if b = btt2's item 2 then -- Hervat
tell application "QuickTime Player"
tell document 1
if duration = current time then exit repeat
play
end tell
end tell
else -- Stop
set s to b
exit repeat
end if
end if
end repeat

-- (5) close document 1 in QTP
tell application "QuickTime Player" to close document 1 saving no

-- (6) quit QTP iff QTP was launched by this script
if not qt_wasrunning then tell application "QuickTime Player" to quit

-- (7) set FMP's global field to exit status
tell application "FileMaker Pro"
set cell output to s
end tell
end script
tell o to run
end main
on secLeft()
tell application "QuickTime Player"
tell document 1
set d to (duration - current time) / time scale -- seconds left
end tell
end tell
set d to round d rounding up
if d < 1 then set d to 1 -- to guarantee d >= 1
return d
end secLeft
--END OF SCRIPT
6 replies
Question marked as Best reply

May 26, 2010 1:36 PM in response to apwscript

Hello

If I understand correctly what you're trying to do, you may try something like the following script. It is presumed that a global field named 'exit status' is defined in FMP document by which the script's return value is passed.

As for the position of dialogue presented by 'display dialog' command, it is not contollable by command's parameter.

If you're using this script in FMP's 'Perform AppleScript' step and 'tell application "FileMaker Pro"' statement is redundant, please remove it.

A variant of this script has been tested with FMP 4.0v3, QT 5.0.2, AppleScript 1.8.3 under OS9.1 at hand.
Hope this may help,
H



--SCRIPT
main()
on main()
script o
property input : "bestandspad"
property output : "exit status"

property mss1 : "Interrupt"
property btt1 : {"Stop", "Pauze", "Next song"}
property mss2 : "Verder gaan ?"
property btt2 : {"Stop", "Hervat"}
-- (0) get file url from the FMP's field of current record
tell application "FileMaker Pro"
set url_path to cell input of current record
end tell

-- (1) check whether QTP is already running
tell application "System Events"
set qt_wasrunning to process "QuickTime Player" exists
end tell

-- (2) open the file and play it in QTP
tell application "QuickTime Player"
--set qt_wasrunning to running -- OSX 10.5 or later only
open URL url_path
play document 1
end tell

-- (3) hide QTP
tell application "System Events"
set visible of process "QuickTime Player" to false
end tell

-- (4) present primitive playback control via dialogues
set s to "" -- exit status
repeat
display dialog mss1 buttons btt1 giving up after (my secLeft())
set {b, fin} to {button returned, gave up} of result
if fin then exit repeat
if b = btt1's item 1 then -- Stop
set s to b
exit repeat
else if b = btt1's item 3 then -- Next song
set s to b
exit repeat
else -- Pauze
--tell application "QuickTime Player" to stop document 1
tell application "QuickTime Player" to pause document 1
display dialog mss2 buttons btt2 default button 2
set b to button returned of result
if b = btt2's item 2 then -- Hervat
tell application "QuickTime Player"
tell document 1
if duration = current time then exit repeat
play
end tell
end tell
else -- Stop
set s to b
exit repeat
end if
end if
end repeat

-- (5) close document 1 in QTP
tell application "QuickTime Player" to close document 1 saving no

-- (6) quit QTP iff QTP was launched by this script
if not qt_wasrunning then tell application "QuickTime Player" to quit

-- (7) set FMP's global field to exit status
tell application "FileMaker Pro"
set cell output to s
end tell
end script
tell o to run
end main
on secLeft()
tell application "QuickTime Player"
tell document 1
set d to (duration - current time) / time scale -- seconds left
end tell
end tell
set d to round d rounding up
if d < 1 then set d to 1 -- to guarantee d >= 1
return d
end secLeft
--END OF SCRIPT

May 27, 2010 11:29 AM in response to apwscript

Hello,

I think you fully understood what I was aiming for in my little program.
Thank for spending so much time and effort on my problem. You even rewrote my program completely.

I implemented your program, and there is only one problem. The variable 'time scale' is not recognized and thus not accepted; I replaced it by the number 1000 (in iTunes time is given in thousandths of seconds) and when this did not work I left it out.

As this happens in the handler secLeft() which provides the delay for the first dialog the program did not work.

Setting the delay for the first dialog to 60 seconds made clear that the program in fact works, but not yet completely.
Pause and stop works, next song results in a stop.
Furthermore only the first song of a list is played, then the program halts.
I am working on it, and I keep you informed.
Thanks again for your contribution which I understand (mostly), but could not have designed myself.

May 28, 2010 12:11 AM in response to apwscript

Hello

Hmm. They have greatly changed the QTP's function and scripting interface between QuickTime 7 (QT7) and QuickTime X (QTX). Under OSX10.6, 'application "QuickTime Player"' refers to QuickTime Player of QTX and not that of QT7.

So I guess you'd have two options.

(1) Install QT7 from OSX 10.6 install disc as explained in the following article:

Installing QuickTime Player 7 on Mac OS X v10.6 Snow Leopard
http://support.apple.com/kb/HT3678?viewlocale=en_US

and change all occurences of "QuickTime Player" to "QuickTime Player 7" in the script.

(2) Or you may try a modified version of secLeft() handler listed below.
(I've just found an article in web saying that, in QTX Player, 'time scale' property has been removed and 'duration' and 'current time' now return value in second. If it's correct (sorry I cannot test it by myself), the following code should work.)


--SCRIPT 2 (excerpt)
(* for QuickTime Player of QuickTime X *)
on secLeft()
tell application "QuickTime Player"
tell document 1
--set d to (duration - current time) / time scale -- seconds left (QT 7)
set d to (duration - current time) -- seconds left (QT X) ?
end tell
end tell
set d to round d rounding up
if d < 1 then set d to 1 -- to guarantee d >= 1
return d
end secLeft
--END OF SCRIPT 2


The option (2) would be the simplest if it suffices.
However, if you have some existing QT scripts that won't work with QTX, you'd perhaps better install QT7 rather than trying to adapt the scripts to QTX.

In case, here's a related topic regarding QTX and QT7 -
Topic : Applescript and Snow Leopard
http://discussions.apple.com/thread.jspa?threadID=2134409

All the best,
Hiroto

controlling quicktime with Applescript

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.