Add a pause button? Play a sound without QuickTime Player?

Hello and happy holidays,

First, the pre-requisite “I’m an AppleScript Newbie, so be kind.” :slight_smile:

A teacher at my school wanted a program that would make a beep sound randomly every 10 to 90 seconds, so I wrote her a script and saved it as an Application bundle (so the beep sound I intended to play would follow the script around…the default beep built into the system won’t cut it).

I would like to add two things, but don’t know how to do it (or in the case of the second request, I don’t even know if it can be done).

  1. Is there a way to add a puse button in addition to the stop button I have provided?

  2. Is there a way to play the sound without using an app like QuickTime Player…kind of an audio version of Image Events?

Thank you,

Dave


--Random Beep Generator by Dave Huber - Nov. 2006
--Makes a beep sound randomly between 10 and 90 seconds


--figure out where the beep sound is located inside the Application Bundle (package)
set beep_sound to (path to me) & "Contents:Resources:Sounds:beep.mov" as string
set beep_sound to beep_sound as alias


--Give user a start button
set dialogResponse to the button returned of (display dialog "Welcome to the Beep Randomizer!

Press Start to begin beeping randomly between 10 and 90 seconds." buttons {"Start"} default button 1)


--Launch QT Player, open beep sound
tell application "QuickTime Player"
	activate
	open beep_sound as alias
end tell


--Hide QT player
tell application "System Events" to set visible of (first process whose name begins with "QuickTime") to false

-- Randomize pause time, then Play the beep sound
repeat
	set pause_time to random number from 10 to 90
	tell application "QuickTime Player"
		play movie 1
	end tell
	
	--Give user a stop button.  If it isn't pressed, repeat the randomizing & playing function
	tell application "SystemUIServer"
		activate
		
		set StopButton to the button returned of (display dialog "Do you want to stop this infernal beeping yet?" buttons {"STOP!"} default button 1 giving up after pause_time)
		
		if StopButton = "STOP!" then exit repeat
		
	end tell
end repeat

--when the stop button is pressed, quit QT Player and end script
tell application "QuickTime Player"
	quit
end tell


Hi Dave,

to 1)
my suggestion is to use a idle handler which make it easier to implement a Pause button (simply a boolean flag)
The script must be saved as stay open to work correctly

to 2)
I would use the beep statement and change the system sound temporarily to your “infernal beeping” (format .aiff)
(I didn’t change anything about this in the script)

--Random Beep Generator by Dave Huber - Nov. 2006
--Makes a beep sound randomly between 10 and 90 seconds

property doRun : true

