Several years ago I wrote an AppleScriipt that I could launch from my Desktop by double-clicking on an alias of the compiled code stored in the /Applications folder. It looked like this:
tell application "Finder"
with timeout of 15 seconds -- make sure we stop
eject (every disk whose ejectable is true)
end timeout
end tell
And it worked just great, until El Capitan (and later systems). I was now required to "hold down the "option key" if any of the disks were partitioned, meaning had more than one Volume on them. That was VERY ANNOYING. But, just recently I read a post on a forum where someone asked how they could write an AppleScript that held down a holdable key, like 'control', 'shift', 'option', 'command', or 'caps lock', for 5 seconds. The script was simple:
tell application "System Events"
key down option
delay 5
key up option
end tell
OK, that looks simple enough. But then I reasoned that 'delay 5' could be anything that might take time to do, like eject disks. So I wrappered my Eject code:
tell application "System Events"
key down option
tell application "Finder"
with timeout of 15 seconds -- make sure we stop
eject (every disk whose ejectable is true)
end timeout
end tell
key up option
end tell
THAT WORKED! I no longer need to hold down the option key ... the executable script does it for me. So now you know how to wrapper a task with held down keys. Just use a 'key down keyname' for each key you need held, do your thing (possibly in another tell -> end tell), and then undo with 'key up keyname' in the reverse order before ending your use of "System Events" code block ... I don't know what would happen if they weren't nested code blocks. Some day, I might try three separate tell -> end tell code blocks, in series. But not today.