Pausing a script

I’m new to this board so if I transgress against the board’s usual practices, please let me know so I can correct myself.

My ultimate problem is to get Excel (2004) to play sounds. The few sound commands in VBA are not supported for Mac. And the usual solutions from books use Windows API functions.

My workaround is to use the VBA command MacScript which accepts an AppleScript (as a string argument) and executes it.

So I have a script that opens a document, plays it and then closes it.

The problem is that the script doesn’t wait until the sound is finished playing before executing the next line, closing the file, which defeates things.

Is there an AppleScript command that I can use to pause in the indicated place until the document is finished playing?
or
Can I get AppleScript to pass a variable (duration of the sound document) back to Excel, where I can have VBA pause before calling the close routine?

tell application "Finder"
	open document file "Cash Register" of folder "Sounds" of folder "Office" of folder "Microsoft Office 2004" of folder "Applications" of startup disk
end tell
tell application "QuickTime Player"
	play document "Cash register"
	-- Desire to pause HERE
	close document "Cash register" -- closes before document is finished playing
end tell

Model: MacBook
AppleScript: Script Editor 2.2
Browser: Safari 525.20.1
Operating System: Mac OS X (10.5)

I also recommend this post :smiley:

And you can also try a «delay» statement:


tell application "QuickTime Player"
   play document "Cash register"
   -- Desire to pause HERE
   delay 5
   close document "Cash register" -- closes before document is finished playing
end tell

Thanks for the link. I’m still learning AppleScript, but something in that link triggered something which led to something …
Which resulted in this script, which does exactly what I want.

tell application "Finder"
	open document file "Cash Register" of folder "Sounds" of folder "Office" of folder "Microsoft Office 2004" of folder "Applications" of startup disk
end tell
tell application "QuickTime Player"
	play document "Cash register"
	
	repeat while playing of document "Cash register"
	end repeat
	
	close document "Cash register"
end tell

Thanks!

If the sound clips were longer, locking things into that loop might cause issues, but for the shorter stuff that I’m looking at, its perfect.

Update:10/27 9:19am PDT Here is the complete application in an Excel workbook http://www.vbaexpress.com/forum/showthread.php?t=23121

have you tried playing the sound with a nice little app called play a sound. it plays many formats without a window showing you that it’s playing.

No, I don’t have that application. (At least Spotlight can’t find it.)
And I suspect the need for a pause would still exist.
As I mentioned, I’m learning AppleScript, which introduces me to a new kind of environment.
I’m used to Excel macros, which tell have Excel tell itself to do something and then waits until that action is complete before handing out its next instruction.
I’ve learned that AppleScript gives instructions to applications different different than itself, and, since its a different program than the one executing the command, it doesn’t wait for the command to finish before giving out its next command.

http://www.macupdate.com/info.php/id/8479/play-sound

I’d put a delay statement in the repeat loop.

tell application "Finder"
	open document file "Cash Register" of folder "Sounds" of folder "Office" of folder "Microsoft Office 2004" of folder "Applications" of startup disk
end tell
tell application "QuickTime Player"
	play document "Cash register"
	
	repeat while playing of document "Cash register"
	delay 2
	end repeat
	
	close document "Cash register"
end tell

Thank you. The command( if thats the proper term) “delay” looks like it will be useful. (I need to get a book. ie a list of all the commands.)

Is there any benefit (use of resources, use of memory etc.) from a loop with delay over an “empty” loop.?