How do i select a paragraph of text with AppleScript?

I have text in Notes.app. I want to place my cursor (arrow-key or mouse click) in a paragraph and then run an applescript that SELECTS the entire paragraph.

How can I do this with AppleScript? Is the script any different if I need to deal with one bulleted line in a bulleted list?

All my searches have turned up only people wanting to do someting with text that they’ve selected. I want the AppleScript to do the selection part for me.

Much thanks,

John

There is no object paragraph defined in the dictionary, just the body of a note.

To get what is selected, I think that you will have to use GUI scripting to copy what is selected.
Then you will be able to retrieve what is in the clipboard in the note’s body
Here is a quick and dirty draft :

use scripting additions

tell application "Notes"
	activate
	tell application "System Events" to tell process "Notes"
		keystroke "c" using {command down}
	end tell
	delay 0.2
	tell me
		set wasSelected to the clipboard as text
	end tell
	set htmlBody to item 1 of (get body of notes)
	set htmlBody to my remplace(htmlBody, wasSelected, "new text to insert")
	set body of notes to {htmlBody}
end tell
#=====
(*
replaces every occurences of d1 by d2 in the text t
*)
on remplace(t, d1, d2)
	local oTIDs, l
	set {oTIDs, AppleScript's text item delimiters} to {AppleScript's text item delimiters, d1}
	set l to text items of t
	set AppleScript's text item delimiters to d2
	set t to "" & l
	set AppleScript's text item delimiters to oTIDs
	return t
end remplace

#=====

Yvan KOENIG (VALLAURIS, France) vendredi 7 mars 2014 19:25:49

Oops, I forgot to write that on my machine there is only one note, the one which I used for test.

:frowning:
I figured that if I found the keycodes for ctrl-A and ctrl-shift-E then I would be able to type those two keys, in order to select the current paragraph, but it just didn’t work!

I also tried to keystroke “a/A” using control down, and that did neither.

Then I found the keycodes for arrow up and down, as option up arrow will also move to the beginning of paragraph, and shift option down arrow will select to end of paragraph.

So, the key code command in the Processes suite of System Events doesn’t work as advertised.

Hello.

I actually found a fix for selecting a paragraph.

  • Quit Notes.

  • Save the file below as utf-8, no BOM with Unix Linefeeds (TextWrangler) as DefaultkyBinding.dict in ~/Library/KeyBindings

  • Restart Notes and run the following script! :slight_smile:
tell application "Notes" to activate
tell application "System Events"
	tell application process "Notes"
		keystroke "l" using control down
	end tell
end tell

No need for that.

There are command tools allowing us to issue a triple click which select a paragraph.

cliclick is one of them which I used in a message posted in the thread :
http://macscripter.net/viewtopic.php?id=42134

Yvan KOENIG (VALLAURIS, France) vendredi 7 mars 2014 20:35:02

Hello.

Of course, if you are using the mouse to place the cursor anyway, then a triple-click does the trick. But that doesn’t work if you want to select the paragraph were the cursor currently resides.

My understanding is that according to what was written before, the cursor is in the paragraph to select to allow your DefaultkyBinding.dict to enlarge the selection.
But maybe I understand wrongly.

In fact, I really doubt that we really need AppleScript.
The original message said :

When the cursor is in the wanted paragraph, applying a triple click is more efficient than triggering a script to achieve the goal.

Yvan KOENIG (VALLAURIS, France) vendredi 7 mars 2014 22:22:30

I am on the same page with you as long as it is the mouse cursor, if it is the text cursor, then I think it is easier to use my method. ( I was rather shocked when I realized that it worked! )

As a sidenote: I think you can use the keybindings to do a lot of stuff with the selectors avaialable, and call them via System Events, as long as the keys you bind, aren’t “hardcoded” like ctrl-A, and alt arrow up.

You can for that matter make an applescript, that just envelopes some lines of AppleScript by comments, by using System Events to send a keystroke to your binding.

I have a great keyboard now by the way, I capitalize a word by hitting opt-c, lowercasing a word by opt-l, and uppercasing by opt-u for instance.

Thanks to all who responded. It helped put me on the right path to something that worked:

activate application “Notes”
tell application “System Events”
keystroke “a” using {control down}
keystroke “e” using {control down, shift down}
end tell

Where are these shortcuts defined ?
I’m remembering that once I saw a file describing numerous ones of this kind but can’t retrieve which it was.

I knew cmd + left arrow to move to beg of paragraph
shift + cmd + right arrow to select from the cursor to the end of paragraph.

Bingo, I found a list of them in :

http://guides.macrumors.com/Keyboard_shortcuts

Yvan KOENIG (VALLAURIS, France) samedi 8 mars 2014 10:36:43

Hello Yvan.

This is the official list.

But there is really no problem in using keybindings, and implement whatever short cut keys you want, that will work globally in all cocoa apps that uses the Cocoa Text System.