Saturday, November 18, 2017

#1 2017-06-22 06:47:48 am

KevS
Member
Registered: 2017-05-23
Posts: 7

Click with modifier?

I use the following script a lot to script Photoshop:

Applescript:

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...

Applescript:

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?

Last edited by KevS (2017-06-22 06:48:42 am)


Filed under: click

Offline

 

#2 2017-06-22 07:16:51 am

DJ Bazzie Wazzie
Member
From:: the Netherlands
Registered: 2004-10-20
Posts: 2724
Website

Re: Click with 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.

Offline

 

#3 2017-06-22 09:25:14 am

t.spoon
Member
From:: BFE, Massachusetts
Registered: 2013-01-13
Posts: 206

Re: Click with modifier?

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/


Hackintosh built February, 2012 |  Mac OS Sierra
GIGABYTE GA-Z68X-UD3H-B3 | Core i5 2500k | 16 GB DDR3 | GIGABYTE Geforce 1050 TI 4GB
250 GB Samsung 850 EVO | 4 TB RAID
Dell Ultrasharp U3011 | Dell Ultrasharp 2007FPb

Offline

 

#4 2017-06-22 11:15:36 am

DJ Bazzie Wazzie
Member
From:: the Netherlands
Registered: 2004-10-20
Posts: 2724
Website

Re: Click with modifier?

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

Applescript:

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

Offline

 

#5 2017-06-23 05:02:34 am

KevS
Member
Registered: 2017-05-23
Posts: 7

Re: Click with modifier?

DJ Bazzie Wazzie wrote:

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

Applescript:

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.

Last edited by KevS (2017-06-23 06:02:41 am)

Offline

 

#6 2017-06-23 06:13:06 am

DJ Bazzie Wazzie
Member
From:: the Netherlands
Registered: 2004-10-20
Posts: 2724
Website

Re: Click with modifier?

KevS wrote:

It works! Thank you.

You did that on the spot?!


Yes, it was a feature that's quite useful anyway smile

KevS wrote:

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).


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:

Applescript:

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:


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

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.

KevS wrote:

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


No application context or current application to avoid those errors.

Last edited by DJ Bazzie Wazzie (2017-06-23 06:18:16 am)

Offline

 

#7 2017-07-04 06:03:34 pm

KevS
Member
Registered: 2017-05-23
Posts: 7

Re: Click with modifier?

DJ Bazzie Wazzie wrote:

...


Thanks a lot for the explanation and help!

Applescript:

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)?

Offline

 

#8 2017-07-04 06:31:00 pm

DJ Bazzie Wazzie
Member
From:: the Netherlands
Registered: 2004-10-20
Posts: 2724
Website

Re: Click with modifier?

KevS wrote:

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.

Last edited by DJ Bazzie Wazzie (2017-07-04 06:31:17 pm)

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)