Re: Scripting search/replace

2011-08-23 Thread Christopher Stone
On Aug 22, 2011, at 17:20, verdonv wrote:
 After looking through the BBEdit script library, I realize the problem is 
 that the 'replace' actually returns an integer that represents the number of 
 instances of the pattern changed, and not the changed string.

Right.  I explained that already.  Perhaps my explanation wasn't clear?

 I know it seems silly, but given this, and the way that Clippings work, I 
 think I'll have to fudge this so that the applescript does the replace on the 
 selection, then copies the modified selection and returns it to the clipping, 
 so the clipping can insert it... 

I also explained that the major problem remaining is that clippings EAT 
trailing whitespace even if they themselves CONTAIN whitespace.  That takes a 
little working around; the script appended does so.

 Anyways, this is an applescript question and not a BBEdit question, so I'll 
 take the discussion to a more appropriate forum.

Any questions about scripting BBEdit are appropriate to this forum.

--
Best Regards,
Chris


tell application BBEdit
  try
tell front text window
  tell text of selection
replace (\\w+) using ¬
  [\\1] options {search mode:grep, case sensitive:false}
set selText to its contents
set chrOfst to its characterOffset
set selLen to its length
set selLen2 to selLen
  end tell
  repeat while selText ends with return
set selText to text 1 thru -2 of selText
set selLen to selLen - 1
  end repeat
  if selLen2 ≠ selLen then
select (characters chrOfst thru (chrOfst + selLen - 1))
  end if
  set sel to contents of text of selection
  return sel
end tell
  on error errMsg number errNum
set sep to ==
set e to sep  return  Error:   errMsg  return  sep  return ¬
   Error Number:   errNum  return  sep
beep
display dialog e
  end try
end tell


-- 
You received this message because you are subscribed to the 
BBEdit Talk discussion group on Google Groups.
To post to this group, send email to bbedit@googlegroups.com
To unsubscribe from this group, send email to
bbedit+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/bbedit?hl=en
If you have a feature request or would like to report a problem, 
please email supp...@barebones.com rather than posting to the group.
Follow @bbedit on Twitter: http://www.twitter.com/bbedit


Re: Scripting search/replace

2011-08-23 Thread verdonv
 Right.  I explained that already.  Perhaps my explanation wasn't clear?
No it was fine. It just took a bit to sink in ;-)

 I also explained that the major problem remaining is that clippings EAT 
 trailing whitespace even if they themselves CONTAIN whitespace.  That takes a 
 little working around; the script appended does so.
I ran into something that must have been that in one of my attempts.

I REALLY appreciate your appended script. I'm reading through it now
to see if I can sort out what it's doing. The language constructs are
foreign to me, but I should be able to puzzle through the logic.

Thanks again,
v

On Aug 23, 9:34 am, Christopher Stone listmeis...@thestoneforge.com
wrote:
 On Aug 22, 2011, at 17:20, verdonv wrote:

  After looking through the BBEdit script library, I realize the problem is 
  that the 'replace' actually returns an integer that represents the number 
  of instances of the pattern changed, and not the changed string.

 Right.  I explained that already.  Perhaps my explanation wasn't clear?

  I know it seems silly, but given this, and the way that Clippings work, I 
  think I'll have to fudge this so that the applescript does the replace on 
  the selection, then copies the modified selection and returns it to the 
  clipping, so the clipping can insert it...

 I also explained that the major problem remaining is that clippings EAT 
 trailing whitespace even if they themselves CONTAIN whitespace.  That takes a 
 little working around; the script appended does so.

  Anyways, this is an applescript question and not a BBEdit question, so I'll 
  take the discussion to a more appropriate forum.

 Any questions about scripting BBEdit are appropriate to this forum.

 --
 Best Regards,
 Chris

 
 tell application BBEdit
   try
     tell front text window
       tell text of selection
         replace (\\w+) using ¬
           [\\1] options {search mode:grep, case sensitive:false}
         set selText to its contents
         set chrOfst to its characterOffset
         set selLen to its length
         set selLen2 to selLen
       end tell
       repeat while selText ends with return
         set selText to text 1 thru -2 of selText
         set selLen to selLen - 1
       end repeat
       if selLen2 ≠ selLen then
         select (characters chrOfst thru (chrOfst + selLen - 1))
       end if
       set sel to contents of text of selection
       return sel
     end tell
   on error errMsg number errNum
     set sep to ==
     set e to sep  return  Error:   errMsg  return  sep  return ¬
        Error Number:   errNum  return  sep
     beep
     display dialog e
   end try
 end tell
 

