Applescript to check for a selection, then do something

619 views
Skip to first unread message

Oliver Taylor

unread,
Feb 1, 2012, 1:32:18 AM2/1/12
to bbe...@googlegroups.com
Is there a way to attach an applescript to a menu-item that checks to see if there's a selection?

I imagine it would go:
- Is there a selection?
+ if yes: trigger the menu item
+ if no: select the current word/line - then trigger the menu item.

Perhaps there's no need to attach it to the menu-item… maybe it's just a script that checks-for/makes a selection, then calls a specified menu-item.

Any help would be appreciated.

Steve Kalkwarf

unread,
Feb 1, 2012, 9:27:29 AM2/1/12
to bbe...@googlegroups.com
On Feb 1, 2012, at 1:32 AM, Oliver Taylor wrote:

> Is there a way to attach an applescript to a menu-item that checks to see if there's a selection?

Yes, but it might be cleaner to solve your problem in a different way.

What, specifically, are you trying to do?

Steve

Kendall Conrad

unread,
Feb 1, 2012, 3:32:47 PM2/1/12
to BBEdit Talk
Yes, you can check the length of the selection to see if anything is
connected and surround that with an if statement. I do it in a script
that I run.

Christopher Stone

unread,
Feb 1, 2012, 3:41:11 PM2/1/12
to bbe...@googlegroups.com
On Feb 1, 1:32 am, Oliver Taylor <olivertay...@me.com> wrote:
Is there a way to attach an applescript to a menu-item that checks to see if there's a selection?

On Feb 01, 2012, at 14:32, Kendall Conrad wrote:
Yes, you can check the length of the selection to see if anything is connected and surround that with an if statement. I do it in a script that I run.
______________________________________________________________________

Yes, you can determine quite a bit about the selection:

tell application "BBEdit"
tell front text window
set selectionProperties to properties of (selection)
end tell
end tell

{
container:project window 1 of application "BBEdit"
contents:"know"
length:4, 
characterOffset:9, 
startLine:1, 
endLine:1, 
startColumn:9, 
endColumn:13, 
startDisplayLine:1, 
endDisplayLine:1
}

But as Steve mentioned Oliver needs to tell us a little more about what he wants to do, so we can better help him.

--
Best Regards,
Chris

Oliver Taylor

unread,
Feb 5, 2012, 4:28:00 PM2/5/12
to bbe...@googlegroups.com
BBedit's text filters, applescripts, etc. act on a selection - if available - and fall-back to acting on the entire document. In some cases this is less than desirable. Sometimes you want to fall-back to the current word or line, or paragraph. I wanted to create an Applescript that would check for a selection and if there is none make one, then trigger the text-filter, script, etc.

Thanks to Mr Stone, here's an example:

tell application "BBEdit"

tell front text window

if length of selection = 0 then

tell application "BBEdit"

tell text window 1

select line (startLine of selection)

end tell

change case selection of text window 1 making raise case with replacing target

end tell

else

tell application "BBEdit"

change case selection of text window 1 making raise case with replacing target

end tell

end if

end tell

end tell

Oliver Taylor

unread,
Feb 5, 2012, 4:28:35 PM2/5/12
to bbe...@googlegroups.com
Thank you Mr Stone, as always.
Reply all
Reply to author
Forward
0 new messages