make applescript auto-click - macos

I play a game in Safari.
I want to make an applescript that auto clicks for me in a special spot on the screen, I have tried many things but none of it has worked for me.
Any ideas how to do it?

You can record the mouse movement with Automator and play this record: you can save it as application, too...
It's the easiest way, in my opinion...

delay 3
tell application "System Events"
tell application process "TheApp"
key code 53
delay 1
click (click at {1800, 1200})
end tell
end tell
However, it doesn't seem to work in Mavericks.

I was trying to achieve the same thing as you a few months/years ago :)
I was botting(the thing you want to achieve) a game named Runescape =P
Of course it is against the rule so you may get banned :S
As sylter says you can use Automator to automate your task (which is the only solution) i tried to click the mouse button but i was not able to find out how :/
you should use automator! it is REALLY easy :)

repeat 10 times
delay 1
tell application "System Events"
tell application process "Safari"
click (click at {650, 730})
end tell
end tell
end repeat
should work for you

Related

Why doesn't Automator AppleScript End?

OK, I realize the solution to this is probably something very simple, but I've wasted a lot of time trying to figure it out. As you'll likely be able to tell, I'm new to AppleScript.
I have an AppleScript being run by Automator. It clicks a button in Chrome. I have an action I need to repeat nearly 1000 times, so I'm trying to automate it. Here's what I have:
tell application "Google Chrome"
activate
set theTab to tab 1 of window 1
execute theTab javascript "document.querySelectorAll('[title=Archive]')[0].click()"
end tell
This works as I want: It activates Chrome and clicks a button on the active tab. Clicking the button brings up an alert box for which the default is an OK button. Next I need to click that button, so I have a second AppleScript:
tell application "Google Chrome" to activate
delay 2
tell application "System Events"
keystroke return
end tell
Let me be clear: I know ultimately I should have a single script, but I created two to diagnose the problem I'm having.
It appears the first script just never ends, so it never gets to the second script. If I run the first script, stop it, and then run the second, I get exactly what I want. But unless I stop the first script, it just churns and never moves on. While it's executing the javascript, it seems that it just stops there.
Like I said, this is probably incredibly simple... and I feel incredibly stupid for not seeing the solution. What am I missing?
It's not as easy as you might think, because alert box with the "OK" button is modal. This means: the script will wait for the "OK" button to be pressed, only then will it continue further.
I can't test, because I don't use Google Chrome, and I don't know webpage you test with. Try my suggestion yourself (it uses idea of throwing artefact interruption):
tell application "Google Chrome"
activate
set theTab to tab 1 of window 1
try
with timeout of 1 second
set theResult to (execute theTab javascript "document.querySelectorAll('[title=Archive]')[0].click()")
end timeout
theResult
on error
tell application "System Events" to keystroke return
end try
end tell
Here is fully tested by me closing modal alert box programatically:
try
with timeout of 1 second
set theResult to display alert "TEST"
end timeout
theResult
on error
tell application "System Events" to keystroke return
end try

script auto correcting incorrectly when i compile and key down key up not working as well

alright I'm currently having a lot of issues with my applescript editor. currently when ever i do a Key up key down sequence it will always key the letter A. for example.
tell application "System Events"
key down "u"
key up "u"
delay 1
end tell
this will keystroke A and i don't know why.
^ thats only a minor issue though my big problem is that i want to swap between three different video game applications. They are all a duplicate of the same game. Heres how my basic script goes.
tell application "Video Game" to activate
tell application "System Events"
keystroke a bunch of stuff
end tell
tell application "Video Game copy" to activate
tell application "System Events"
keystroke a bunch of stuff
end tell
tell application "Video Game copy 2" to activate
tell application "System Events"
keystroke a bunch of stuff
end tell
The problem is when i try to compile the script the application name always corrects to "Video Game".
Any ideas on how to keep it from compiling in correctly?
I have tried putting in a bogus name then when applescript asks me to find the app i click the correct one under the browse search box. and it will still compile incorrectly.
thanks a bunch :)
what you describe seems a bug in applescript. Here is a little workaround for that. I wrote it for jxa (javascript for ui automation), so you have to port it to applescript i think:
ObjC.import("Cocoa");
function key(aiKeyCode, aiDelay)
{
var source = $.CGEventSourceCreate($.kCGEventSourceStateCombinedSessionState),
keyDown = $.CGEventCreateKeyboardEvent(source, aiKeyCode, true),
keyUp = $.CGEventCreateKeyboardEvent(source, aiKeyCode, false);
$.CGEventPost($.kCGAnnotatedSessionEventTap, keyDown);
delay(aiDelay);
$.CGEventPost($.kCGAnnotatedSessionEventTap, keyUp);
}
keystroke will get the key to be pressed once. It helps to tell the process App you are telling System Events to do something, i.e.
tell application "System Events" to tell process "App Name" to keystroke "u"
If you want to do it with command, option, shift, etc. use this:
tell application "System Events" to tell process "App Name" to keystroke "u" using {command down, shift down, option down}
What AppleScript Editor is doing is not “autocorrect” — it’s “compiling.” It’s not doing it incorrectly. You can change the name of an app on the Mac and it doesn’t break the app. AppleScript Editor is still able to find the app “Video Game” even if you change its name to “Video Game copy 1.” This is a feature, not a bug.

