I am trying to convert a short integration Applescript for BBEdit into a macro to make BBAutocomplete (http://c-command.com/bbautocomplete/) compatible with NWP. BBAutocomplete is good because it lets you cycle through completions by invoking BBAutocomplete again à la Emacs’ dabbrev-expand (M-/), which apparently is the inspiration for the program, until you get the right one.
A few problems: I need to convert an NWP-array to an AS-record and pass it to the script to be run from the macro, pass NWP-variables to the script and I need to be able to return an AS-variable from the script to an NWP-variable. Is this possible?
Of course I may also have look over some other detail that makes this whole thing not doable…
Regards,
Martin
Code: Select all
$doc = Documents.active
if ! $doc
Prompt “No Document open!”
exit
end
# to be passed into the script that calls BBAutocomplete
$currentFileText = $doc.text
#Get start offset of word to be completed to be passed into the script that calls BBAutocomplete
Send Selector “moveWordBackwardAndModifySelection:”
$theOffset = $doc.activeSelection.range.location
# Fetch texts from open documents to be converted into an AS-record ‘alsoTexts’
$documentTexts = Array.new
foreach $d in Documents
$documentTexts.push $d.text
end
# AppleScript parts.
# Convert array $documentTexts into AS-record $alsoTexts.
#
# AS-block copied from handler makeCompletion()
#
# Is this possible?
$AS = “
tell application "BBAutoComplete"
set theCompletion to complete word starting at offset $theOffset ¬
in text $theCurrentFileText also using text $alsoTexts -- or some such thing
end tell
-- theCompletion is a record with three items: startOffset, endOffset and completionText
return third item of theCompletion“
$error = “”
$completionText = Run AppleScript $AS, $error
if defined $error
Prompt “AppleScript returned error: ” $error
Exit
End
#Replacement of handler applyCompletion().
$selectionRange = Range.new $thetOffset, $thetOffset + $doc.activeSelection.range.length
$doc.deleteInRange $selectionRange
$doc.insertAtIndex $theOffset, $completionText
Code: Select all
(*
BBEdit integration AppleScript to accompany
BBAutoComplete <http://c-command.com/bbautocomplete>.
Copyright © 2002-2012 Michael Tsai, <bbautocomplete@c-command.com>.
Put this script in BBEdit's Scripts folder.
*)
on canCompleteInWindow(w)
tell application "BBEdit"
return class of w is in {shell window, text window, disk browser window, project window, search results browser}
end tell
end canCompleteInWindow
on windowHasSingleText(w)
tell application "BBEdit"
return class of w is in {shell window, disk browser window, results browser, search results browser}
end tell
end windowHasSingleText
on windowHasMultipleTexts(w)
tell application "BBEdit"
return class of w is in {text window, project window}
end tell
end windowHasMultipleTexts
on makeAlsoTexts()
tell application "BBEdit"
set theResult to {}
-- this will include the first document from window 1, but that's OK
repeat with w in windows
if my windowHasSingleText(w) then
try
copy text of w to end of theResult
end try
else if my windowHasMultipleTexts(w) then
repeat with d in text documents of w
copy text of d to end of theResult
end repeat
else
--log "Skipping: " & (name of w)
end if
end repeat
return theResult
end tell
end makeAlsoTexts
on makeCompletion()
tell application "BBEdit"
set theCurrentFileText to contents of window 1
set theOffset to characterOffset of selection
set alsoTexts to my makeAlsoTexts()
tell application "BBAutoComplete"
set theCompletion to complete word starting at offset theOffset ¬
in text theCurrentFileText also using text alsoTexts
end tell
return theCompletion
end tell
end makeCompletion
on applyCompletion(c)
set {startOffset, endOffset, completionText} to c
tell application "BBEdit"
if startOffset is not equal to endOffset then
select characters (startOffset + 1) thru endOffset of window 1
else
try
select insertion point after character endOffset of window 1
end try
end if
set selection to completionText
if the length of the selection is greater than 0 then
select insertion point after selection
end if
end tell
end applyCompletion
on run
tell application "BBEdit"
if my canCompleteInWindow(window 1) then
my applyCompletion(my makeCompletion())
else
display dialog "Completion doesn’t work when the front window is of type “" & class of window 1 & "”." buttons {"OK"} default button "OK"
end if
end tell
end run