Friday, April 28, 2017

#1 2013-06-03 12:43:25 pm

Zettt
Member
Registered: 2010-05-11
Posts: 21

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:

Applescript:

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?

Thanks, 
Andreas

Offline

 

#2 2013-06-03 01:00:56 pm

DJ Bazzie Wazzie
Member
From: the Netherlands
Registered: 2004-10-20
Posts: 2606

Re: Disable system sound output for a script?

Zettt wrote:

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:


Applescript:

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

Offline

 

#3 2013-06-03 01:08:30 pm

Zettt
Member
Registered: 2010-05-11
Posts: 21

Re: Disable system sound output for a script?

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".

Offline

 

#4 2013-06-03 01:44:41 pm

McUsrII
Member
Registered: 2012-11-20
Posts: 3046
Website

Re: Disable system sound output for a script?

Hello.

This how you disable system sounds.

Applescript:

set volume alert volume 0

You may want to save them first.

Applescript:

set currentVol to alert volume of (get volume settings)


Filed under: volume

Offline

 

#5 2013-06-03 05:33:15 pm

Zettt
Member
Registered: 2010-05-11
Posts: 21

Re: Disable system sound output for a script?

That works perfectly! Thank you a lot!

Offline

 

#6 2013-06-03 05:33:55 pm

Shane Stanley
Member
From: Australia
Registered: 2002-12-07
Posts: 4998

Re: Disable system sound output for a script?

Zettt wrote:

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".

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


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/

Offline

 

#7 2013-06-03 05:50:28 pm

Zettt
Member
Registered: 2010-05-11
Posts: 21

Re: Disable system sound output for a script?

Please read reply #3 in this thread.

Last edited by Zettt (2013-06-03 05:51:40 pm)

Offline

 

#8 2013-06-03 05:55:52 pm

Shane Stanley
Member
From: Australia
Registered: 2002-12-07
Posts: 4998

Re: Disable system sound output for a script?

Zettt wrote:

Please read reply #3 in this thread.

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


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/

Offline

 

#9 2013-06-03 06:16:48 pm

Zettt
Member
Registered: 2010-05-11
Posts: 21

Re: Disable system sound output for a script?

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

Offline

 

#10 2013-06-03 06:38:04 pm

Nigel Garvey
Moderator
From: Warwickshire, England
Registered: 2002-11-19
Posts: 4263

Re: Disable system sound output for a script?

Shane Stanley wrote:

Zettt wrote:

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".

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

Zettt's right, unless things have just changed.

Applescript:

tell application "Stickies" to activate

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


NG

Offline

 

#11 2013-06-03 06:43:31 pm

Shane Stanley
Member
From: Australia
Registered: 2002-12-07
Posts: 4998

Re: Disable system sound output for a script?

Nigel Garvey wrote:

Zettt's right, unless things have just changed.

Applescript:

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.


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/

Offline

 

#12 2013-06-03 06:50:03 pm

Nigel Garvey
Moderator
From: Warwickshire, England
Registered: 2002-11-19
Posts: 4263

Re: Disable system sound output for a script?

Shane Stanley wrote:

Nigel Garvey wrote:

Zettt's right, unless things have just changed.

Applescript:

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.


NG

Offline

 

#13 2013-06-03 07:31:10 pm

Shane Stanley
Member
From: Australia
Registered: 2002-12-07
Posts: 4998

Re: Disable system sound output for a script?

Aaah...


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/

Offline

 

#14 2013-06-04 01:45:42 am

Nigel Garvey
Moderator
From: Warwickshire, England
Registered: 2002-11-19
Posts: 4263

Re: Disable system sound output for a script?

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.

Last edited by Nigel Garvey (2013-06-04 01:52:14 am)


NG

Offline

 

#15 2013-06-04 01:59:15 am

Shane Stanley
Member
From: Australia
Registered: 2002-12-07
Posts: 4998

Re: Disable system sound output for a script?

Nigel Garvey wrote:

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.

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

But in any case, they're not influenced by process 'tell' statements

So that's what "cause the target process to behave as if" means...


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/

Offline

 

#16 2013-06-04 03:10:10 am

Nigel Garvey
Moderator
From: Warwickshire, England
Registered: 2002-11-19
Posts: 4263

Re: Disable system sound output for a script?

Shane Stanley wrote:

But in any case, they're not influenced by process 'tell' statements

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


NG

Offline

 

#17 2013-06-04 03:11:14 am

Shane Stanley
Member
From: Australia
Registered: 2002-12-07
Posts: 4998

Re: Disable system sound output for a script?

Point taken...


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/

Offline

 

#18 2013-06-04 03:27:33 am

Zettt
Member
Registered: 2010-05-11
Posts: 21

Re: Disable system sound output for a script?

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:

Applescript:

tell application frontAppName to activate
delay 0.3 -- wait a little for process to be frontmost
tell application "System Events"
   try
       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.

Offline

 

#19 2013-06-04 04:19:00 am

kel1
Member
Registered: 2013-01-11
Posts: 2188

Re: Disable system sound output for a script?

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


Os 10.10.3
Mbp

Offline

 

#20 2013-06-04 04:55:26 am

Nigel Garvey
Moderator
From: Warwickshire, England
Registered: 2002-11-19
Posts: 4263

Re: Disable system sound output for a script?

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.

Applescript:

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.

Last edited by Nigel Garvey (2013-06-04 04:58:42 am)


NG

Offline

 

#21 2013-06-04 06:21:52 am

Zettt
Member
Registered: 2010-05-11
Posts: 21

Re: Disable system sound output for a script?

I didn't remember seeing that code. I know I found it when I searched for it (before posting). I didn't believe this was the solution. That said I just tried it and it's fast, doesn't beep and seems to work, though I have yet to see whether this also works in the context of my bigger script. I'll try and let you know whether this works. Thanks for help, Nigel.

Offline

 

Board footer

Powered by FluxBB

[ Generated in 0.097 seconds, 14 queries executed ]

RSS (new topics) RSS (active topics)