On Feb 03, 2016, at 11:28, Max Horn <[email protected]> wrote:
> it requires the contents of the whole file. This means that I unfortunately 
> cannot (as far as I know) use a regular text filter.
______________________________________________________________________

Hey Max,

You probably can use a regular text filter by putting your get-text AppleScript 
in with `osascript`.

> The idea is to save the content of the active text document into a temporary 
> location (to make sure it also supports unsaved files)

That doesn't make obvious sense, unless you're wanting to save so you can read 
it back from disk.

In the latter case you can do something like this:

-------------------------------------------------------------------------------------------
tell application "BBEdit"
  tell front text document
    set docName to its name
    set _text to its text
    set docSaved to on disk
  end tell
  if not docSaved then
    set tempFilePath to (path to temporary items as text) & docName
    save front document to tempFilePath
  end if
end tell

read file tempFilePath
-------------------------------------------------------------------------------------------

> To my surprise, the hardest part seems to be to actually store a copy of the 
> contents of the text document into a temporary file... All my attempts so far 
> failed.

Simple. ( When you know AppleScript and have spent many hours scripting BBEdit. 
:)

-------------------------------------------------------------------------------------------
tell application "BBEdit"
  tell front text document
    set docName to its name
    set _text to its text
  end tell
  set tempFilePath to (path to temporary items as text) & docName
  set newDoc to make new document with properties {text:_text} initial save 
location tempFilePath
  save newDoc
end tell
-------------------------------------------------------------------------------------------

* I believe this correctly manages the line endings but have not thoroughly 
tested (particularly with non-Unix line endings).

> Then I tried to write the "contents of text document 1" to a file, like this:

That not too hard either.

-------------------------------------------------------------------------------------------
tell application "BBEdit"
  tell front text document
    set docName to its name
    set _text to its text
  end tell
  # Convert carriage returns to linefeeds.
  set _text to replace "\\x{0D}" using "\\x{0A}" searchingString _text options 
{search mode:grep, case sensitive:false, starting at top:true}
  set tempFilePath to (path to temporary items as text) & docName
  writeUTF8File(_text, tempFilePath) of me
  open tempFilePath
end tell

on writeUTF8File(_text, _file)
  try
    if _file starts with "~/" then
      set _file to POSIX path of (path to home folder as text) & text 3 thru -1 
of _file
    end if
    set fRef to open for access _file with write permission
    set eof of fRef to 0
    write _text to fRef as «class utf8»
    close access fRef
  on error e number n
    try
      close access fRef
    on error e number n
      error "Error in writeUTF8() handler!" & return & return & e
    end try
  end try
end writeUTF8File
-------------------------------------------------------------------------------------------

> However, the result has LF (mac) line ends, instead of CR (unix) lineends as 
> the original file.

Yes.  This is an idiosyncrasy of BBEdit that must be quite difficult to change 
(or Rich would have done so by now).

Within a document window (not the file on-disk) and within text gotten via 
AppleScript all EOL characters are represented by carriage returns.

Somehow BBEdit correctly translates these on-disk, but even in the Find Dialog 
\n and \r are synonymous.

I work around that in the script with this line:

# Convert carriage returns to linefeeds.
set _text to replace "\\x{0D}" using "\\x{0A}" searchingString _text options 
{search mode:grep, case 

So.  That should give you some ideas

> BTW, is there *any* serious documentation on the BBEdit AppleScript suite?

No.

But if you search the group for me you'll find just a few examples.

--
Best Regards,
Chris

-- 
This is the BBEdit Talk public discussion group. 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>

--- 
You received this message because you are subscribed to the Google Groups 
"BBEdit Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].

Reply via email to