timkl
timkl

Reputation: 3339

Add character at beginning and end of each line

I want to cook up an Applescript that adds some characters to the beginning and end of each line of text, like this:

Before script execution:

<div>Something</div>
<div>Something</div>
<div>Something</div>

After script execution:

'<div>Something</div>' +
'<div>Something</div>' +
'<div>Something</div>'

How would you go about scripting something like this? Any hints or ideas is highly appreciated :)

Upvotes: 0

Views: 938

Answers (1)

dj bazzie wazzie
dj bazzie wazzie

Reputation: 3542

Something like this?

set theFile to (choose file) as string

try
    set fd to open for access file theFile
    set fileContents to read fd as string
    close access fd
on error
    close access file theFile
    return false
end try

set AppleScript's text item delimiters to "' +" & linefeed & "'"
set newFileContents to "'" & (every paragraph of fileContents) & "'" as string
set AppleScript's text item delimiters to ""

try
    set fd to open for access file theFile with write permission
    set eof of fd to 0
    write newFileContents to fd as string
    close access fd
on error
    close access file theFile
    return false
end try

return true

Upvotes: 1

Related Questions