How to access drop down in application using applescript?

I'm trying to tell applescript to open the app, Application Loader, and choose from the drop down list that's in the window. The item that should be chosen will always be the very first item. How do i do this?
Thanks in advance.
Wow I finally got it working! I pretty much got mad at my script so I just put a random ASCII number and it magically worked. I got pretty happy aha :)
activate application "Application Loader"
tell application "System Events"
tell process "Application Loader"
tell the first combo box of window 1
delay 3
keystroke (ASCII character 30)
keystroke (ASCII character 12)
keystroke return
end tell
end tell
end tell

How do you programmatically press a toolbar button with AppleScript?

I hope this isn't too obvious, but I'd like to press one of the toolbar buttons within an application by means of AppleScript.
Background: The button doesn't have any menu item or keyboard shortcut. Thus, I can't activate it by any of those methods; I need to find an AppleScript way of actually 'pressing' the button.
I am going to expand a little on what #hced said.
I agree that click button 3 of tool bar 1 of window 1 works in many cases. However from OS to OS thic can be a moving target. My example is iTunes. In iTunes 10.5.8 the airplay button is 17 where in 10.7.5 it is 25. However, in both instances it is called "AirPlay"!
So I suggest using the developer kit, and getting this code from Apple:
http://developer.apple.com/library/mac/#samplecode/UIElementInspector/Introduction/Intro.html
Run the application and whatever you mouse over will get you all the info you need to make this work in your AppleScripts:
click (every button whose description is "AirPlay") of window "iTunes" of application process "iTunes"
In this case there should only be one button with this description, but you can use the tool to discover whether this is the case. If so, limit the scope!
Hope this helps!!
Bo
Ah, figured out that one myself, too. Seems I'm on a self-commenting spree here.
This example worked for me, referencing a specific toolbar button by means of its description attribute:
tell application "System Events"
tell process "OmniFocus"
click (every button whose description is "Contexts") of tool bar 1 of window 1
end tell
end tell
I'll leave it up for any commenters with an opinion about referencing every button whose descripton is "Contexts" (as per my example), to express those down below. But I presume there can't be multiple buttons with the same description, right?
Ok, that was simpler than I thought, once I found out how. (Googling it didn't leave much help at all, but an application called Prefab UI Browser did the trick finding out how this is done).
Example:
tell application "System Events"
tell process "OmniFocus"
click button 3 of tool bar 1 of window 1
end tell
end tell
I've got a follow-up question, though:
How do you reference a toolbar button by name/description? I know for a fact that the button has a description attribute with the value: "Contexts". But again, how do I reference it in the click statement?

Make ESCAPE (ESC) key close window

How do I make an application that I can make the ESC key close a window? Or really any key (I can change if I want to).
Please help. Thanks
Here is what I have and I can't get it to work.
-- close window with "ESC"
tell application "System Events"
tell application "Finder" to activate
key code 53
end tell
beep 3
Thanks! B-
There's a system wide shortcut (Command + W) that will close the currently activated window
tell application "System Events"
tell application "Finder" to activate
keystroke "w" using command down
end tell
However, I'm not sure that this answers your question. Its impossible to use plain AppleScript to listen to system wide keyboard events. Take a look at the answer to this question.
PCWiz's link should show you the way - have a look at the second answer (by ghoppe - setting keyboard shortcut to application in preferences). Just remember to save the script as application, otherwise it'll not be eligible.

Resources