Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!


Click here to return to the 'Move and click the mouse via code' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Move and click the mouse via code
Authored by: rccharles on Feb 10, '10 12:05:45PM
I have developed a drag routine. I called it clickdrag>

// File: 
// clickdrag.m
//
// clickdrag will drag the "mouse" from the current cursor position to
// the given coordinates.
//
// derived from:
//  http://www.macosxhints.com/article.php?story=2008051406323031
//    &
//  http://davehope.co.uk/Blog/osx-click-the-mouse-via-code/
//
// Requires:
//   xtools
//
// Compile with: 
// gcc -o clickdrag clickdrag.m -framework ApplicationServices -framework Foundation
//
// Usage:
// ./clickdrag -x pixels -y pixels
//



#import <Foundation/Foundation.h>
#import <ApplicationServices/ApplicationServices.h>


int main(int argc, char *argv[]) {
  NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
  NSUserDefaults *args = [NSUserDefaults standardUserDefaults];


  // grabs command line arguments -x and -y
  //
  int x = [args integerForKey:@"x"];
  int y = [args integerForKey:@"y"];


  // The data structure CGPoint represents a point in a two-dimensional
  // coordinate system.  Here, X and Y distance from upper left, in pixels.
  //
  CGPoint pt;
  pt.x = x;
  pt.y = y;

  // This is where the magic happens.  See CGRemoteOperation.h for details.

  // Get the current mouse location
  CGEventRef ourEvent = CGEventCreate(NULL);
  CGPoint ourLoc = CGEventGetLocation(ourEvent);
  
  // CGPostMouseEvent( CGPoint        mouseCursorPosition,
  //                   boolean_t      updateMouseCursorPosition,
  //                   CGButtonCount  buttonCount,
  //                   boolean_t      mouseButtonDown, ... )
  //
  
  
  

  // Start drag from current cursor location
  CGPostMouseEvent( ourLoc, TRUE, 1, FALSE );
  CGPostMouseEvent( ourLoc, FALSE, 1, TRUE );

  // End drag by moving to new location
  CGPostMouseEvent( pt, TRUE, 1, TRUE );
  CGPostMouseEvent( pt, FALSE, 1, FALSE );
  
  // Possible sleep routines
  //usleep(100000);
  //sleep(2);

  [pool release];
  return 0;
}
Here is the applescript code to test clickdrag.

(* 
 Required:
  Paintbrush
  http://paintbrush.sourceforge.net/downloads/
  
  Optional:
     Play Sound
   	http://microcosmsoftware.com/playsound/
  
     the XTools scripting addition gives AppleScript the ability to move the mouse. 
	
	http://www.lestang.org/osax/XTool/
	XTool-2.0-src.dmg.tgz 
*)
on run
	
	-- Write a message into the event log.
	log "  --- Starting on " & ((current date) as string) & " --- "
	
	
	(* 
	Play Sound
   	http://microcosmsoftware.com/playsound/
	*)
	try
		set theLoc to ((path to system folder) as string) & "Library:Sounds:Funk.aiff"
		tell application "Play Sound"
			play theLoc repeat 4
		end tell
	end try
	
	tell application "Paintbrush"
		activate
	end tell
	delay 10
	
	-- Click on return for confirmation of painting size.
	try
		set see to "/Users/mac/clickcode/click -x 432 -y 242"
		log "see= " & see
		set results to do shell script see
	on error theError
		display dialog "click error 1 results for " & theError
	end try
	
	delay 3
	
	-- Draw 5 lines with pencil.  Assume pencil icon is clicked.
	repeat with i from 100 to 500 by 100
		try
			set see to "/Users/mac/clickcode/click -x 200 -y " & i
			log "see= " & see
			set results to do shell script see
		on error theError
			display dialog "click error 2 results for " & theError
		end try
		
		try
			set see to "/Users/mac/clickcode/clickdrag -x 579 -y 365"
			log "see= " & see
			set results to do shell script see
		on error theError
			display dialog "clickdrag error results for " & theError
		end try
	end repeat
end run


Edited on Feb 10, '10 12:10:08PM by rccharles


[ Reply to This | # ]
Move and click the mouse via code
Authored by: buzzwig on Feb 01, '12 04:35:53PM

Hi. I'd like to use a command line tool like this to drag by calling from AppleScript. But I don't know how to compile, and my attempts have been frustrating and have failed. Is there a compiled command line tool that I can use to click and drag? I use cliclick and mousetools, but they don't allow dragging, that I'm aware of. It would be wonderful to find a tool that provides this! Better yet, a tool that does the stuff the others do, but also drags, so I wouldn't have to have a multitude of little command line tools stacked up in my system. Whoever manages to accomplish this will be my hero!



[ Reply to This | # ]