On Oct 03, 2014, at 12:11, Christopher Stone <[email protected]> wrote:
> I will post a 100% vanilla AppleScript later today.
______________________________________________________________________
Hey Folks,
Okay. Here's the vanilla solution.
BBEdit can search/replace within a string not just a document, so this kind of
thing isn't very difficult.
* Note the data separator is expected to be a space, although that's easy to
change.
--
Best Regards,
Chris
-------------------------------------------------------------------------------------------
# Auth: Christopher Stone
# dCre: 2014/10/02 11:45
# dMod: 2014/10/03 16:06
# Appl: BBEdit
# Task: Create files from data in BBEdit
# Libs: None
# Osax: None
# Tags: @Applescript, @Script, @BBEdit, @Create, @Files, @Template
# Note: Pure Vanilla AppleScript
-------------------------------------------------------------------------------------------
property _template : text 2 thru -1 of "
IP1 : %IP1%
Host1 : %HN1%
DATA :
%D1%
"
-------------------------------------------------------------------------------------------
try
set outputFldrPath to "~/Desktop/"
set outputFileName to "Output_File_"
tell application "BBEdit"
tell front document
set dataList to contents of lines whose contents is not ""
end tell
set AppleScript's text item delimiters to " "
repeat with ndx from 1 to length of dataList
set _item to text items of item ndx of dataList
set IP1 to item 1 of _item
set HN1 to item 2 of _item
set _data to (items 3 thru -1 of _item) as text
set _temp to replace "%IP1%" using IP1 searchingString _template options
{search mode:grep, case sensitive:false, starting at top:true}
set _temp to replace "%HN1%" using HN1 searchingString _temp options
{search mode:grep, case sensitive:false, starting at top:true}
set _temp to replace "%D1%" using _data searchingString _temp options
{search mode:grep, case sensitive:false, starting at top:true}
set outputPath to outputFldrPath & outputFileName & ndx & ".txt"
_writeUTF8(_temp, outputPath) of me
end repeat
end tell
if outputPath starts with "~/" then
set outputPath to POSIX path of (path to home folder) & (text 3 thru -1 of
outputPath)
end if
set outputPath to alias POSIX file outputPath
tell application "Finder"
activate
update outputPath
reveal outputPath
end tell
on error e number n
set e to e & return & return & "Num: " & n
if n ≠ -128 then
try
tell current application to button returned of ¬
(display dialog e with title "ERROR!" buttons {"Copy Error Message",
"Cancel", "OK"} ¬
default button "OK" giving up after 30)
if ddButton = "Copy" then set the clipboard to e
end try
end if
end try
-------------------------------------------------------------------------------------------
--» HANDLERS
-------------------------------------------------------------------------------------------
-- Name: writeUTF8()
-- Task: Write utf8 text to a new or existing file and overwrite any content.
-- dMod: 2012/10/26 20:00
-------------------------------------------------------------------------------------------
on _writeUTF8(_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 _writeUTF8
-------------------------------------------------------------------------------------------
--
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].