Is it possible to toggle a keyboard modifer key or alpha numeric key via Applescript?
For example can Applescript turn the caps lock key or the option key on and off?
Model: Mac Book Pro 2015
AppleScript: 2.11
Browser: Safari 537.36
Operating System: macOS 10.14
Open Keyboard Viewer manually (just to see that the following script works)
Run script:
-- example 1: toggling option key of the keyboard
tell application "System Events"
key down option
delay 5 -- do here what you need instead
key up option
end tell
delay 2
-- example 2: toggling shift key of the keyboard
tell application "System Events"
key down shift -- toogle numeric keys & capitalize letter keys
delay 5 -- do here what you need instead
key up shift
end tell
delay 2
-- example 3: hiding the script editor
tell application "System Events" to keystroke "h" using command down
Thank you very much for your help. The toggle works well.
The idea of the Option key toggle is create the combination key sequence Otion+Click.
While the Option key is down a pointer click takes place.
Is it possible to end the delay after the pointer click?
In other words if the delay is 3 seconds, can de delay stop immediately after the click input?
Let me know if this makes sense?
The delay, controlled by the occurrence of a certain event, can be implemented by applying an infinite repeat loop to the following scheme. But what event follows after your mouse click? If, for example, a window appears on the screen under the name “I got hung up”, then the script can exit the repeat loop, making sure that such a window has just appeared:
tell application "System Events"
key down option
repeat
delay 0.02
if window "I got hung up" of process myApp exists then
exit repeat
end if
end repeat
key up option
end tell
I work with a graphic tablet and Photoshop. I use a stylus pen with a button to trigger the AppleScript. With the AppleScript Option key down, I can copy the area below the pen tip. The delay allows me to position the pen tip over the area that I want to copy. Once I copy, then I must wait for the delay to terminate. It would be useful to terminate the delay as soon as the pen copies the area below the tip.
Also, there is a lag in the process. Once I sample the area using the AppleScript I must wait 3-5 seconds before I can resample using the AppleScript.
I don’t know. Try to clean the clipboard, then check if it receives some picture:
set the clipboard to ""
tell application "System Events"
key down option
repeat
delay 0.02
try
set |selection| to (the clipboard as JPEG picture)
exit repeat -- exits repeat loop only when the clipboard has some picture
end try
end repeat
key up option
end tell
You can put your process into external repeat loop:
repeat
display dialog "You want to make new selection?" buttons {"Yes", "No"}
if button returned of result is "No" then exit repeat
set the clipboard to ""
tell application "System Events"
key down option
repeat
delay 0.02
try
set |selection| to (the clipboard as JPEG picture)
exit repeat -- exits repeat loop only when the clipboard has some picture
end try
end repeat
key up option
end tell
delay 5
end repeat
That is an interesting idea, to clear the clipboard first. Then when the stylus copies to the clipboard end the delay. It makes sense but I run into trouble when I run the script.
When I run the AppleScript the OSX gets caught in a loop and it is not able to end the task. The option key eye target stays on as if the option key was permanently in the down position. The only way to exit is by restarting the computer.