--figure out where the beep sound is located inside the Application Bundle (package)
on run
	set beep_sound to ((path to me) & "Contents:Resources:Sounds:beep.mov" as string) as alias
	
	--Give user a start button
	set dialogResponse to the button returned of (display dialog "Welcome to the Beep Randomizer!

Press Start to begin beeping randomly between 10 and 90 seconds." buttons {"Start"} default button 1)
set doRun to true	
	
	--Launch QT Player, open beep sound
	tell application "QuickTime Player"
		activate
		open beep_sound
	end tell
	
	
	--Hide QT player
	tell application "System Events" to set visible of (first process whose name begins with "QuickTime") to false
	return (random number from 10 to 90)
end run

-- Randomize pause time, then Play the beep sound
on idle
	if doRun then tell application "QuickTime Player" to play movie 1
	
	--Give user a stop button.  If it isn't pressed, repeat the randomizing & playing function
	tell application "SystemUIServer"
		activate
		
		if doRun then
			set StopButton to the button returned of (display dialog "Do you want to stop or pause this infernal beeping yet?" buttons {"STOP!", "PAUSE"} default button 1 giving up after 10)
		else
			set StopButton to the button returned of (display dialog "Do you want to stop or run again this infernal beeping yet?" buttons {"RUN", "PAUSE"} default button 1 giving up after 10)
		end if
		if StopButton = "STOP!" then
			tell me to quit
		else if StopButton = "RUN" then
			set doRun to true
		else if StopButton = "PAUSE" then
			set doRun to false
		end if
	end tell
	return (random number from 10 to 90)
end idle

--when the stop button is pressed, quit QT Player and end script
on quit
	tell application "QuickTime Player"
		quit
	end tell
	continue quit
end quit

Have you looked at Play Sound?

http://osaxen.com/files/playsound1.6.2.html

here is a version without QuickTime Player.
It saves the current system alert sound and sets a new one for simply using the beep command.
I couldn’t find out how to set the sound with “defaults write…” so I used UI scripting


--Random Beep Generator by Dave Huber - Nov. 2006
--Makes a beep sound randomly between 10 and 90 seconds

property doRun : true
property mySound : "Oing" -- name of a system alert sound
global currentSound

--figure out where the beep sound is located inside the Application Bundle (package)
on run
	-- get current system alert sound
	set myMACaddress to words 2 thru 7 of (do shell script "ifconfig en0 ether | grep -i ether" as string) as string
	set currentSound to do shell script "defaults read ~/Library/Preferences/ByHost/.GlobalPreferences." & myMACaddress & " com.apple.sound.beep.sound"
	set currentSound to 1st word of name of (info for (POSIX file currentSound as alias))
	--Give user a start button
	set dialogResponse to the button returned of (display dialog "Welcome to the Beep Randomizer!

Press Start to begin beeping randomly between 10 and 90 seconds." buttons {"Start"} default button 1)
	set_system_sound(mySound) -- set new sound
	set doRun to true
	return (random number from 10 to 90)
end run

-- Randomize pause time, then Play the beep sound
on idle
	if doRun then beep
	
	--Give user a stop button.  If it isn't pressed, repeat the randomizing & playing function
	tell application "SystemUIServer"
		activate
		if doRun then
			set StopButton to the button returned of (display dialog "Do you want to stop or pause this infernal beeping yet?" buttons {"STOP!", "PAUSE"} default button 1 giving up after 10)
		else
			set StopButton to the button returned of (display dialog "Do you want to run again or pause this infernal beeping yet?" buttons {"RUN", "PAUSE"} default button 1 giving up after 10)
		end if
		if StopButton = "STOP!" then
			tell me to quit
		else if StopButton = "RUN" then
			set doRun to true
			beep
		else if StopButton = "PAUSE" then
			set doRun to false
		end if
	end tell
	return (random number from 10 to 90)
end idle

--when the stop button is pressed, quit QT Player and end script
on quit
	set_system_sound(currentSound)
	continue quit
end quit

on set_system_sound(theSound)
	set av to alert volume of (get volume settings)
	set volume alert volume 0
	tell application "System Preferences"
		reveal anchor "effects" of pane id "com.apple.preference.sound"
	end tell
	
	tell application "System Events"
		tell table 1 of scroll area 1 of tab group 1 of window 1 of process "System Preferences" of application "System Events"
			set a to 1st row whose value of text field 1 is theSound
			tell a to set selected to true
		end tell
	end tell
	tell application "System Preferences" to quit
	set volume alert volume av
end set_system_sound

Here it is using the defaults write, so no UI interaction needed.

It resets to the old sound when quit.


--Random Beep Generator by Dave Huber - Nov. 2006
--Makes a beep sound randomly between 10 and 90 seconds

property doRun : true
property mySound : "/Users/UserName/Documents/TigerGrowl.mp3" -- name of a system alert sound
global currentSound, myMACaddress

--figure out where the beep sound is located inside the Application Bundle (package)
on run
	-- get current system alert sound
	set myMACaddress to words 2 thru 7 of (do shell script "ifconfig en0 ether | grep -i ether" as string) as string
	set currentSound to do shell script "defaults read ~/Library/Preferences/ByHost/.GlobalPreferences." & myMACaddress & " com.apple.sound.beep.sound"
	
	--Give user a start button
	set dialogResponse to the button returned of (display dialog "Welcome to the Beep Randomizer!

Press Start to begin beeping randomly between 10 and 90 seconds." buttons {"Start"} default button 1)
	
	set av to alert volume of (get volume settings)
	set volume alert volume 0
	set_system_sound() -- set new sound
	set volume alert volume av
	set doRun to true
end run

-- Randomize pause time, then Play the beep sound
on idle
	if doRun then beep
	
	--Give user a stop button.  If it isn't pressed, repeat the randomizing & playing function
	tell application "SystemUIServer"
		activate
		if doRun then
			set StopButton to the button returned of (display dialog "Do you want to stop or pause this infernal beeping yet?" buttons {"STOP!", "PAUSE"} default button 1 giving up after 10)
		else
			set StopButton to the button returned of (display dialog "Do you want to stop or run again this infernal beeping yet?" buttons {"RUN", "PAUSE"} default button 1 giving up after 10)
		end if
		if StopButton = "STOP!" then
			tell me to quit
		else if StopButton = "RUN" then
			set doRun to true
			beep
		else if StopButton = "PAUSE" then
			set doRun to false
		end if
	end tell
	return (random number from 10 to 90)
end idle

--when the stop button is pressed, quit QT Player and end script
on quit
	set av to alert volume of (get volume settings)
	set volume alert volume 0
	do shell script "defaults write ~/Library/Preferences/ByHost/.GlobalPreferences." & myMACaddress & " com.apple.sound.beep.sound " & currentSound
	set volume alert volume av
	continue quit
end quit

on set_system_sound()
	do shell script "defaults write ~/Library/Preferences/ByHost/.GlobalPreferences." & myMACaddress & " com.apple.sound.beep.sound " & mySound
	
end set_system_sound

thanks, I tried this (of course), Mark, but on my machine the sound didn’t change after writing in this (I call it cache-)file

Hi stefen, sorry I do not understand what you mean by

on my computer the “do shell script ‘defaults write…’” line has no effect, the system alert sound doesn’t change

em… works perfect here?

what did you put into the:

property mySound :

a POSIX path to the sound file, what else? :wink:

ah :wink:

ok what do you get if you do

set myMACaddress to words 2 thru 7 of (do shell script "ifconfig en0 ether | grep -i ether" as string) as string
set currentSound to do shell script "defaults read ~/Library/Preferences/ByHost/.GlobalPreferences." & myMACaddress

before and after

set mySound to "full/path/to/sound/file"
set myMACaddress to words 2 thru 7 of (do shell script "ifconfig en0 ether | grep -i ether" as string) as string
do shell script "defaults write ~/Library/Preferences/ByHost/.GlobalPreferences." & myMACaddress & " com.apple.sound.beep.sound " & mySound

Note: to reset just open the sys pref and click a system sound.

Wow, thanks for all of the input. I have not been able to get the script working per your suggestions, but maybe midnight is the wrong time to try to understand this stuff.

I am going to hit it fresh again tomorrow.

Thanks again,

Dave

You have to save it as a app. And Stay open selected.

Also when I first tried the default read, I did not get a result. For some reason the part I was looking for did not exist.
So I went into my Sys prefs and clicked a system sound. This then created the part needed.

You are right, it works indeed.
I tried it yesterday and it failed, maybe because I wrote the “defaults write” line and straight after it a beep command.
When i ran it the sound didn’t change and I gave up.

Thanks

This is almost exactly what I have been searching for, however is it possible to adjust this script so that it plays my custom alert sound once and then switches back to the default alert sound just by running the script and without the GUI interaction of pressing start, pause, stop etc.? I know that there are several applications that allow you to play custom sounds but my problem is that I am trying to play an empty system alert file so that instead of hearing an audible sound, my screen flashes.

“hexum

Man, I had forgotten about this project. I would like to help, but I actually went a different direction by making the script an application and embedding the sound into the package contents. This way the script would work on any Mac and I did not have to use the system sounds at all. Sorry…

For anyone that views this in the future, here is the script. I created a beep sound, saved it as “beep.mov” and put it into the package when I created the app (Contents:Resources:Sounds:beep.mov)

-Dave



--Random Beep Generator by Dave Huber - Nov. 2006
--Makes a beep sound randomly between 10 and 90 seconds


--figure out where the beep sound is located
set beep_sound to (path to me) & "Contents:Resources:Sounds:beep.mov" as string
set beep_sound to beep_sound as alias


--Give user a start button
set dialogResponse to the button returned of (display dialog "Welcome to the Beep Randomizer!

Press Start to begin beeping randomly between 10 and 90 seconds." buttons {"Start"} default button 1)


--Launch QT Player, open beep sound
tell application "QuickTime Player"
	activate
	open beep_sound as alias
end tell


--Hide QT player
tell application "System Events" to set visible of (first process whose name begins with "QuickTime") to false

-- Randomize pause time, then Play the beep sound
repeat
	set pause_time to random number from 10 to 90
	tell application "QuickTime Player"
		play movie 1
	end tell
	
	--Give user a stop button.  If it isn't pressed, repeat the randomizing & playing function
	tell application "SystemUIServer"
		activate
		
		set StopButton to the button returned of (display dialog "Do you want to stop this infernal beeping yet?" buttons {"STOP!"} default button 1 giving up after pause_time)
		
		if StopButton = "STOP!" then exit repeat
		
	end tell
end repeat

--when the stop button is pressed, quit QT Player and end script
tell application "QuickTime Player"
	quit
end tell