Tuesday, December 6, 2022

Announcement

MacScripeter.net will transition to a new Discourse server soon. Watch this space for the date and time of the transition. Please expect the site to experience periods of outage during this transition. See this and this for more details.

#1 2010-07-04 09:57:55 am

danwan
Member
Registered: 2008-03-21
Posts: 215

Mouse Position on the screen with a trick

HI all,

since holding down apple-shift-4 changes the cursor to display the position on the screen is it possible to copy the coordinates to the clipboard to use elsewhere?

For example to tell another script where to position a window next to another.

Using System events as

tell application "System Events"
    keystroke "4" using using {command down, shift down}
end tell

doesnt return anything or it appears and disappear so fast to make it useless ...


Thanks

Last edited by danwan (2010-07-04 10:00:14 am)

Offline

 

#2 2010-07-12 09:01:21 pm

JaiM
Member
Registered: 2009-04-22
Posts: 24

Re: Mouse Position on the screen with a trick

Hi DanWan,

I am not sure, what you are trying to achieve. If your intent is to get the coords of the mouse. You can always do this:

Applescript:

tell application "System Events"
   set mousePosition to position of the mouse
end tell

Hope it helps

Rgds
JaiM

Offline

 

#3 2010-07-13 04:09:50 am

regulus6633
Member
From:: Taulov, Denmark
Registered: 2006-11-02
Posts: 1695
Website

Re: Mouse Position on the screen with a trick

This can be done using cocoa, so if you have the developer tools installed, you can create a command line tool to return the coordinates to applescript. Do this...
1) create a text file on your desktop called MouseLocation.m with these contents

Applescript:

#import <AppKit/AppKit.h>

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

NSPoint mouseLoc = [NSEvent mouseLocation]; //get current mouse position
   NSString* locString = [NSString stringWithFormat:@"%.3f, %.3f", mouseLoc.x,mouseLoc.y];
   printf("%s\n", [locString UTF8String]);
   
[pool drain];
return 0;
}

2) open the Terminal and cd to the desktop, then enter this command

Applescript:

gcc -o MouseLocation MouseLocation.m -framework AppKit

3) you can delete the MouseLocation.m file
4) in applescript you can use the command line tool like this

Applescript:

set unixExe to (path to desktop folder as text) & "MouseLocation"
do shell script quoted form of POSIX path of unixExe

Last edited by regulus6633 (2010-07-13 04:15:36 am)

Offline

 

#4 2010-08-16 07:10:12 am

McUsr
Member
From:: Southern Norway
Registered: 2010-04-07
Posts: 1776

Re: Mouse Position on the screen with a trick

Hello.

I was so tired of having to type stuff like applescript text item's delimiters from AS Editor, so I thought I could have a script installed in its contextual menu to do that for me. Then I realized that there were no keyboard shortcut to invoke the contextual menu with.

First of all I looked for something that could actually make the click with the control key. I found cliclick by Carsten Blühm which can be downloaded from here.

Then I discovered this post to obtain the mouse position. Regulus solution works wonderful, I just did a minor change in order to get more "integer like coordinates.

[code]#import <AppKit/AppKit.h>
const char origin[] = "http://www.macscripter.net/viewtopic.php?id=33468 Regulus6633" ;

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

NSPoint mouseLoc = [NSEvent mouseLocation]; //get current mouse position
NSString* locString = [NSString stringWithFormat:@"%.0f, %.0f", mouseLoc.x,mouseLoc.y];
printf("%s\n", [locString UTF8String]);

[pool drain];
return 0;
}[/code]
Then I created this AppleScript, which I have installed as a short key cut with Spark which can be downloaded from here

You will have to change the hardcoded paths to what suits your installation policy.

Applescript:


-- macscripter.net/viewtopic.php?pid=132022#p132022 McUsr
set mouseLoc to (do shell script "/usr/local/opt/MouseLocation")
set {astid, AppleScript's text item delimiters} to {AppleScript's text item delimiters, ","}
tell mouseLoc to set {mouseX, mouseY} to {"c" & it's text item 1, it's text item 2}
set mouseY to 1200 - (mouseY as integer)
set mouseLoc to mouseX & " " & mouseY
do shell script "/usr/local/opt/cliclick " & mouseLoc
set AppleScript's text item delimiters to astid

I have assigned it to ctrl opt cmd space Since all other combinations with space is taken. Logically it should really have been put on ctrl space, as you simulate a mouse click with a space, but there may be both quicksilver and spotlight on these places so…


Edit: It appears to be some discrepancies with the mouse position given of the mouse location returned by MouseLocation, so I wonder What I will have to do to adjust the geometry, and if this is possible.
It looks like the Y coordinate is put some 400 pixels below its original position, this may have something to do with the short cut I use, as it changes to crosshairs. I'll update this when I have investigated it more thoroughly

