Is there a way to have applescript make sure all modifiers are release before moving on in a script? I am trying to run modifiers by triggering applescripts from QuicKeys or Quicksilver and it gets messed up of course by the modifiers I am holding down to trigger the script in the first place.
It seems like this should work if you add to the start of the script but it doesn’t.
tell application "System Events"
key up {command, option, control, shift}
end tell
I just suggest right away, that this is close to unsolvable without having a delay long enough to let you manage to get your fingers off the keyboard, after you have triggered the script and , to let the Applescript figure out that the keys aren’t pressed, because I think any keyboard modifiers that are physically down will override the key up command.
That would make sense. And sorry for not bringing a positive answer.
Actually, you could make an applet out of what itches you, and execute the applet by Spotlight, to not interfere with modifier keys on the keyboard.
Another solution would be to execute the scripts by mouse from the script menu. Or assigning the script to a Function Key without modifier.
Yes, you could loop/delay until the modifier key state is false!
And … getModKey by StefanK provides the same functionality, but it should be used in a shell script really, so you’d need only one do shell script. (That shell script is reusable from then onwards). It uses the Carbon Frameworks, so I am a bit unsure whether it works still on Mountain Lion.
This seems to be the case from all my tests. Even if I do key down then key up nothing seems to override the fact that you are still physically holding down the modifier from executing the script.
This might be a possibility. It was my hopes to pass this on to others and allow them to be able to assign whatever key they wanted. I pretty much have it working for me but if I can make it as failsafe as possible that would be nice.
This seems to be a great idea I’ll have to experiment with this and ASObjCRunner and see what seems to be the best solution. I am trying to not do extra installs since this will go on others computers but if that is more elegant and works better I’ll give that a go. Thank you both for the great ideas and help!
That is greeat, it so happens that I can make a getModKeys for you, that uses Corefoundation and works from Leopard onwards. I could have it ready in 4 days as I am in the middle of something. That you could put into the Contents:Resources folder of a script bundle, easy to distribute.
The deal is, that you see to that it is tested on a computer with two keyboards connected to it.
But I hope you do try Stefan’s first! I see no need to do this, if his works.
It returns 0 if no modifier key is pressed.
You can check single keys by passing the key name(s) as argument(s) (shift, option, command, control, capslock)
Thanks StephanK for the update link. I have googled “checkModifierKeys” and searched this forum but don’t see a place where you discuss how to use it. I see other references to it though, but where did this originally post?
The following script doesn’t seem to work, I am thinking I am needing an “if, than” statement but it seemed like from the dictionary and script example http://www.macosxautomation.com/applescript/apps/runner_vanilla.html that this would force modifier keys to be release and not check on their state.
tell application "ASObjC Runner"
shift key down of modifier keys
false
control key down of modifier keys
false
option key down of modifier keys
false
command key down of modifier keys
false
end tell
tell application "Finder"
activate
--Open a new finder window after all physical modifier keys have been made to release.
tell application "System Events" to keystroke "n" using {command down}
end tell
It seems ideally if I could
I should go down that road since it requires no other installs other than the script. How do I do this or maybe it isn’t possible?
checkModifierKeys returns a non-zero value, if any modifier key is down, otherwise 0
Put the executable somewhere and adjust the path
set modifierkeysPressed to true
repeat while modifierkeysPressed
set modifierkeysPressed to (do shell script "/path/to/checkModifierKeys") is not "0"
delay 0.2
end repeat
[i]There is also a property called modifier keys, which you can use to find out the state of the various modifier keys. You can get its properties in a single call, or query one of the properties representing a particular key. For example:
tell application “ASObjC Runner”
caps lock key down of modifier keys
→ true
properties of modifier keys
→ {caps lock key down:true, shift key down:false, command key down:true, control key down:false, option key down:false, class:mod keys}
end tell[/i]
It seems clear that it reads the status of the modifiers but doesn’t change it.
More, in the application dictionary, we may read :
mod keys‚n : The modifier keys.
properties
caps lock key down (boolean,r/o) : Is the caps lock key down?
command key down (boolean, r/o) : Is the command key down?
control key down (boolean, r/o) : Is the control key down?
option key down (boolean, r/o) : Is the option key down?
shift key down (boolean, r/o) : Is the shift key down?
properties (record, r/o) : All the object’s properties returned as a record.
It seems that you didn’t understood that “r/o” means : “read only”
KOENIG Yvan (VALLAURIS, France) mercredi 1 mai 2013 20:24:12
DESCRIPTION
checkModifierKeys, Returns “1” to standard output if one of or more of
the modifierkeys in the argumentlist were held down under execution or
“0” if not.
This is great thanks! Working fabulously, it had me stumped for a little bit because it was saying my path did not exist.
/Users/boshoshua/Dropbox/Reference/Applications/Script\ Reference/checkModifierKeys
When I ran the executable from the folder it gave me this path so apparently Dropbox is doing some strange path modification I haven’t seen before.
Yvan Koenig I did see the read only, I just misread that the example was showing it might return true and was thinking that the example was setting it to true. Thanks for clearing that up for me.
FWIW, I’ve adding an “await release of” command to ASObjC Runner. If anyone wants to test it, email me.
await release of‚v : Pause as long as these modifier keys are pressed. await release of list of shift/Œoption/Œcommand/Œcontrol/Œcaps lock : A list of the modifier keys to wait for.
[flashing for real] : Maximum number of seconds for which the menu bar icon will flash while waiting. Default is 0.
By the way, I am wondering about something, would I do any thing harm, or make havoc, if I put your app into the contents of an applet? -I haven’t done it, but I wonder how that would go, if it was legal to do so, and if something would become malfunctioning if the user had registered and older version.
The second scenario.
And how would you feel about it if I just “brought AsObjC runner with me”, in the applet’s contents, and then copied it out into the applications folder, if it already wasn’t there? Maybe showing a dialog to the user, disclosing what is about to happen.
I’m wondering, as some approach like this, would make it easier for me to incorporate it into scripts/applets, should I need it.
It should be fine on all counts. If you look at the Web page, under “Embedding ASObjC Runner in applets”, you’ll see instructions for the best way to do it.
I can’t see any problem. It’s basically yours to do what you like with, other than pass off as your own or sell.