Hey Adam,

Hmm.  That script doesn't work for me.  [I believe I've figured it out.]

Your script runs fine from BBEdit's script menu.

  ~/Library/Application Support/BBEdit/Scripts/Number Markdown * Links.scpt

On Jan 16, 2013, at 11:32, Adam Engst <[email protected]> wrote:
> But every now and then I need to number the links for compatibility with a 
> system that doesn't understand lazylinks, such as [Leanpub][*]. (This 
> paragraph is an example of lazylinks - the two links should be numbered 1 and 
> 2.)

Okay.  I think I figured it out:

You're trying to link unenumerated markers in article text to their associated 
footnoted URLs (again unenumerated)?  (This wasn't completely clear from the 
example you posted - to me at least.)

-------------------------------------------------------------------------------------------
Input:
-------------------------------------------------------------------------------------------

A TidBITS Reference in the article body [*]
A LeanPub Reference in the article body [*]
...

[*]: http://tidbits.com/
[*]: https://leanpub.com/

-------------------------------------------------------------------------------------------
Output:
-------------------------------------------------------------------------------------------

A TidBITS Reference in the article body ][1]
A LeanPub Reference in the article body ][2]
...

[1]: http://tidbits.com/
[2]: https://leanpub.com/

> The problem is that the AppleScript works fine when run from AppleScript 
> Editor, but throws an error when run as a text filter from within BBEdit.

Applescripts run from the script menu.  They do not run as 'Text Filters', and 
their utility is constrained when run as an 'Applescript Filter' from within a 
'Text Factory' to INPUT-->OUTPUT — so you cannot process piecemeal as you're 
doing in the script — you must instead generate the entire output.

  on ApplyTextTransform (fileData)
      -- do something to fileData
      return fileData -- or some reasonable facsimile thereof
  end

The only reason to add an Applescript to a Text Factory is if you need 
capabilities in the TF that aren't available directly to Applescript.  Is that 
true in your use-case?

If you really need to do this I can probably help.

It looks like JD missed the boat with his Perl script, but I'm sure he can fix 
it in a trice.  It certainly would be more efficient than using Applescript to 
iterate through the ref-links in BBEdit.

If I was going to use Applescript I'd probably use the Satimage.osax to provide 
more options.  For instance I would do some basic error-checking and make 
certain the number of references and their targets matched up.

This basically emulates what you've done in BBEdit, but it's faster.  (Tested 
with 50 ref-links.)

-------------------------------------------------------------------------------------------

try
  
  tell application "BBEdit" to set _text to text of front text window
  
  set refList to find text "\\[\\*\\]([^:]|$)" in _text with regexp, all 
occurrences and string result
  set targetList to find text "\\[\\*\\]:" in _text with regexp, all 
occurrences and string result
  
  if length of refList ≠ length of targetList then
    error "The number of references and links are unequal."
  else
    repeat with i from 1 to length of refList
      set {sPos, _len} to {matchPos, matchLen} of (find text 
"\\[\\*\\]([^:]|$)" in _text with regexp)
      set _text to change "\\[\\*\\]" into "][" & i & "]" in _text starting at 
sPos for _len with regexp
      set {sPos, _len} to {matchPos, matchLen} of (find text "\\[\\*\\]:" in 
_text with regexp)
      set _text to change "\\*" into (i as text) in _text starting at sPos for 
_len with regexp
      
    end repeat
    
    tell application "BBEdit" to set text of front text window to _text
    
  end if
  
on error e number n
  set e to e & return & return & "Num: " & n
  tell me to set dDlg to display dialog e with title "ERROR!" buttons 
{"Cancel", "Copy", "OK"} default button "OK"
  if button returned of dDlg = "Copy" then set the clipboard to e
  
end try

-------------------------------------------------------------------------------------------

--
Take Care,
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 [email protected]
To unsubscribe from this group, send email to
[email protected]
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 "[email protected]" rather than posting to the group.
Follow @bbedit on Twitter: <http://www.twitter.com/bbedit>



Reply via email to