• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
2

How can I get current Mouse position via Javascript?

Explorer ,
Mar 25, 2016 Mar 25, 2016

Copy link to clipboard

Copied

I'm trying to build a javascript tool in ExtendScript Toolkit CC to work with Illustrator CC (2015) but have run into a snag.

The script will create an object (a small circle in this case) at the mouse's current position within the document's active layer. But I can't seem to find a call in the library to retrieve the location info. I know that the data is tracked because if I open the "Window > Info" tool it shows the cursor's X and Y position in relation to the document bounds.

Is there a call anywhere in the javascript library that gives me access to these data points?

TOPICS
Scripting

Views

5.5K

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe
Guide ,
Mar 26, 2017 Mar 26, 2017

Copy link to clipboard

Copied

unfortunately this is not possible with a simple ExtendScript.

I am not sure why you need the small circle at the cursor location.

will it be replaced with something else later?

The way I would tackle something like this is to have a small Symbol, ie, a cross hair, or a small circle.

I would then place this as a marker in the desired locations.

then you can use a script to replace each instance with something else...

There is a third party scripting app that has built in mouse commands such as mouseup, and mouseclick etc...

I think its called scriptographer.

that may help you, I have not used it...

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Nov 22, 2017 Nov 22, 2017

Copy link to clipboard

Copied

I know this has been a while since you posted but wondered if this might help you as a work around in some way?

Just throwing it out there for you and maybe others for that matter....

// Select an item and run the script to find x y

for (var i = 0; i < app.selection.length; i++) {

alert(app.selection.position);

}

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Valorous Hero ,
Nov 22, 2017 Nov 22, 2017

Copy link to clipboard

Copied

The person wanted to place an item at the cursor of the mouse, which I'm not sure if that's possible with the CEP extensions -but I'll ask around there to see if there's an event for it.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Nov 24, 2017 Nov 24, 2017

Copy link to clipboard

Copied

here is the location of a site which offers a free plugin which can handle mouse events.

I am not familiar with it but i bookmarked it for myself for future use. Perhaps this would work for you.

https://scriptographer.org/tutorials/interaction/creating-mouse-tools/

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Dec 20, 2017 Dec 20, 2017

Copy link to clipboard

Copied

You can get it from ScriptUI mouse event. 

If you are a Mac user, you can get it more easily with AppleScript / JXA and ObjC. 

(function() {

  var doc = app.documents[0] ;

  var clickEvent = mousePosition(doc) ;

  var r = 50 ;

  var d = r * 2 ;

  if(clickEvent.event.altKey) {

    doc.activeLayer.pathItems.ellipse(clickEvent.y + r, clickEvent.x - r, d, d) ;

  } else {

    doc.activeLayer.pathItems.ellipse(clickEvent.y, clickEvent.x, d, d) ;

  }

})() ;

function mousePosition(doc) {

  // A function for get main screen

  var mainScreen = function() {

    var screenArray = $.screens ;

    var res ;

    for(var i = 0, len = screenArray.length ; i < len ; i++) {

      if(screenArray.primary) {

        res = screenArray ;

        break ;

      }

    }

    return res ;

  } ;

 

  // Store active view

  var currentView = doc.activeView ;

  var oldBounds = currentView.bounds ;

  var defaultScreenMode = currentView.screenMode ;

  var zoomValue = currentView.zoom ;

 

  // Calculate offset of ruler

  var menuStr = 'ruler' ;

  app.executeMenuCommand(menuStr) ;

  var newBounds = currentView.bounds ;

  app.executeMenuCommand(menuStr) ;

  var gap = [oldBounds[0] - newBounds[0], oldBounds[1] - newBounds[1]] ;

 

  // Simplify calculation

  currentView.screenMode = ScreenMode.FULLSCREEN ;

 

  // Get mouse position on main screen.  You have to click somewhere

  var scrn = mainScreen() ;

  var dlg = new Window('''dialog {

bounds:[0, 0, ''' + scrn.right + ', ' + scrn.bottom + '''],

borderless:true,

opacity:0.4

}''') ;

  var mouseEvent ;

  dlg.addEventListener('click', function(ev) {

    mouseEvent = {

      screenX : ev.screenX,

      screenY : ev.screenY,

      metaKey : ev.metaKey, // When true, Command Key was active

      shiftKey : ev.shiftKey, //When true,  Shift Key was active

      altKey : ev.altKey, // When true,  Option Key was active

      ctrlKey : ev.ctrlKey // When true,  Control Key was active

    } ;

    dlg.close() ;

  }) ;

 

  dlg.show() ;

  if(!mouseEvent) {return ;}

 

  // Convert to Illustrator position

  var pos_x = oldBounds[0] + (mouseEvent.screenX / zoomValue) ;

  var pos_y = oldBounds[1] - (mouseEvent.screenY / zoomValue) ;

  pos_x -= gap[0] ;

  pos_y -= gap[1] ;

 

  // Restore active view

  currentView.screenMode = defaultScreenMode ;

 

  return {x : pos_x, y : pos_y, zoom : zoomValue, event : mouseEvent} ;

}

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Valorous Hero ,
Dec 20, 2017 Dec 20, 2017

Copy link to clipboard

Copied

Thank you for providing this solution! Unfortunately on my Win7 workstation which has 2 horizontal monitors, the dialog does not get dismissed with a mouse click

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Feb 23, 2022 Feb 23, 2022

Copy link to clipboard

Copied

LATEST

Hi,

 

I am trying to get something similar to work.

My setup is a laptop with an attached work monitor.

The screens are laptop, starting at left 0, primary and work, starting at width of laptop.

So I changed the script to use ScreenArray[1] regardless of primary, and positioned the dialog at the screen boundaries (with small margins set apart to verify placement)

Next, CS supported click on the dialog itself. CC no longer does.I just added a group to to the dialog and specified its bounds to completely cover the dialog. This works fine

Next I need to find out whether I can determine the window location if not running fullscreen

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Dec 20, 2017 Dec 20, 2017

Copy link to clipboard

Copied

it didn't work here in my laptop either. The dialog is not covering the main screen and nothing happens when I click. Sounds like an interesting approach.

xyPos.png

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Dec 21, 2017 Dec 21, 2017

Copy link to clipboard

Copied

I’m sorry but this script is for CS6 Mac and single screen. 

It works like this. 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines