A different kind of "select word"

52 views
Skip to first unread message

Oliver Taylor

unread,
Oct 17, 2012, 6:10:02 PM10/17/12
to bbe...@googlegroups.com
I'm trying to write a different kind of "Select Word" script. I've used Gruber's and several others, but find them lacking or buggy in lengthy documents. Because I'm an applescript simpleton, and this is an interesting exercise, I've done it with a chain of grep searches.

The logic I'm using is this:
1. Start by looking at the character to the right of the insertion point.
2. If that character is a space, then move to the beginning of the word (using customized regex),
3. and then select to the end of the word (again, using a custom regex)
4. if not reverse the order of the searches.

This works very well for what I need, but I'd like to test for more than just a space. Meaning: if the character to the right of the insertion point is a space, or anything I define.

Is it possible to test the found text of nextChar as regex?

If you can point me in the right direction, I'd be grateful.

tell application "BBEdit"


--look at the next character
set nextChar to find "." searching in text of front text window options {search mode:grep, wrap around:false}


if found text of nextChar = " " then --should also test for punctuation
-- search for the beginning of the word
find "^|(?<!\\w[^\\w\\s])\\b(?=\\w)" searching in text of front text window options {search mode:grep, wrap around:false, backwards:true} with selecting match
-- then extend the selection to the end
find "(?<=\\w)\\b(?![^\\w\\s]\\w)|$" searching in text of front text window options {search mode:grep, wrap around:false, extend selection:true} with selecting match
else
-- search for the end of the word
find "(?<=\\w)\\b(?![^\\w\\s]\\w)|$" searching in text of front text window options {search mode:grep, wrap around:false} with selecting match
-- then extend the selection to the beginning
find "^|(?<!\\w[^\\w\\s])\\b(?=\\w)" searching in text of front text window options {search mode:grep, wrap around:false, backwards:true, extend selection:true} with selecting match
end if


end tell

Kendall Conrad

unread,
Oct 17, 2012, 7:10:41 PM10/17/12
to bbe...@googlegroups.com
I've used something like this with a couple of my scripts.

if character 1 of found text of nextChar is in (characters 1 through -1 of " ,;'\"") then

-Kendall

John Delacour

unread,
Dec 14, 2012, 5:39:29 PM12/14/12
to bbe...@googlegroups.com
On 17/10/2012 23:10, Oliver Taylor wrote:
> Is it possible to test the found text*of* nextChar as regex?
>
> If you can point me in the right direction, I'd be grateful.
>
> *tell* /application/ "BBEdit"
>
>
> --look at the next character
> *set* nextChar*to* *find*"." searching in/text/*of* *front* /text
> window/options{search mode:grep, wrap around:false}
>
>
> *if* found text*of* nextChar= " " *then* --should also test for
> punctuation


You don’t need to use regular expressions but rather do something like this:

tell application "BBEdit"
set _matched_chars to {space, tab, "\"", "'"} -- etc.
set _nextchar to (characterOffset of selection)
tell the text in the front text document
set _char to (character _nextchar as string)
if _char is in _matched_chars then
------
end if
end tell
end tell

--JD

Reply all
Reply to author
Forward
0 new messages