1

I am making a service in Automator that runs the following AppleScript code:

on run {input, parameters}

    tell application "System Events"
        click at {393, 77}
        delay 0.1
        keystroke "Hello"
        delay 0.1
        keystroke return
    end tell
    return input

end run

If you couldn't tell, I'm trying to click on something and type "Hello" there. However, when I run this, all that happens is I hear the "plunk" sound when you do something invalid, twice. Why isn't this code working?

The log does say "Run AppleScript completed" and then "Workflow completed."

I have it set to run with no input on any application.

I tried increasing the delays, but that didn't help.

Let me know if there's any more details I can provide.

6
  • 1
    It doesn't work because the click at function doesn't actually simulate a system mouse click, it appears more to trigger an AXPress action associated with the object underneath the cursor at the time. Therefore, it will appear to click menu items and buttons, as those objects have this action programmed into them; but it would, for example, put focus onto a text field. Your code is fine - but it's just expecting click at to do something it cannot do.
    – CJK
    May 22, 2018 at 20:53
  • 1
    Anyway, keystroking text entry is really a last resort kind of technique - what specific app are you using, or what website and text field are you targeting ? There's probably a better way to do it.
    – CJK
    May 22, 2018 at 20:53
  • @CJK I'm trying to type a chat message into chat systems like Skype. I was hoping I could use it for more than one app though, hence why I did the keystroke method.
    – RothX
    May 23, 2018 at 12:44
  • @CJK So there's no way to simulate an actual mouse click?
    – RothX
    May 23, 2018 at 12:45
  • No, there is no way to simulate a "physical" mouse click with ordinary AppleScript; and, as far as I know, even AS-ObjC can't do it (although it used to be able to, and may be able to again in the future). You would have to use a plug in/scripting addition as suggested in the answer below.
    – CJK
    May 23, 2018 at 18:32

1 Answer 1

1

I would suggest downloading AppleScript toolbox scripting addition. Once installed in the proper locations, in Script Editor.app, , you will be able to use commands from the key and mouse suite from the AppleScript toolbox dictionary to…get, set, and click at mouse locations (coordinates)

Here is some sample code using commands from the AppleScript toolbox dictionary


EXAMPLE 1

set mousePointLocation1 to {745, 110} -- The Collapsed Menu
set mousePointLocation2 to {780, 340} -- TV Link In The Menu
set mousePointLocation3 to {885, 180} -- Apple TV 4K Icon

delay 1 -- For Demonstration Purposes
activate application "Safari"
delay 1 -- For Demonstration Purposes

AST click at mousePointLocation1 ¬
    number of clicks 1

delay 1 -- For Demonstration Purposes

AST click at mousePointLocation2 ¬
    number of clicks 1

delay 1 -- For Demonstration Purposes

AST click at mousePointLocation3 ¬
    number of clicks 1

enter image description here

enter image description here


EXAMPLE 2

-- For Demonstration Purposes
-- Gives Me Time To Put The Mouse Where I Want
delay 3

-- Gets Coordinates Of The Current Mouse Location
set currentMouseLocation to AST mouse point location

-- Mouse Click At Defined Location
AST click at currentMouseLocation ¬
    number of clicks 2 -- How Many Clicks

enter image description here


EXAMPLE 3

set mousePointLocation to {20.0, 20.0}

delay 1 -- For Demonstration Purposes

AST set mouse point location mousePointLocation ¬
    without holding mouse down

delay 1 -- For Demonstration Purposes

AST click at mousePointLocation ¬
    number of clicks 1

enter image description here

2
  • I will try this, but do you know why my initial code didn't work?
    – RothX
    May 22, 2018 at 20:17
  • 1
    Without knowing all the information such as which application you're trying to click in, etc... There's no way to isolate where the error Is being triggered
    – wch1zpink
    May 22, 2018 at 20:21

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .