Jump to content
Macro Express Forums

Get mouse coordinates of current selection?


Recommended Posts

Apparently it's possible to select text in a macro if I know the mouse coordinates that I want.

 

Is it possible to get the coordinates of an already existing selection, so that the macro could re-select the same thing again on that same page of the document? Suppose that when the macro starts, certain text is already selected (as if you were going to copy it). The macro copies it. Then the user has to select a different tool, which the macro can accomplish by keyboard shortcut -- but after the tool is selected, the previous text selection is no longer selected (that's just the normal way that the application we're working in works). Is there any way for a macro to save information about the previous selection such that it could re-select the same thing?

 

 

Thanks.

Link to post
Share on other sites
Apparently it's possible to select text in a macro if I know the mouse coordinates that I want.

 

Is it possible to get the coordinates of an already existing selection, so that the macro could re-select the same thing again on that same page of the document? Suppose that when the macro starts, certain text is already selected (as if you were going to copy it). The macro copies it. Then the user has to select a different tool, which the macro can accomplish by keyboard shortcut -- but after the tool is selected, the previous text selection is no longer selected (that's just the normal way that the application we're working in works). Is there any way for a macro to save information about the previous selection such that it could re-select the same thing?

 

 

Thanks.

 

 

Meg

 

See attached macro. It will allow you to define the area to select. It waits for the mouse click at the beginning and then grabs the coordinates and then it waits for you to click at the end of the selection. it will then go back and highlight the text in between and then copy it to the clipboard. Have your macro then open the second tool and do whatever it needs with the text and then have it reopen the first tool and repeat the first part of the macro so it will automatically highlight the selection again.

 

Just a note, wait a half beat after clicking at the front and the end of the selection. If you click and move at the same time, it will grab a wrong coordinate.

 

 

Text_Selection_Macro.mex

Link to post
Share on other sites

This should work with a MS Word document and some others:

 

When your macro starts, data is already highlighted, so copy to clipboard and save in a text variable, T1.

 

To reproduce the highlighting later, the macro will:

Mouse click within the document screen to restore focus to the document.

Ctrl-Home to go to beginning of document, or mouse click at upper left corner of the screen to stay on the same page.

Ctrl-f or Alt-Edit-Find to open search box.

Type T1 into the search box, type ENTER to do the search.

At completion of the search, your text should again be highlighted.

Link to post
Share on other sites
This should work with a MS Word document and some others:

 

When your macro starts, data is already highlighted, so copy to clipboard and save in a text variable, T1.

 

To reproduce the highlighting later, the macro will:

Mouse click within the document screen to restore focus to the document.

Ctrl-Home to go to beginning of document, or mouse click at upper left corner of the screen to stay on the same page.

Ctrl-f or Alt-Edit-Find to open search box.

Type T1 into the search box, type ENTER to do the search.

At completion of the search, your text should again be highlighted.

 

 

Thats if it has a cursor, i was thinking about that and just having them do that, but then I got thinking.. what if their program doesn't have a cursor? :P

Link to post
Share on other sites

Another way, but probably too slow --

 

If you know the color of highlighted text, scan the screen to find the upper left and lower right positions of the color, and save the pixel positions.

 

Then to reproduce the highlighting later,

move mouse to upper left saved position,

mouse button down,

move mouse to lower right saved position,

mouse button up

 

To make this work fast, you would have to be fairly creative in the logic of the original scan, because to examine every pixel of the screen would just take too long. Maybe some sort of binary search to put you in the highlighted area, then sequential stepping backward and forward to find the limits.

 

If I judge Brain Virus correctly, he will have the logic written for you by tomorrow morning. :)

Link to post
Share on other sites
Meg

 

See attached macro. It will allow you to define the area to select. It waits for the mouse click at the beginning and then grabs the coordinates and then it waits for you to click at the end of the selection. it will then go back and highlight the text in between and then copy it to the clipboard. Have your macro then open the second tool and do whatever it needs with the text and then have it reopen the first tool and repeat the first part of the macro so it will automatically highlight the selection again.

 

