Mouse position: Difference between revisions

Simplified version of the previously submitted solution
(explain more)
(Simplified version of the previously submitted solution)
Line 145:
 
=={{header|AppleScript}}==
TheSystem windowEvents can bereport anythe windowposition onof the system,currently externallyactive created or otherwise, provided it can acquire focuswindow. MouseThe mouse's location is retrieved with help from AppleScriptObjC. This will be relative to the bottom-left corner of the screen. Therefore, we also retrieve the screen's height.
 
<lang AppleScript>use applicationframework "System EventsAppKit"
 
tell application id "com.apple.SystemEvents" to tell ¬
property process : a reference to (first process whose frontmost = true)
(the first process where it is frontmost) to ¬
property window : a reference to front window of my process
set [Wx{x, Wy]y} to mythe front window's position
 
propertytell this : a reference tothe current application
set H to NSHeight(its NSScreen's mainScreen's frame)
set tell mouseLoc[] to& its (NSEvent's mouseLocation as record)
set item 1 to (item 1) - x
set item 2 to H - (item 2) - y
set coords to it as point
end tell
end tell
 
log coords</lang>
set [Wx, Wy] to my window's position
set [Cx, Cy] to {x, y} of the mouse's coordinates()
 
{{out}}
 
{187, 171}
script mouse
use framework "Foundation"
property this : a reference to current application
property NSEvent : a reference to NSEvent of this
property NSScreen : a reference to NSScreen of this
on coordinates()
-- Screen dimensions
set display to NSDeviceSize of deviceDescription() ¬
of item 1 of NSScreen's screens() as record
-- Mouse position relative to bottom-left of screen
set mouseLoc to (NSEvent's mouseLocation as record)
-- Flip mouse y-coordinate so it's relative to top of screen
set mouseLoc's y to (display's height) - (mouseLoc's y)
mouseLoc
end coordinates
end script
</lang>
 
{{out}}<lang AppleScript>-- Cursor position relative to active window
[Cx - Wx as integer, Cy - Wy as integer]</lang>
 
=={{header|AutoHotkey}}==
Anonymous user