Disable system sound output for a script?

Hello community,

Does anyone of you know if there is a way to disable system sounds (such as OS X’s error beep) during the time a script is running?

I could be using ⌘C and a simple comparison between a previous clipboard state and a new clipboard state to determine whether there was a text selection in an app. (If the content is still empty afterwards, there was no text selected) Unfortunately the following:

tell application "System Events" to keystroke "c" using {command down}

Produces an error beep when the keystroke can’t be executed. Is there a way around this?


You’re not telling to which process the keystroke (read: event) needs to send to. So when for example TextEdit is your target application you should have something like this:

tell application "System Events" to tell process "TextEdit" to keystroke "c" using {command down}

The code presses c with {command key} down in the frontmost application, which is what I’m after. No need to add an additional “tell application”.


This how you disable system sounds.

set volume alert volume 0

You may want to save them first.

set currentVol to alert volume of (get volume settings)

That works perfectly! Thank you a lot!

But you most certainly do need to add a “tell process”, exactly as DJ advised.

Please read reply #3 in this thread.

I did, which is why I suggested you follow reply #2.

Then I’ll have to repeat myself. It is not necessary for what I’m doing.

Zettt’s right, unless things have just changed.

tell application "Stickies" to activate

tell application "System Events" to tell application process "TextEdit" to keystroke "Where am I?"

You’ve lost me.

Your code is (more or less) what DJ said: Zettt’s original code had no “tell [application] process” in it.

Precisely. My script writes the text in Stickies, not in TextEdit.


I have a vague recollection ” possibly wrong ” that on my Jaguar machine, ‘keystroke’ and ‘key code’ were listed separately from the Processes Suite in System Events’s dictionary. But in any case, they’re not influenced by process ‘tell’ statements and can be used even with GUI Scripting disabled. However, what ‘keystroke’, at least, can type is subject to the software keyboard in use at the time.

I notice they use the same code as click and perform, “prcs”, which smells a bit like Processes Suite, so maybe this once I’ll query your recollection :wink:

So that’s what “cause the target process to behave as if” means…

I wouldn’t make assumptions about the semantics in a dictionary populated with excruciating grammatical howlers like ‘datas’ and “Is the media a local volume?” :wink:

Point taken…

Nigel is entirely correct. In my scenario the code I’ve been asking for is part of a bigger script and I needed this bit to figure out if the frontmost app had a text selection. The easiest way to do this is to set the clipboard contents to “”, press ⌘C, then figure out if the clipboard is still empty. If it isn’t, there was a text selection and the original clipboard contents can be restored.

This whole “if text was selected”-thing is something I’m after for a while now and it drives me nuts. The easiest way to check for a text selection would be a Service. Services can be activated via text selection from every app, but then AppleScript would need some way to activate this Service. The easiest I could come up with was:

tell application frontAppName to activate
delay 0.3 -- wait a little for process to be frontmost
tell application "System Events"
		click menu item "Get Selection" of ((process frontAppName)'s (menu bar 1)'s ¬
			(menu bar item frontAppName)'s (menu frontAppName)'s ¬
			(menu item "Services")'s (menu "Services"))
	on error errMsg
		display dialog errMsg
	end try
end tell

But this code is really slow. It takes about 1 sec for AppleScript to execute, no matter how often it runs.

Telling System Events to ⌘C is way faster, but has the downside that if no text was selected the user hears this little error beep. (Probably making them assume there was something wrong, which is not the case. For the script there was just no text selection.) The easy way out is to turn down the volume for that moment, which has the downside that if there’s another error sound it would also be muted.

I haven’t been following this thread, but a keystroke Tab moves the focus.

Hi Zettt.

Does this work with your intended applications? It should return a boolean indicating whether or not there’s a copyable selection in the application’s front document.

tell application frontAppName to activate

tell application "System Events"
	tell application process frontAppName
		set frontmost to true
		set selectionExists to (enabled of first menu item of (menu 1 of menu bar item 4 of menu bar 1) where (value of attribute "AXMenuItemCmdChar" is "C") and (value of attribute "AXMenuItemCmdModifiers" is 0))
	end tell
end tell

Edit: Ah. I see you already knew about this from the other thread to which you linked. I’ll leave the suggestion here though.