Jump to content

[Solved] applescript click event not working in Alfred


Recommended Posts

set dialNumber to "{query}"
try
tell application "Safari"
activate
tell window 1
try
set current tab to tab "AirDroid Web | Manage your phone on web"
on error e
try
set current tab to tab "AirDroid"
on error e
set the clipboard to e as text
end try
end try
end tell

delay 2
say "here"
tell application "System Events"
tell process "Safari"
click at {1014, 106}
delay 1
say "here2"
click at {752, 397}
keystroke dialNumber
delay 1
click at {796, 635}
end tell
end tell
end tell
end try

I have set this osascript as my phone action.

I can hear "here1" and "here2" but no clicking is done. The same script run fine when run from Script Editor.app.

Edited by rounak
Link to comment

Hi,

 

What do the numbers in click at {796, 635} mean ?

I tried to use keystroke for some windows as well, even with Accessibility features enabled, it never worked. So I was wondering if I missed something and just found your post. Maybe this could help.

 

Regards

Edited by MEs
Link to comment

You can see those co-ordinates near your mouse cursor after pressing the relevant keyboard shortcut as listed in 

/System/Library/PreferencePanes/Keyboard.prefPane

>>Shortcuts

>>Screen Shots

 

 

That's a very handy trick. Would never have occurred to me to do that.

Link to comment

EDIT: Disregard; this is incorrect.

tell process "Safari"
    click at {100,100}
end tell
 You're telling an application to click on its own window. The coordinates refer to that application's active window, not the whole screen. Edited by deanishe
Link to comment

Maybe I am missing something here. How can you get those coordinates using the Cmd+Shift+4 shortcut ? Or maybe by selecting the window itself and "measuring" the place where we need it to click ?

 

EDIT : I did a quick test. The only way I made it work was by using the coordinate relative to the entire screen.

Edited by MEs
Link to comment

Hi rounak,

 

Thank you for your explanation.

Have you tried this with a second monitor plugged into your mac by any chance ? If yes, how do you tell which monitor to work on ?

 

Regards

 

No, never used a second monitor. 

Any details you can share about what you are trying to do?

(Stating the known: UI scripting should be the last resort). 

Link to comment

(Stating the known: UI scripting should be the last resort).

Yeah. It's slow and unreliable. Especially simulating mouse clicks (accessing the window's widgets is slightly better, but also very fragile).

If you're trying to automate a webpage, using Safari's do JavaScript to run your own JS on the page (much like a GreaseMonkey userscript) is a more robust solution. That way, you can access elements using CSS selectors, which is much more reliable.

 

In fact, rounak just posted an example of this.

Edited by deanishe
Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...