-- 
You received this message because you are subscribed to the 
BBEdit Talk discussion group on Google Groups.
To post to this group, send email to bbedit@googlegroups.com
To unsubscribe from this group, send email to
bbedit+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/bbedit?hl=en
If you have a feature request or would like to report a problem, 
please email supp...@barebones.com rather than posting to the group.
Follow @bbedit on Twitter: http://www.twitter.com/bbedit


Re: Scripting search/replace

2011-08-23 Thread John Delacour

At 13:54 -0700 20/08/2011, verdonv wrote:



Hi Chris,

Thanks for the feedback and the example. I like the simpler pattern
too :-)

As to why I am activating it from a clipping, well because it is part
of a bigger set of clippings.


So far as I can see you can use shell scripts in clippings just as 
well, and to do this:



My string might look like this...
C EM   Am7F

The result looks like this...
[C] [EM]   [Am7][F]



All you need is:

#! /usr/bin/perl
while () {s~(\w+)~[$1]~g; print}

JD

--
You received this message because you are subscribed to the 
BBEdit Talk discussion group on Google Groups.

To post to this group, send email to bbedit@googlegroups.com
To unsubscribe from this group, send email to
bbedit+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/bbedit?hl=en
If you have a feature request or would like to report a problem, 
please email supp...@barebones.com rather than posting to the group.

Follow @bbedit on Twitter: http://www.twitter.com/bbedit


Re: Scripting search/replace

2011-08-23 Thread John Delacour

At 09:03 -0400 20/08/2011, Verdon Vaillancourt wrote:

I am writing a tiny applescript, that I invoke with a clipping, to 
do a search/replace on a string and return the string. I'm mostly 
there.


My string might look like this...
C EM   Am7F

The result looks like this...
[C] [EM]   [Am7][F]



With your string selected, insert this clipping:

#script /posix/path/to/a.scpt#


which calls the AppleScript script:

-- a.scpt
tell application BBEdit to set _sele to get selection
do shell script perl -e '$_ = qq~  _sele  ~;
s~(\\w+)~[$1]~g;  print;'

This will work on multiple line selections as well.

JD

--
You received this message because you are subscribed to the 
BBEdit Talk discussion group on Google Groups.

To post to this group, send email to bbedit@googlegroups.com
To unsubscribe from this group, send email to
bbedit+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/bbedit?hl=en
If you have a feature request or would like to report a problem, 
please email supp...@barebones.com rather than posting to the group.

Follow @bbedit on Twitter: http://www.twitter.com/bbedit


Re: Scripting search/replace

2011-08-22 Thread verdonv
After looking through the BBEdit script library, I realize the problem
is that the 'replace' actually returns an integer that represents the
number of instances of the pattern changed, and not the changed
string. I know it seems silly, but given this, and the way that
Clippings work, I think I'll have to fudge this so that the
applescript does the replace on the selection, then copies the
modified selection and returns it to the clipping, so the clipping can
insert it... seems kind of round-about, but I really want to keep all
this within the clipping set.

Anyways, this is an applescript question and not a BBEdit question, so
I'll take the discussion to a more appropriate forum.

tty,
v