Last edited by McUsr (2010-08-17 10:47:26 am)


Mercurial vcs is a joy to use for scripting.


Filed under: Contextual Menu

Offline

 

#5 2010-08-16 07:26:43 am

regulus6633
Member
From:: Taulov, Denmark
Registered: 2006-11-02
Posts: 1695
Website

Re: Mouse Position on the screen with a trick

Hi McUsr, I'm glad you got some use out of it. cool
Note, I posted MouseLocation on my website to make it easier for people... it's already compiled... anyone can get it from here.

Offline

 

#6 2010-08-16 07:43:08 am

McUsr
Member
From:: Southern Norway
Registered: 2010-04-07
Posts: 1776

Re: Mouse Position on the screen with a trick

Hello Hank, it is cool. cool
Thanks!

The problem I had with the coordinates and the script, was that using opt made the editor use block editing mode, aka the crosshair mouse cursor. The other problem was that the position is measured from the bottom for the y coordinate.

I have hardcoded my screen resolution into the script, which has a depth of 1200 pixels.
It is as such just a hack, but a useful hack if you'd like to invoke the contextual menu from the keyboard. smile
Now I use shift cmd space to invoke it.

Applescript:


set mouseLoc to (do shell script "/usr/local/opt/MouseLocation")
set {astid, AppleScript's text item delimiters} to {AppleScript's text item delimiters, ","}
tell mouseLoc to set {mouseX, mouseY} to {"c" & it's text item 1, it's text item 2}
set mouseY to 1200 - (mouseY as integer)
set mouseLoc to mouseX & " " & mouseY
do shell script "/usr/local/opt/cliclick " & mouseLoc
set AppleScript's text item delimiters to astid

You will need cliclick and MouseLocation, and some shortcut utility, all which are referenced in the posts above in order to make it to work.

Last edited by McUsr (2010-08-16 09:05:29 am)


Mercurial vcs is a joy to use for scripting.


Filed under: Contextual Menu

Offline

 

#7 2010-08-16 08:01:54 am

regulus6633
Member
From:: Taulov, Denmark
Registered: 2006-11-02
Posts: 1695
Website

Re: Mouse Position on the screen with a trick

McUsr wrote:

The other problem was that the position is measured from the bottom for the y coordinate.


FYI: I know about this. Actually I'm working on another tool called MouseTools which allows you to do many things with the mouse, including getting mouse location, clicking at a location, clicking with modifier keys and a few other things. It's mostly done but needs a few more minor adjustments.

The reason I mention that is I have code in the new tool for getting the mouse location from the upper left corner instead of the bottom left. You can have that now if you wish. Here's that code so just replace the code and rebuild the tool.
Replace...

Applescript:

NSPoint mouseLoc = [NSEvent mouseLocation]; //get current mouse position
NSString* locString = [NSString stringWithFormat:@"%.0f, %.0f", mouseLoc.x,mouseLoc.y];
printf("%s\n", [locString UTF8String]);

With...

Applescript:

CGEventRef mouseEvent = CGEventCreate(NULL);
CGPoint mouseLoc = CGEventGetLocation(mouseEvent);
NSString* locString = [NSString stringWithFormat:@"%.0f, %.0f", (float)mouseLoc.x, (float)mouseLoc.y];
printf("%s\n", [locString UTF8String]);
CFRelease(mouseEvent);

NOTE: in your applescript you never reset text item delimiters to astid.

Last edited by regulus6633 (2010-08-16 08:02:38 am)

Offline

 

#8 2010-08-16 09:11:48 am

McUsr
Member
From:: Southern Norway
Registered: 2010-04-07
Posts: 1776

Re: Mouse Position on the screen with a trick

Hello Hank.

Corrected the code to reset AppleScript text item delimiters, Thanks.

I have to think about using the new tool, I really think you should have a switch for that, for System events's windows for instance uses positions relative to the bottom of the screen, where as  Finder uses positions from the upper left, at least it is so in Snow Leopard.


Mercurial vcs is a joy to use for scripting.


Filed under: contextMenu

Offline

 

#9 2010-08-16 09:13:27 am

regulus6633
Member
From:: Taulov, Denmark
Registered: 2006-11-02
Posts: 1695
Website

Re: Mouse Position on the screen with a trick

McUsr wrote:

Hello Hank.

Corrected the code to reset AppleScript text item delimiters, Thanks.

I have to think about using the new tool, I really think you should have a switch for that, for System events's windows for instance uses positions relative to the bottom of the screen, where as  Finder uses positions from the upper left, at least it is so in Snow Leopard.


The switch is in the new tool so there will be one! big_smile

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)