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}
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 wouldn’t make assumptions about the semantics in a dictionary populated with excruciating grammatical howlers like ‘datas’ and “Is the media a local volume?”
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"
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.
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.