The goal was to obtain the code that allows text selection to continue in a script so that it could be used as a voice command (the default commands offered by MacOS for selecting words or paragraphs are an end in themselves and cannot be integrated into an AppleScript voice command).
On the other hand, the reference point you use is the cursor, not the mouse pointer (which is what I’m really interested in).
The following code stopped working some time ago.
use AppleScript version "2.4" --max version macOS 10.12
use scripting additions
use framework "Foundation"
use framework "Quartz"
property updateMouseCursorPosition : true
property buttonCount : 1 -- left mouse button number
property mouseButtonDown : true -- mouse button pressed?
--delay 3 --
set aPoint to getCurrentMousePosition() -- get current cursor position on screen
repeat 3 times
mouseLeftClick at aPoint
0.05
end repeat
on getCurrentMousePosition()
-- 2 Getting the screen resolution and the bottom bound
tell application "Finder" to set screen_resolution to bounds of window of desktop
set theBottom to item 4 of screen_resolution
-- Getting current mouse position {0,0} is top left corner of screen
set currentMousePosition to (current application's NSEvent's (mouseLocation)) as record
set x to currentMousePosition's x as integer
set y to theBottom - (currentMousePosition's y) as integer
return {x, y}
end getCurrentMousePosition
on mouseLeftClick at aPoint
current application's CGPostMouseEvent(aPoint, updateMouseCursorPosition, buttonCount, mouseButtonDown)
current application's CGPostMouseEvent(aPoint, updateMouseCursorPosition, buttonCount, not mouseButtonDown)
end mouseLeftClick
⇒ I found a solution through Claude from Anthropic that I want to share with you ⇐
–Copy/Cut/Overwrite word/paragraph located under the mouse pointer
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"
set screenHeight to do shell script "system_profiler SPDisplaysDataType | awk '/UI Looks like/{print $6; exit}'"
set currentMousePosition to (current application's NSEvent's mouseLocation) as record
set x to currentMousePosition's x as integer
set y to (screenHeight as integer) - (currentMousePosition's y as integer)
--do shell script "/usr/local/bin/clic " & x & " " & y & " 2" -- Select word
do shell script "/usr/local/bin/clic " & x & " " & y & " 3" -- Select paragraph
delay 0.5 -- 1 -- ???
tell application "System Events" to set frontApp to name of first application process whose frontmost is true
tell application frontApp to activate
delay 0.2
tell application "System Events" to keystroke "c" using command down -- copy
--tell application "System Events" to keystroke "x" using command down -- cut
--tell application "System Events" to keystroke "v" using command down -- overwrite with clipboard
The code for clic.c (in /usr/local/bin) is as follows: (unfortunately, it is not written in Apple’s own language)
(Granting Accessibility permissions)
#include <ApplicationServices/ApplicationServices.h>
#include <unistd.h>
int main(int argc, char *argv[]) {
if (argc != 4) return 1;
float x = atof(argv[1]);
float y = atof(argv[2]);
int clicks = atoi(argv[3]);
CGPoint point = CGPointMake(x, y);
for (int i = 1; i <= clicks; i++) {
CGEventRef down = CGEventCreateMouseEvent(NULL, kCGEventLeftMouseDown, point, kCGMouseButtonLeft);
CGEventRef up = CGEventCreateMouseEvent(NULL, kCGEventLeftMouseUp, point, kCGMouseButtonLeft);
CGEventSetIntegerValueField(down, kCGMouseEventClickState, i);
CGEventSetIntegerValueField(up, kCGMouseEventClickState, i);
CGEventPost(kCGHIDEventTap, down);
CGEventPost(kCGHIDEventTap, up);
CFRelease(down);
CFRelease(up);
usleep(50000);
}
return 0;
}