On Aug 20, 7:27 pm, verdonv verd...@verdon.ca wrote:
 Yes, that's the sort of direction I was trying to go with the 'as
 alias' thing... My thoughts, a) create a string from the contents of
 the selection, b) run the replace on the string, c) return the string,
 which the clipping will replace the original selection with... I just
 don't know much about applescript ;-)

 My original attempt works, but also produces the error/alert.

 v

 On Aug 20, 6:33 pm, Christopher Stone listmeis...@thestoneforge.com
 wrote:







  On Aug 20, 2011, at 15:54, verdonv wrote: Thanks for the feedback and the 
  example. I like the simpler pattern too :-)

  __

  Hey Verdon,

  You bet.

   As to why I am activating it from a clipping, well because it is part of 
   a bigger set of clippings. Clippings are the sensible method for most of 
   the set. This one is the oddball, but I want to keep everything together 
   in one toolbox, so to speak.

  If I understand the clipping/script mechanism correctly the script must 
  return a text value, which the clipping will emplace.

  The trouble is that your script acts on the selection, and then the 
  clipping wants to change the selection again to the output of the script.

  I was going to say that you could go ahead and get the selection after the 
  replace and return it, but there's some funky issue with whitespace getting 
  eaten.

  --
  Best Regards,
  Chris

-- 
You received this message because you are subscribed to the 
BBEdit Talk discussion group on Google Groups.
To post to this group, send email to bbedit@googlegroups.com
To unsubscribe from this group, send email to
bbedit+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/bbedit?hl=en
If you have a feature request or would like to report a problem, 
please email supp...@barebones.com rather than posting to the group.
Follow @bbedit on Twitter: http://www.twitter.com/bbedit


Scripting search/replace

2011-08-20 Thread Verdon Vaillancourt
Hi,

I am writing a tiny applescript, that I invoke with a clipping, to do a 
search/replace on a string and return the string. I'm mostly there.

My string might look like this...
C EM   Am7F

The result looks like this...
[C] [EM]   [Am7][F]

My script looks like this...
tell application BBEdit
activate
set chords to selection of text window 1
replace ([A-Za-z]+[0-9]|[A-Za-z]+)( *) using [\\1]\\2 searching in 
chords options {search mode:grep}
return chords
end tell

My Script does work, but it also returns an error...
Could not make some data into the desired type (MacOS Error code: -1700)

I've done some googling and believe I need to use 'as alias' somehow when I'm 
declaring the var chords. I have tried...
set chords as alias to selection of text window 1

.. but that didn't do it.

Any suggestions?
v

-- 
You received this message because you are subscribed to the 
BBEdit Talk discussion group on Google Groups.
To post to this group, send email to bbedit@googlegroups.com
To unsubscribe from this group, send email to
bbedit+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/bbedit?hl=en
If you have a feature request or would like to report a problem, 
please email supp...@barebones.com rather than posting to the group.
Follow @bbedit on Twitter: http://www.twitter.com/bbedit


Re: Scripting search/replace

2011-08-20 Thread Christopher Stone
On Aug 20, 2011, at 08:03, Verdon Vaillancourt wrote:
 I am writing a tiny applescript, that I invoke with a clipping, to do a 
 search/replace on a string and return the string. I'm mostly there.

__

Hey Verdon,

Any special reason you're activating this with a clipping rather simply putting 
your script in the script menu?

When you 'return chords' you're getting back something similar to:

 characters 2 thru 42 of text document 1 of application BBEdit

BBEdit then want to use this to replace the text you've already replaced with 
the script, but the data types don't match up.

Why not just use a script in the first place?

tell application BBEdit
  tell selection of front text window
replace (\\w+) using ¬
  [\\1] options {search mode:grep, case sensitive:false}
  end tell
end tell

--
Best Regards,
Chris

-- 
You received this message because you are subscribed to the 
BBEdit Talk discussion group on Google Groups.
To post to this group, send email to bbedit@googlegroups.com
To unsubscribe from this group, send email to
bbedit+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/bbedit?hl=en
If you have a feature request or would like to report a problem, 
please email supp...@barebones.com rather than posting to the group.
Follow @bbedit on Twitter: http://www.twitter.com/bbedit


Re: Scripting search/replace

2011-08-20 Thread verdonv
Hi Chris,

Thanks for the feedback and the example. I like the simpler pattern
too :-)

As to why I am activating it from a clipping, well because it is part
of a bigger set of clippings. Clippings are the sensible method for
most of the set. This one is the oddball, but I want to keep
everything together in one toolbox, so to speak.

best rgds,
verdon


