detecting a mouseclick globally?

I’m having trouble writing a script that sits and waits for a mouse click, executes a command, and then quits. I would think a simple do-while loop with an “on mousedown” line would do the trick, but I can’t quite get the syntax right. Applescript doesn’t seem to like “on some_event” handlers inside of repeat loops, and, more importantly, I can’t figure out what arguments to feed the On Mousedown event. It seems to want an “into”, “given”, or “with” statement, but I can’t find any documentation on what those might be.
Oh, and to clarify, this mouseclick would be at a global level, rather than belonging to a specific window or application.
Thanks for any help!

Hi,

System Events simulates mouse clicks and does not detect them. I don’t know of any scripting addition or app that can do this in OSX. Akua Sweets could do this pre-OSX. So, currently, you can only detect mouse clicks in the app that is programmed to do so.

One thing you can do is detect when the frontmost process changes. For instance, if a user clicks in the window of another process, you can compare a stored process with the current one. Here’s a script that I use to bring all windows of an app to the front:

I must have trown it away, or maybe it didn’t work.

brb,

Here’s an example fo the process thing:

global front_proc_name

on run
tell application “System Events”
set front_proc_name to name of (first process whose frontmost is true)
end tell
end run

on idle
tell application “System Events”
set cur_proc to (first process whose frontmost is true)
set cur_proc_name to name of cur_proc
end tell
if cur_proc_name is not front_proc_name then
set front_proc_name to cur_proc_name
tell application front_proc_name to activate
end if
return 1
end idle

The thing that was wrong with it is speed. You have to wait 1 second so it’s inconsistant. Sometimes it jumps back to the last process when switching processes by clicking on the window and also takes a while to bring all windows of a process to the front.

gl,

Hey that’s kind of a neat script. I can see where it might make problems for my particular task, but it’s good to keep in mind.

I guess that what I need really is a bigger gun. I might ask a friend who’s good with C if he can help me out. Otherwise, I guess I’ll just live with what I’ve got. Thanks again for your help. This has been a lot of fun.

Have you considered wrapping your script inside a realBasic gui and use RB to detect the mouse clicks?

RealBasic is another whole level of sophistication for me. I mean, I’m sure it’s simple enough, but for now I think this is where I get off the train.

I recognize that I’ll have to learn it sooner or later, so I’ll most likely be back, but for now I’ve been more than taken care of.

You guys rock,
thank you 5x,
David