Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

AppleScript Text Insertion at Cursor

I would like to get my AppleScript to insert a textual comment at the current cursor position in a Word document. The comments are mostly longer (multi-lines) than what Word AutoText allows. Automator has insertion of text at the beginning and end of the document. But, I would like the text to be at the cursor position. In reviewing the AppleScript documentation, I cannot determine how AppleScript identifies or inserts text at the cursor position. Any suggestions?

Mac Pro, MacBook Pro, MacMini Server(10.8.2)

Posted on Dec 7, 2012 10:31 AM

Reply
6 replies

Jun 28, 2017 10:29 AM in response to sysx

Send the string to the Clipboard and then use the keystroke method to paste. Here's an example using JXA (Javascript) to insert a timeStamp in any application:


function run() {

const appSE = Application('System Events');


appSE.includeStandardAdditions = true;


// Create a date object with the current time

const now = new Date();


// takes a number and returns a minimum two-digit string

function leadingZero(num) {

return num < 10 ? "0" + num : num.toString();

}


// Get full Year, month and day of the month

const Ymd = [

now.getFullYear(),

leadingZero(now.getMonth() + 1),

leadingZero(now.getDate())

];



// convert array to string and concat with 24 hour time and local time-zone

const timeStamp = Ymd.join("-") + " " + now.toLocaleTimeString();


// copy to clipboard

appSE.setTheClipboardTo(timeStamp);

// paste at cursor prompt with "Cmd-V"

appSE.keystroke('v', { using: 'command down' });

}

AppleScript Text Insertion at Cursor

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.