Click with modifier?

I use the following script a lot to script Photoshop:

tell application "System Events"
	set origPos to AST mouse point location
	AST click at {2032.1, 964.0}
	AST set mouse point location origPos
end tell

However, for one function, I need to have a click, but with a modifier.

I tried…

AST click at {2032.1, 964.0} using {option down}

…, but it doesn’t work.

How do you do an AppleScript to click with a modifier?

Hello KevS,

AppleScript Toolbox only clicks at certain locations which uses CoreGraphics set send an kCGEventLeftMouseDown event followed by an kCGEventLeftMouseUp event. If you want I can add support for an keydown before the click and keyup afterwards which for key modifiers shift, alt, control and command. I don’t think it too much work to add it to AST.

DJ modifying Applescript Toolbox to add this functionality certainly sounds like the most convenient option, but alternately, you should be able to do this with ClickClick.

https://www.bluem.net/en/mac/cliclick/

Download version 2.0.8 from my site and you can use modifier keys while clicking

set origPos to AST mouse point location
AST click at {2032.1, 964.0} holding modifier key ast option key
AST set mouse point location origPos

p.s. AST will only run in current application context and is not part of system events

It works! Thank you.

You did that on the spot?!

About System Events, I’ve been using that for all my AST click scripts. It works”just tested it a few minutes ago. Tried with the active application and it works as well. I think it works slightly faster with the application (but there is so little difference that there might not be one).

I’m not telling you this to debate, but just to gives you a heads up (maybe my computer is set up in a weird way).

But I think it’s better to use the active application instead of System Events?

Regarding the code, what would you put for multiple modifiers?

Thanks again.

Yes, it was a feature that’s quite useful anyway :slight_smile:

I can explain why it seem to work. To inject an osax in an remote application the end user needs to authorize the osax to allow this or the osax needs to be installed in /System/Library/ScriptingAdditions folder. To avoid weird errors I declared all commands in the osax to be “User” AppleEvents. That means when an command is used outside the current application context it will throw an error. Because this security feature is added in Mac OS 10.6 and for backward compatibility the same command will be executed in the current application context when this error occurs. So for you running the script it may seem like it works but in fact it doesn’t.

Let’s execute the following code:

tell application "System Events"
	AST click
end tell

It works like a charm, right? No error and a nice click is performed. But let’s take a look at the log window:

[format]tell application “System Events”
AST click
→ error number -1708
«event ascrgdut»
→ error number -1708
AST click
→ error number -10004
end tell
tell application “Script Editor”
AST click
end tell[/format]

As expected the AST click command throws an error. The script is trying a second time and eventually it will try the same command in the current application context, which is in this case Script Editor. So while it may seems like System Events works for you in fact it doesn’t. So to avoid any errors it is better not to use system events because it’s unclear how long Apple will support this bounce back and one day your script won’t run and stops with error -1708.

With AST click there is another reason why I chose to let it bounce back to the current application context. The command is using CoreGraphics to perform the click and the click itself is in the global scope of your desktop with no regards to processes or applications. So even if I would add support for application context the command itself would still be performed in the global scope of your desktop. Adding support for application context would only led to more confusion so it’s more or less obvious not to support it.

No application context or current application to avoid those errors.

Thanks a lot for the explanation and help!

set origPos to AST mouse point location
AST click at {2032.1, 964.0} holding modifier key ast option key
AST set mouse point location origPos

With the new AST mouse click and modifier feature, how would you script multiple modifiers (at the same time for one click)?

As the dictionary discribes the value of parameter holding modifier key can either be a single value or a list of values; holding multiple modifier keys requires an list of values.