Skip to content

Instantly share code, notes, and snippets.

@sleekweasel

sleekweasel/README.md

Last active Jan 5, 2017
Embed
What would you like to do?
Videocapture applescript needs clickdrag.m to do its mouse clicking.

CLI video capture for iOS: the applescript uses the clickdrag tool to select the given window.

  • To start recording Simulator: osascript videocapture.scpt start Simulator 1 /Users/username/bin/clickdrag
  • To stop recording and save movie: osascript videoccapture.scpt stop /Users/username/Movies/somename
  • To stop recording without saving movie: osascript videocapture.scpt stop

Used by https://gist.github.com/sleekweasel/227315961b0b102e82d9 for automatic recording of test execution in cucumber.

Probably, if I renamed 'videocapture.scpt' to 'videocapture.applescript' and put a #!env osascript line at the start, it would be better.

// 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"];
int dx = [args integerForKey:@"dx"];
int dy = [args integerForKey:@"dy"];
// The data structure CGPoint represents a point in a two-dimensional
// coordinate system. Here, X and Y distance from upper left, in pixels.
//
CGPoint pt0;
pt0.x = x;
pt0.y = y;
CGPoint pt1;
pt1.x = x + dx;
pt1.y = y + dy;
CGPoint pt2;
pt2.x = x + dx / 2;
pt2.y = y + dy / 2;
// 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( pt0, TRUE, 1, FALSE );
CGPostMouseEvent( pt0, FALSE, 1, TRUE );
// End drag by moving to new location
CGPostMouseEvent( pt1, TRUE, 1, TRUE );
CGPostMouseEvent( pt1, FALSE, 1, FALSE );
sleep(1);
// Click middle
CGPostMouseEvent( pt2, TRUE, 1, TRUE );
CGPostMouseEvent( pt2, FALSE, 1, FALSE );
// Possible sleep routines
//usleep(100000);
//sleep(2);
[pool release];
return 0;
}
# To start recording Simulator: osascript videocapture.scpt start Simulator 1 /Users/username/bin/clickdrag
# To stop recording and save movie: osascript videoccapture.scpt stop /Users/username/Movies/somename
# To stop recording without saving movie: osascript videocapture.scpt stop
#
# It appears to be mandatory to save to Movies, or it won't save. I have no idea why.
on run argv
if item 1 of argv is equal to "start" then
set theProcessName to item 2 of argv
set theWindowNumber to item 3 of argv as integer
set clickdrag to item 4 of argv
tell application "System Events"
tell process theProcessName
activate
tell window theWindowNumber
set thePosition to position
set theSize to size
end tell
end tell
end tell
tell application "QuickTime Player"
quit
activate
new screen recording
delay 1
tell application "System Events" to key code 49
delay 1
do shell script clickdrag & " -x " & (item 1 of thePosition) & " -y " & (item 2 of thePosition) & " -dx " & (item 1 of theSize) & " -dy " & (item 2 of theSize)
end tell
else
tell application "QuickTime Player"
tell first document
stop
if (count of argv) = 2 then
close saving in item 2 of argv
else
close saving no
end if
#delay 1
# do shell script clickdrag & " -x 0 -y 0 -dx 0 -dy 0"
quit
end tell
end tell
end if
end run
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment