0

I'm not an AppleScript coder or a technical person by any means but I have done some research and came across a solution for my problem statement.

My problem statement

How to make a mouse click happen at a specified time.

To that end, I have found a simple script online that makes the mouse cursor click.

Here it is.

tell application "System Events"
    click
end tell

And now, I am trying to trigger this script at a certain using launchd. I have created a plist file under the "LaunchAgents" folder in Library as you can see in the screenshot below. enter image description here

I have configured this plist file to trigger the script at 6AM my time. I even open the webpage and position my cursor just above the item that I want clicked in order to reduce complexity.

Coding of the plist file

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>Label</key>
    <string>com.mouseclick.plist</string>
    <key>ProgramArguments</key>
    <array>
        <string>/usr/bin/osascript</string>
        <string>/Users/mugenvoid/Downloads/mouseclick.scpt</string>
    </array>
    <key>StartCalendarInterval</key>
    <dict>
        <key>Hour</key>
        <integer>6</integer> <!-- Adjust the hour as needed -->
        <key>Minute</key>
        <integer>10</integer> <!-- Adjust the minute as needed -->
    </dict>
</dict>
</plist>

But for the life of me, I just cannot get it to trigger at the concerned time even after I load this plist file into launchd.

This is the guide I've been trying to use --> https://www.maketecheasier.com/use-launchd-run-scripts-on-schedule-macos/

Would appreciate some help here.

  • FWIW: I was unable to get anything going at all with Apple Script. However I learned enough about Automator in this Q&A to achieve my objective. Also - I learned some time ago that the 3rd-party app LaunchControl is so much more efficient than diddling around with that stuff in your guide that it's impossible to ignore; i.e. I recommend LaunchControl highly! – Seamus Mar 26 '24 at 08:42
  • Putting aside your launchd stuff, your 'click' script doesn't do anything when I run it. What does it do for you? – Mockman Mar 26 '24 at 13:14
  • @Seamus Thanks for the answer! I have fiddled around with Automator a bit and tried creating an automation workflow that opened Google Chrome (my go-to browser) and performed mouse-clicks on the desired webpage element. But the workflow would always run into an error and never give me the desired results. Since you are more well versed with this application than me, would you be able to suggest how I can go about achieving this in automator without any issues? I'm ok with using Safari (if other browsers are not supported) or reducing complexity by placing my mouse cursor at the spot directly – Anshu Chang Mar 26 '24 at 23:45
  • @Mockman It doesn't do anything as well. I'm not versed with AppleScript at all and this is a code snippet I picked up from ChatGPT. I had a feeling that the code is not sufficient. Any way I can actually get it to perform a mouse click event? – Anshu Chang Mar 26 '24 at 23:46
  • I wrote a response below as an answer. By the way, I should also ask… what is the purpose of the click. I hadn't looked closely before but now i see that your screen shot is of the Finder (I don't have a dark mode). – Mockman Mar 27 '24 at 00:34
  • Anshu: Unfortunately, I am not well-versed. I asked a question siimilar to yours a couple of weeks ago, and @Mockman answered. I was lucky in that my application was simple. One thing I will mention is this: the output of Automator is a <name>.workflow file. What I had to do was write a script that called automator, and process the data retrieved with my script. – Seamus Mar 27 '24 at 01:55
  • @Mockman Apologies for the delayed reply. I was sucked into some irl work and obligations. The point of me writing this script and going through all this automation effort is to click on a "Save" button for an "punch in/punch out" attendance marking system at exactly 6AM my time in order to punch myself out.

    At work, I punch my hours in and out using a website called Replicon. And at times, I run the graveyard shift. So during the latter half of such a shift, I may not always be around my device to punch myself out at the right time, which may lead to complications for me down the road.

    – Anshu Chang Mar 29 '24 at 23:12

1 Answers1

1

Here is an example mouse click (that works for me) using safari.

tell application "Safari"
    activate -- bring safari to front
    tell application "System Events"
        tell application process "Safari"
            set xy to click at {250, 675}
        end tell
    end tell
end tell

Notice the result: The 'element' at the click point is a link to a web site.

static text "example.com" of UI element "example.com" of group 2 of list 6 of ¬
    UI element 1 of scroll area 1 of group 1 of group 1 of tab group 1 of ¬
    splitter group 1 of window "net links" of application process "Safari"

I can also click the link by referring to its element directly. To do this, replace the click at command line with the following commands. This would be more robust than clicking a spot on the screen.

set xy to static text "example.com" of UI element "example.com" of group 2 of list 6 of ¬
    UI element 1 of scroll area 1 of group 1 of group 1 of tab group 1 of ¬
    splitter group 1 of window "net links" of application process "Safari"
click xy

It assigns the reference to the variable xy and then clicks it. You could consolidate the two lines into one but I think it's clearer this way.

It may be possible to get the equivalent for whatever app is in your screen shot.

By the way, that's one of the reasons that chatgpt shouldn't be sourced for code here. It seems to often produce code that looks sensible but does nothing. There are some questions here (and on stackoverflow) which explain in detail how to script the user interface; try searching for a combination of applescript and ui scripting.

Mockman
  • 1,200
  • Good answer! I'm glad we have a resident expert on Automator now. :) – Seamus Mar 27 '24 at 01:48
  • @Seamus All I can say is… Yikes! – Mockman Mar 27 '24 at 02:20
  • Thanks for the answer @Seamus Since you asked about my use-case, I have elaborated on the same on the comments section of my original post. I will try to make sense of this answer and return back to you if I have doubts. – Anshu Chang Mar 29 '24 at 23:08