I need to parse a bunch of html files and pull their list items and rewrite as javascript arrays. So I wrote a quickie script to take care of it but my output keeps coming out with the escape backslashes:
set thisFile to choose file
set thisFileContents to readFile(thisFile)
set AppleScript's text item delimiters to ":"
set arrayNameAndTag to last text item of (thisFile as string)
set AppleScript's text item delimiters to "."
set arrayName to text item 1 of arrayNameAndTag
set jsArray to "var " & arrayName & " = new Array("
set AppleScript's text item delimiters to "<li>"
repeat with x from 1 to count of every text item of thisFileContents
if x > 1 then
-->parse html
set thisAnchor to text item x of thisFileContents
set AppleScript's text item delimiters to ">"
set thisTextStart to text item 2 of thisAnchor
set AppleScript's text item delimiters to "<"
set thisText to text item 1 of thisTextStart
set jsArray to jsArray & "\"" & thisText & "\","
end if
set AppleScript's text item delimiters to "<li>"
end repeat
set jsArray to jsArray & ");"
on readFile(thisFile)
set foo to (open for access (thisFile))
set txt to (read foo for (get eof foo))
close access foo
return txt
end readFile
I am sure you could have pasted it to the clipboard earlier, and gotten rid of the "" !
set theOut to "var outcomes = new Array(\"Blood Loss\",\"Complication Rates\",\"Costs\",\"Cuff Dehiscence\",\"Hospital Stay\",\"Instrument Exchange\",\"Learning Curve\",\"OR Time\",\"Set-up Time\",\"Securing Vessels\",\"Smoke Plume\",\"Thermal Spread\",\"Tissue Effect\",\"Tissue Temperature\",\"Wound Retraction\",\"Additional Articles\",);"
set {tids, AppleScript's text item delimiters} to {AppleScript's text item delimiters, "\\"}
set newout to every text item of theOut
set AppleScript's text item delimiters to ""
set the clipboard to newout
set AppleScript's text item delimiters to tids
”> (in a text document:)
(*
var outcomes = new Array("Blood Loss","Complication Rates","Costs","Cuff Dehiscence","Hospital Stay","Instrument Exchange","Learning Curve","OR Time","Set-up Time","Securing Vessels","Smoke Plume","Thermal Spread","Tissue Effect","Tissue Temperature","Wound Retraction","Additional Articles",);
*)
The backslashes are only in your script editor’s Results window, they’re not in the actual string. You do have an extra comma at the end of the array. Is that allowed?