Some months ago I did a smaller version of this but hard coded the insert text. This time with the project so much larger with so much more text it was only practical to open a choice of documents from the application resource and use that for inserting into a document onto the personâs computer. My problem is although it does actually insert the text, it also inserts a map of where the insert source came from at the beginning of the insertion. The document the text will be inserted into is a simple plain text document however uses a .props file extension. Do I need to specify some kind of Unicode command or something?
Also, I received an error about one of the files not being open. Hereâs the script:
set new_name to "W"
set theDestinationFolder2 to .
set thisSource to path to resource "K.props"
set search_text to "BLACK="
set insert_text to path to resource "Block1.txt"
set insert_text2 to path to resource "Block2.txt"
set insert_textB to path to resource "Block1B.txt"
set insert_text2B to path to resource "Block2B.txt"
set the_file to (path to ...
set ref_num to (open for access the_file with write permission)
tell application "Finder"
try
set f to (((path to preferences folder from user domain) as Unicode text) & new_name) as alias
on error
set theDestinationFolder2 to (path to preferences folder from user domain)
set theDestinationFolder2 to ((theDestinationFolder2 as text) & "W:") as alias
set f to {make new folder at (path to preferences folder from user domain) with properties {name:"W"}} as alias -- creates folder if does not exist
duplicate thisSource to theDestinationFolder2 -- copies a props file across if one does not already exist
end try
display dialog ".." buttons {"A", "B", "Cancel"} default button "B"
set response to button returned of the result
if response is "B" then -- inserts text either after 'BlACK=' or adds it to end of document.
try
set the_text to (read ref_num) -- eof error if no text
-- find the byte
set the_offset to (offset of search_text in the_text)
if (the_offset = 0) then error "Search string wasn't found."
set byte_after to (the_offset + (length of search_text))
try
set trail_text to (text byte_after thru -1 of the_text)
on error -- there is no trailing text
set trail_text to ""
end try
open insert_text
write (insert_text & trail_text) to ref_num starting at byte_after
close access ref_num
close insert_text
on error
close access ref_num
set thePath to (path to preferences folder from user domain) & "W:K.props" as text
set fRef to (open for access file thePath with write permission)
try
open insert_textB
insert_textB
-- Start writing from the end of the file.
write to fRef starting at eof
close insert_textB
end try
close access fRef
set theData to (read file thePath)
end try
else if response is "A" then -- Same as response B but different data to be inserted.
try
set the_text to (read ref_num) -- eof error if no text
-- find the byte
set the_offset to (offset of search_text in the_text)
if (the_offset = 0) then error "Search string wasn't found."
set byte_after to (the_offset + (length of search_text))
try
set trail_text to (text byte_after thru -1 of the_text)
on error -- there is no trailing text
set trail_text to ""
end try
open insert_text2
write (insert_text2 & trail_text) to ref_num starting at byte_after
close access ref_num
close insert_text2
on error
close access ref_num
set thePath to (path to preferences folder from user domain) & "W:K.props" as text
set fRef to (open for access file thePath with write permission)
try
open insert_text2B
insert_text2B
-- Start writing from the end of the file.
write to fRef starting at eof
close insert_text2B
end try
close access fRef
set theData to (read file thePath)
end try
end if
display alert "... "... as critical buttons {"Quit"} default button "Quit" ... set response to button returned of the result
end tell
quit
Here is the applescript error message:
This is the bonus text that was inserted at the beginning:
BLACK=listalis HDD Name (Lots of strange characters)
Resources HDD Name: Applescript installer work:MacOSX - WWW installer.app:Contents:Resources:Block1.txt