Trapping cursor coordinates in Javascript
Copy link to clipboard
Copied
Hello Everyone:
I am attempting to trap cursor coordinates but I do not know how to make that happen. Here is the short algorithm I hope to implement:
click object to select.
click new location for object to move to
move object to new location.
I think I know how to do the first, but I have no idea how to make Javascript report the coordinates of the mouse cursor when I click on the new location.
Please share with me any thoughts/documentation/snippets that might help. I will greatly appreciate it.
TIA.
John
Copy link to clipboard
Copied
@John – if you are using InDesign CS5 (or above) I have something for you.
At least a proof of concept that will work. Tested with InDesign CS5.5.
The script will not read out any cursor coordinates.
Unfortunately we cannot do this…
Instead I am using:
one event listener,
an JPEG image already placed,
the placeGun
and the "afterSelectionChanged" event.
To test the script below, you need:
1.A The ESTK open, run the script from that
OR:
1.B Run the script with a keyboard shortcut from InDesign's Scripts Panel
2. An open document
3. An already placed JPEG image in the document (with a link status OK)
4. One single page item selected (no text selected, not more than one object selected)
This is just a proof of concept!
I did not consider moving a selection greater than one object, I needed at least ONE image placed in the document, I did not consider double sided spreads with a coordiante system set by page.
And: I really had fun writing this!
Here the script (ExtendScript/JavaScript):
//MoveSelectionTo_POSITION-OF-CURSOR_ProofOfConcept.jsx
//Uwe Laubender
//DESCRIPTION:Just a proof of concept. Some pieces missing!
#targetengine "MovePositionToCursor[Proof Of Concept ONLY]"
app.scriptPreferences.userInteractionLevel = UserInteractionLevels.interactWithAll;
//SOME CHECKS:
if(parseFloat(app.version)<7){alert("Does not run in InDesign v. "+app.version);exit()};
if(app.documents.length == 0){alert("No document open!");exit()};
if(app.selection.length !== 1 && !app.selection[0].hasOwnProperty("baselineShift")){
alert("This is a proof of concept! Does ONLY work with ONE page item selected. NOT TEXT!");
exit();
};
if(app.documents[0].allGraphics.length == 0){
alert("This is a proof of concept! Does ONLY work with AT LEAST ONE JPEG ALREADY placed!");
exit();
};
moveSelection();
function moveSelection(){
var myDoc = app.documents[0];
var myOriginalSel = app.selection[0];
var myOriginalSelID = myOriginalSel.id;
for(var n=0;n<myDoc.links.length;n++){
if(myDoc.links
.status == LinkStatus.NORMAL && myDoc.links .linkType == "JPEG"){ var myImage = File(myDoc.links
.filePath); break;
};
};
if(myImage){myDoc.placeGuns.loadPlaceGun(myImage)}
else{
alert("This is a proof of concept! Does ONLY work with AT LEAST ONE JPEG ALREADY placed!");
exit();
};
app.addEventListener("afterSelectionChanged", doSomething);
app.select(app.documents[0].pageItems.itemByID(myOriginalSelID));
function doSomething(myEvent){
try{
app.removeEventListener("afterSelectionChanged", doSomething);
var myNewID = app.selection[0].id;
var myGeoBounds = app.selection[0].getElements()[0].geometricBounds;
app.documents[0].pageItems.itemByID(myNewID).remove();
app.documents[0].pageItems.itemByID(myOriginalSelID).move([myGeoBounds[1],myGeoBounds[0]]);
app.select(app.documents[0].pageItems.itemByID(myOriginalSelID));
}catch(e){};
};
};
Uwe
Copy link to clipboard
Copied
Oh. I forgot something 😉
To move the selection, the user has to click the cursor after executing the script.
Uwe
Copy link to clipboard
Copied
Uwe:
Unfortunately, your suggested process even in its seminal stage was not something I could use. However, your thought process did spawn a thought process that led to an eventual solution that ultimately did what I was looking for. So I still owe you loads of thanks and if you are ever in Brookfield Missouri, I promise to take you to lunch. We have a number of fine eating establishments in the area, such as McDonalds, Sonic (clone of McDonalds) and Pizza Hut. I promise you they are fastidiously clean, though they are a little unimaginative on the menu side (grin).
Take care and thanks for your input.
R,
John