Just a note, wait a half beat after clicking at the front and the end of the selection. If you click and move at the same time, it will grab a wrong coordinate.

 

 

Text_Selection_Macro.mex

Hi Brandon,

 

I tried this, but it seems to just not do anything. What I was hoping for was for the user to initiate the macro after they've selected the text (since this is the way the existing macro that I want to expand upon already works). As I suspected, this is probably not possible.

 

Thanks.

Link to post
Share on other sites
This should work with a MS Word document and some others:

 

When your macro starts, data is already highlighted, so copy to clipboard and save in a text variable, T1.

 

To reproduce the highlighting later, the macro will:

Mouse click within the document screen to restore focus to the document.

Ctrl-Home to go to beginning of document, or mouse click at upper left corner of the screen to stay on the same page.

Ctrl-f or Alt-Edit-Find to open search box.

Type T1 into the search box, type ENTER to do the search.

At completion of the search, your text should again be highlighted.

 

 

Hi rberq,

That would leave the text "highlighted" in the sense of being selected (as if you were going to copy). That's not what I'm trying to do.

 

In Adobe Acrobat, if you select text and copy it, then the text is still selected. But then if you click on the Highlighter tool (to make the text yellow), that text becomes deselected and the cursor no longer knows where it was. So you have to click on the Highlighter tool and then re-select the text if you want to make it yellow. The idea is to get the macro to both copy it and highlight it yellow without the user having to re-select the text. (In Word, this wouldn't be necessary -- in Word, when you select something, then copy it, then it's still selected, and you can click on the Highlight button to highlight it -- clicking on the Highlight button doesn't de-select it.)

 

If the cursor knew where it had been, I might be able to select with Shift-arrow combinations from that point by the number of characters equal to the length of the original selection, but as far as I can see, selecting text to highlight it yellow only works with the mouse.

Link to post
Share on other sites
Hi Brandon,

 

I tried this, but it seems to just not do anything. What I was hoping for was for the user to initiate the macro after they've selected the text (since this is the way the existing macro that I want to expand upon already works). As I suspected, this is probably not possible.

 

Thanks.

 

 

hrmm thats not good.

 

Try this.

Open up notepad and type some gibberish in it.

Activate the macro.

ALT Tab over to the notepad again. (dont click anywhere because the macro waits for a left mouse click to pull the first coordinate)

Left click at the beginning of the text (i.e. if you typed "gibberish" click just to the left of the "g")

pause for a second

Then left click at the end of selection.

the it should go and highlight the selection and then copy it and display a text box with the copied information.

 

 

Now, we should be able to integrate this into your current macro, how does the text on the first program get selected in the first place?

Link to post
Share on other sites

I think I've got an idea of how it SHOULD work, but the problem I'm having is with the get coordinates command. At one point in the macro it doesn't work, at another point it does.

 

the idea is:

 

copy the selection

save the length of the selection in variable

 

press right-arrow key (moves text cursor to end of where selection was)

move mouse to text cursor location

click mouse

get mouse coordinates

 

move left-arrow key the number of times = length of original selection

move mouse to text cursor location

click mouse

get mouse coordinates

 

subtract 2nd set of coordinates from 1st, save in New Variable

mouse left button down

mouse move position by the amount in New Variable

mouse left button up

 

The problem is, at the first time I try to get mouse coordinates, it gets 0,0. The second time, it gets coordinates, so I don't know why it wouldn't get them the first time. I've tested enough that I know the "move mouse to text cursor location" command is working.

Link to post
Share on other sites
hrmm thats not good.

 

Try this.

Open up notepad and type some gibberish in it.

Activate the macro.

ALT Tab over to the notepad again. (dont click anywhere because the macro waits for a left mouse click to pull the first coordinate)

Left click at the beginning of the text (i.e. if you typed "gibberish" click just to the left of the "g")

pause for a second

Then left click at the end of selection.

the it should go and highlight the selection and then copy it and display a text box with the copied information.

 

 

Now, we should be able to integrate this into your current macro, how does the text on the first program get selected in the first place?

Hi Brandon,

 

That sorta kinda works. The amount of text it selects is unpredictable. But I'm working on another idea, see the message I just posted. The text in the first program is selected by the user -- it has to be as it's unpredictable, depends on the user's judgment.

Link to post
Share on other sites

OK, more testing has shown that the get coordinates thing just doesn't really work in this application. I was getting 0,0 at one point because I had my variables mixed up. When I got them straightened out, I saw I was getting the same coordinates no matter where I was. I think that idea would be the way to go *IF* that command worked, but it's no go in this application.

 

There are a couple of similar applications I'll try the same thing in, though.

Link to post
Share on other sites

Very strange. It certainly seems that your solution is a good one. If the command MOVE MOUSE TO CURSOR POSITION is working, then GET MOUSE COORDINATES should certainly work.

 

You should not have to click the mouse before trying to get its coordinates. I wonder if it would help to get rid of that command? Maybe the application is doing something funny when the mouse is clicked. But if you are clicking to assure Windows returns focus to your application, then yes you may need the click.

 

After MOVE MOUSE TO CURSOR POSITION, try inserting a quarter-second or half-second delay, to give the application time to finish positioning the mouse before the macro continues.

 

Are you getting mouse coordinates relative to the screen, or relative to the window? Whichever it is that is failing, try switching it to the other.

 

Also, near the end of your logic:

subtract 2nd set of coordinates from 1st, save in New Variable

mouse left button down

mouse move position by the amount in New Variable

mouse left button up

Instead of stepping the mouse one character at a time to the end of the text, simply move it to the coordinates you originally saved after your PRESS RIGHT-ARROW KEY. Ditto for stepping the mouse backwards to get to the beginning of your text -- if you can find a trick like PRESS RIGHT-ARROW KEY to automatically position the cursor there (HOME key maybe???) it will be much faster than single-stepping the cursor.

Link to post
Share on other sites
Very strange. It certainly seems that your solution is a good one. If the command MOVE MOUSE TO CURSOR POSITION is working, then GET MOUSE COORDINATES should certainly work.

 

You should not have to click the mouse before trying to get its coordinates. I wonder if it would help to get rid of that command? Maybe the application is doing something funny when the mouse is clicked. But if you are clicking to assure Windows returns focus to your application, then yes you may need the click.

 

After MOVE MOUSE TO CURSOR POSITION, try inserting a quarter-second or half-second delay, to give the application time to finish positioning the mouse before the macro continues.

 

Are you getting mouse coordinates relative to the screen, or relative to the window? Whichever it is that is failing, try switching it to the other.

 

Also, near the end of your logic:

subtract 2nd set of coordinates from 1st, save in New Variable

mouse left button down

mouse move position by the amount in New Variable

mouse left button up

Instead of stepping the mouse one character at a time to the end of the text, simply move it to the coordinates you originally saved after your PRESS RIGHT-ARROW KEY. Ditto for stepping the mouse backwards to get to the beginning of your text -- if you can find a trick like PRESS RIGHT-ARROW KEY to automatically position the cursor there (HOME key maybe???) it will be much faster than single-stepping the cursor.

 

 

Hi rberq,

I tried taking out the mouse click and adding a delay. No difference. I tried a bigger delay (2 seconds), no difference.

 

As far as what you say near the end your message, I'm not quite following. I'm not sure what you mean by "stepping the mouse backwards". The idea is to use stepping backwards by keys in order to obtain:

 

#1 set of coordinates = where end of selection was

#2 set of coordinates = where beginning of selection was

 

at this point, the mouse is at #2, so we need to move it by the difference between #1 and #2. Supposing that #1 was 500,200, and #2 was 200,100, then if we're at 200,0, we need to move right 300,100.

 

This whole thing is about working with PDF files. So far, in Adobe, can't get coordinates (I get numbers but they are meaningless as they are always the same no matter where I am). In PDF X-Change Viewer, can't get coordinates. In Foxit Reader, I get the coordinates. It sounds as if you can't get coordinates in every application.

 

I'll try to attach a file with my macro.

 

OK, the first macro I uploaded is wrong. The 2nd and 3rd are corrected and are the same (couldn't figure out how to delete attachments, thought I had deleted them so I uploaded again).

 

THe good news is that the revised macro works in Foxit Reader -- the bad news is that it selects on the line above where the original selection was.

ExperimentingWithSelections.mex

Link to post
Share on other sites

I see what you mean about the coordinates being always the same. Apparently in Adobe there IS no "text cursor", so even though the MOVE MOUSE TO TEXT CURSOR command does not error out, the mouse coordinates returned seem to be always the upper left corner of the screen. Brandon's original idea of having the macro running before the user selects text MIGHT work, by capturing the mouse-button-down coordinates and then the mouse-button-up coordinates. But in practical everyday use it will probably fail due to timing constraints -- the user simply is not going to pause long enough at the begin and end points to allow the macro to capture the mouse coordinates. The second difficulty I see is, the user may highlight data that spans several screens by dragging the mouse down below the bottom of the screen while the text scrolls off the top. Then, capture of mouse coordinates relative to the screen becomes useless.

 

IF THE DATA SELECTED IS CONFINED TO A SINGLE SCREEN, WITH NO SCROLLING, then I still think what you want could be accomplished by scanning the screen for the selection color, finding where the color begins and ends, saving those coordinates, and using them to re-select (drag the mouse from beginning point to end point) after the highlighting tool has been invoked. The difficulty would be knowing what selection color to look for, adjusting for 16-bit vs. 24-bit vs. 32-bit color, and getting a scan routine that works fast enough.

Link to post
Share on other sites
I see what you mean about the coordinates being always the same. Apparently in Adobe there IS no "text cursor", so even though the MOVE MOUSE TO TEXT CURSOR command does not error out, the mouse coordinates returned seem to be always the upper left corner of the screen. Brandon's original idea of having the macro running before the user selects text MIGHT work, by capturing the mouse-button-down coordinates and then the mouse-button-up coordinates. But in practical everyday use it will probably fail due to timing constraints -- the user simply is not going to pause long enough at the begin and end points to allow the macro to capture the mouse coordinates. The second difficulty I see is, the user may highlight data that spans several screens by dragging the mouse down below the bottom of the screen while the text scrolls off the top. Then, capture of mouse coordinates relative to the screen becomes useless.

 

IF THE DATA SELECTED IS CONFINED TO A SINGLE SCREEN, WITH NO SCROLLING, then I still think what you want could be accomplished by scanning the screen for the selection color, finding where the color begins and ends, saving those coordinates, and using them to re-select (drag the mouse from beginning point to end point) after the highlighting tool has been invoked. The difficulty would be knowing what selection color to look for, adjusting for 16-bit vs. 24-bit vs. 32-bit color, and getting a scan routine that works fast enough.

 

hi Bob,

 

Yes, the data selected would be confined to a single screen. The user will be selecting only a word or phrase, most likely never more than about a line's worth at a time. The only exception would be if the desired word or phrase began on one page and ended on another. I've not looked into finding colors, but since the macros are intended to be used by different users on different systems, it seems as if the selection color could vary unpredictably. It's not an absolutely-must-have feature; it's a "would-be-extra-cool" feature that one user particularly wants. If only it could be done by keys instead of mouse, it'd be easy, but I can't change Acrobat or Reader! I have to finish up some other aspects of the macros now, and I'll look at this again later. Thanks for all your help.

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...