Need Applescript Guidance

Hi All,
I have RSI troubles and am using MacSpeech Dictate for voice commands. It uses Applescripts for custom commands. I know nothing about Applescript.

I am wondering if it is possible to make an Applescript that will hold down a chosen modifier key until I tell it to release with another command.

I need to be able to hold down shift, command, spacebar, option, etc. and then have a 2nd command to release all keys at once.

Can anyone tell me if this is do-able and maybe point me in the right direction, ie code or resources that would help me understand?
Thanks

Mac Pro 2.8 ghz 8 core, Mac OS X (10.5.1), 10 GB Ram

Posted on Mar 26, 2008 12:31 PM

Reply
15 replies

Mar 26, 2008 3:37 PM in response to Vicky Silbert

The AppleScript Systems Events Process Suite has the 'keystroke' command. Here is an example of sending a page back to Safari:

tell application "Safari"
activate
tell application "System Events" to keystroke "[" using command down
end tell


Here is the complete syntax:
keystroke text : The keystrokes to be sent.
\[using command down/control down/option down/shift down\] : modifiers with which >the keystrokes are to be entered


This doesn't 'hold' the modifier key down until 'released', but applies it for all 'text' specified in the keystroke command. Maybe this will help you out.

Mar 26, 2008 3:59 PM in response to xnav

I appreciate the reply!

I guess I should clarify that this issue is very specific to pressing and holding a key until released. There are many built in scripts in Dictate which press a key. The issue is to be able to have a key, specifically the "SHIFT" key, be pressed continuously until I want to release it.

This was possible in the old iListen with and extra script pak called "MouseAnywhere", however the scripts from that version for this particular system event no longer work, and the version of Applescript being used seems to have changed because all the scripting language is very different in Dictate than it was in iListen....

So I don't know if what I want to do is possible, but would very much like to find out!!!
🙂

Mar 26, 2008 5:41 PM in response to Vicky Silbert

OK, now I am trying to use an Applescript to turn on and off Sticky Keys, and to hit the shift key 2x so it holds on. Here's my script so far, based on other scripts in the Dictate program. It's obviously wrong since it is not working yet at all.

============
set _currentAppName to short name of (info for (path to frontmost application as alias))
try
tell application "System Events"
tell process _currentAppName
{shift down}
end tell
end tell
end try
===============

I thought I could just put this code 2 times to hit shift twice, or five times to turn Sticky Keys off or on. I don't think this script is working even 1 time so far, obviously I do not know what I'm doing... clues?

Also if anyone knows a way to hold down a key w/o having Sticky Keys turned on, I am still interested to know this. Appreciate the help!!!
🙂

Message was edited by: V Silly

Mar 27, 2008 7:20 AM in response to Vicky Silbert

Have a look in the Scripts folder of your machine. On Tiger, there's a folder titled 'UI Element Scripts' that contains a script 'Key Down-Up.applescript' with examples for holding down modifiers.

In fact you can do so with AppleScript alone. But you need to use the method with caution. As an experiment, I altered the script to simply hold the command key down... without releasing it... and had to log out to recover use of the keyboard.

Here's a specific example using TextEdit:

--

tell application "TextEdit" to activate

tell application "System Events"
tell process "TextEdit"

key down shift
keystroke "p"
key up shift

end tell
end tell

--

Peter B.

-----

Mar 27, 2008 10:59 AM in response to Vicky Silbert

So far not working for me. Here is what I am trying... trying to make shift key trigger 2x to hold down w/ Sticky Keys. Manually hitting SHIFT 2x works, but my code does not:

set _currentAppName to short name of (info for (path to frontmost application as alias))
try
tell application "System Events"
tell process _currentAppName
key down shift
key up shift
key down shift
key up shift
end tell
end tell
end try


What am I doing wrong and how did you code it exactly to get the command key to hold down?
Thanks

Message was edited by: V Silly

Message was edited by: V Silly

Mar 27, 2008 12:01 PM in response to Vicky Silbert

I hate it when Apple examples use undocumented commands! 'key down/up' is just that! I found that the 'key down shift' will not trigger the sticky, however, if you run a script that just has a 'key down shift' it will lock you into shifted state for all apps until you run another script with 'key up shift'. This is why the poster 'None' locked himself up by issuing just a 'key down command'.

BTW, you must have 'Enable access for assistive devices' set on for all this to work.

Message was edited by: xnav

Mar 27, 2008 12:05 PM in response to xnav

I'd try addressing the process directly first, then introduce other niceties later. I don't think you need Sticky Keys for this.

e.g.:

--

tell application "MacSpeech Dictate" to activate

tell application "System Events"
tell process "MacSpeech Dictate"

key down shift
-- do whatever while shift key is down
key up shift

end tell
end tell

--

In your example, you're releasing the shift key before doing anything else.

But I don't have MacSpeech Dictate to know how it works... or what you're specifically trying to do.

Some trial and error will be required.

Peter B.

-----

Mar 27, 2008 1:03 PM in response to None

Thanks to everyone who answered this post. Got it working, YAY! Here is what worked...

I turned off Sticky keys as suggested. First I made a script to work globally in all applications and release all modifiers at once:

set _currentAppName to short name of (info for (path to frontmost application as alias))
try
tell application "System Events"
tell process _currentAppName
key up option
key up shift
key up command
end tell
end tell
end try


Then I made a similar script to hold down SHIFT:

set _currentAppName to short name of (info for (path to frontmost application as alias))
try
tell application "System Events"
tell process _currentAppName
key down shift
end tell
end tell
end try


Pasted these into Dictate Commands and now I can hold and release SHIFT modifier hands free! YAY!!! Thanks again, this is a life saver!!!
🙂
Best
V

Message was edited by: V Silly

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

Need Applescript Guidance

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.