On Aug 20, 2:16 pm, Christopher Stone listmeis...@thestoneforge.com
wrote:
 On Aug 20, 2011, at 08:03, Verdon Vaillancourt wrote:

  I am writing a tiny applescript, that I invoke with a clipping, to do a 
  search/replace on a string and return the string. I'm mostly there.

 __

 Hey Verdon,

 Any special reason you're activating this with a clipping rather simply 
 putting your script in the script menu?

 When you 'return chords' you're getting back something similar to:

  characters 2 thru 42 of text document 1 of application BBEdit

 BBEdit then want to use this to replace the text you've already replaced with 
 the script, but the data types don't match up.

 Why not just use a script in the first place?

 tell application BBEdit
   tell selection of front text window
     replace (\\w+) using ¬
       [\\1] options {search mode:grep, case sensitive:false}
   end tell
 end tell

 --
 Best Regards,
 Chris

-- 
You received this message because you are subscribed to the 
BBEdit Talk discussion group on Google Groups.
To post to this group, send email to bbedit@googlegroups.com
To unsubscribe from this group, send email to
bbedit+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/bbedit?hl=en
If you have a feature request or would like to report a problem, 
please email supp...@barebones.com rather than posting to the group.
Follow @bbedit on Twitter: http://www.twitter.com/bbedit


Re: Scripting search/replace

2011-08-20 Thread Christopher Stone
On Aug 20, 2011, at 15:54, verdonv wrote:
 Thanks for the feedback and the example. I like the simpler pattern too :-)
__

Hey Verdon,

You bet.

 As to why I am activating it from a clipping, well because it is part of a 
 bigger set of clippings. Clippings are the sensible method for most of the 
 set. This one is the oddball, but I want to keep everything together in one 
 toolbox, so to speak.


If I understand the clipping/script mechanism correctly the script must return 
a text value, which the clipping will emplace.

The trouble is that your script acts on the selection, and then the clipping 
wants to change the selection again to the output of the script.

I was going to say that you could go ahead and get the selection after the 
replace and return it, but there's some funky issue with whitespace getting 
eaten.

--
Best Regards,
Chris

-- 
You received this message because you are subscribed to the 
BBEdit Talk discussion group on Google Groups.
To post to this group, send email to bbedit@googlegroups.com
To unsubscribe from this group, send email to
bbedit+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/bbedit?hl=en
If you have a feature request or would like to report a problem, 
please email supp...@barebones.com rather than posting to the group.
Follow @bbedit on Twitter: http://www.twitter.com/bbedit


Re: Scripting search/replace

2011-08-20 Thread verdonv
Yes, that's the sort of direction I was trying to go with the 'as
alias' thing... My thoughts, a) create a string from the contents of
the selection, b) run the replace on the string, c) return the string,
which the clipping will replace the original selection with... I just
don't know much about applescript ;-)

My original attempt works, but also produces the error/alert.

v


On Aug 20, 6:33 pm, Christopher Stone listmeis...@thestoneforge.com
wrote:
 On Aug 20, 2011, at 15:54, verdonv wrote: Thanks for the feedback and the 
 example. I like the simpler pattern too :-)

 __

 Hey Verdon,

 You bet.

  As to why I am activating it from a clipping, well because it is part of a 
  bigger set of clippings. Clippings are the sensible method for most of the 
  set. This one is the oddball, but I want to keep everything together in one 
  toolbox, so to speak.

 If I understand the clipping/script mechanism correctly the script must 
 return a text value, which the clipping will emplace.

 The trouble is that your script acts on the selection, and then the clipping 
 wants to change the selection again to the output of the script.

 I was going to say that you could go ahead and get the selection after the 
 replace and return it, but there's some funky issue with whitespace getting 
 eaten.

 --
 Best Regards,
 Chris

-- 
You received this message because you are subscribed to the 
BBEdit Talk discussion group on Google Groups.
To post to this group, send email to bbedit@googlegroups.com
To unsubscribe from this group, send email to
bbedit+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/bbedit?hl=en
If you have a feature request or would like to report a problem, 
please email supp...@barebones.com rather than posting to the group.
Follow @bbedit on Twitter: http://www.twitter.com/bbedit