opening and writing file as unicode

I’m trying to use this file to load a song title in itunes.

on process(theArgs)
	
	--Coerce args to be a list
	if class of theArgs is not list then
		set theArgs to {theArgs}
	end if
	
	--Into iTunes ye files shall go
	tell application "iTunes"
		repeat with TheFile in theArgs
			add TheFile to playlist "best knowla"
			set name of thisfile to my GetTitleFromFile()
		end repeat
		
	end tell
end process

on GetTitleFromFile()
	tell application "Finder"
		set the_file to (read ("/Users/username/desktop/finaltext1.txt" as POSIX file) as Unicode text)
		open for access the_file
		set title_text to (read the_file)
		close access the_file
		return title_text
	end tell
end GetTitleFromFile

when I run the GetTitleFromFile() part only

set name of thisfile to my GetTitleFromFile()
return thisfile
on GetTitleFromFile()
	tell application "Finder"
		set the_file to (read ("/Users/username/desktop/finaltext1.txt" as POSIX file) as Unicode text)
		open for access the_file
		set title_text to (read the_file as Unicode text)
		close access the_file
	end tell
end GetTitleFromFile

I get an error with a bunch of chinese characters.
I am assuming the text is not formatted properly and I thought my text file was unicode text but I"m not sure.
what is going wrong?

If there is no ‘return’ statement at the end of the statements of your subroutine, then the result of the last command is returned.

You can’t set the name of a file with the Finder.

When you run the subroutine without the rest of the script, the variable thisfile is undefined.

You don’t need ‘open for access’ to read the file unless you’re doing multiple reads. You then use the reference number returned by ‘open for access’ for the mutiple reads.

gl,

Hi,

AppleScript’s Unicode text is always UTF-16,
your text could also be UTF-8, which is the same as MacRoman (ASCII) for characters < hex 80.
There is no “human readable” keyword for UTF-8, but you can use «class utf8»

btw: your subroutine GetTitleFromFile() can be reduced to one line:

read file (((path to desktop) as Unicode text) & "finaltext1.txt")

Side note: Use the command’s parameter instead of an extra coercion:

(path to desktop as Unicode text)

Also, you can check if the file is utf16 (unicode text) by checking for bom. e.g.


set bom to ((ASCII character 254) & (ASCII character 255))
set f to choose file
set t to read f
if t begins with bom then
	set t to read f as Unicode text
end if

gl,

Yes, that is easy but what are the rules when you replace “path to desktop” with a posix or other path?

"/Users/username/desktop/ttbook.txt" as POSIX file

or

"Nautilus HD:Users:username:Desktop:finaltext1.txt"

Try something like this:

read ("/Users/username/desktop/ttbook.txt" as POSIX file)
read file "Nautilus HD:Users:username:Desktop:finaltext1.txt"

And using the ‘read’ command’s ‘as’ parameter as well: :slight_smile:

read file ((path to desktop as Unicode text) & "finaltext1.txt") as Unicode text

But the original getTitleFromFile() handler apparently reads a path from the specified file, then reads the title text from the file at the end of that path. Obviously it would be helpful to know how each file was created or obtained.

In the other handler, an undefined variable’s accessed. Whatever ‘thisfile’ refers to, its name’s set to the same thing each time round the repeat. :confused:

The read command just needs an acceptable reference; How you get that reference is up to you.

well the finaltext1 file is basically a text file that has been edited to only contain a small phrase or title. It was saved to the folder as a text file since there were two parts to the task. The first part of the task got text from a webpage and edited it as an autmator action and an additional script inside the automator. The second part takes that file and puts it in a title after audio Hijack has had a session. This is my first project so I"m pretty bad at scripting but with a lot of help I got it working.
The first part is this

-- syntax : changeCase of someText to caseType
-- someText (string) : plain or encoded text
-- caseType (string) : the type of case required ("upper", "lower", "sentence", "title" or "mixed")

-- "upper" : all uppercase text (no exceptions)
-- "lower" : all lowercase text (no exceptions)
-- "sentence" : uppercase character at start of each sentence, other characters lowercase (apart from words in sentenceModList)
-- "title" : uppercase character at start of each word, other characters lowercase (no exceptions)
-- "mixed" : similar to title, except for definite and indefinite articles, conjunctions and prepositions (see mixedModList) that don't start a sentence

property lowerStr : "abcdefghijklmnopqrstuvwxyzáà âäãåæçéèêëíìîïñóòôöõøœúùûüÿ"
property upperStr : "ABCDEFGHIJKLMNOPQRSTUVWXYZÁÀÂÄÃÅÆÇÉÈÊËÍÌÎÏÑÓÒÔÖÕØŒÚÙÛÜŸ"
property alphaList : lowerStr's characters & reverse of upperStr's characters
property sentenceBreak : {".", "!", "?"}
property wordBreak : {space, ASCII character 202, tab}
property everyBreak : wordBreak & sentenceBreak
property whiteSpace : wordBreak & {return, ASCII character 10}
property currList : missing value
property sentenceModList : {"i", "i'm", "i'm", "i've", "i've", "I've", "I've", "I'm", "I'm", "I"} (* could be extended to include certain proper nouns, acronyms, etc. *)
property mixedModList : {"By Means Of", "In Front Of", "In Order That", "On Account Of", "Whether Or Not", "According To", "As To", "Aside From", "Because Of", "Even If", "Even Though", "In Case", "Inside Of", "Now That", "Only If", "Out Of", "Owing To", "Prior To", "Subsequent To", "A", "About", "Above", "Across", "After", "Against", "Along", "Although", "Among", "An", "And", "Around", "As", "At", "Because", "Before", "Behind", "Below", "Beneath", "Beside", "Between", "Beyond", "But", "By", "De", "Down", "During", "Except", "For", "From", "If", "In", "Inside", "Into", "Like", "Near", "Of", "Off", "On", "Onto", "Or", "Out", "Outside", "Over", "Past", "Since", "So", "The", "Though", "Through", "Throughout", "To", "Under", "Unless", "Until", "Up", "Upon", "When", "Whereas", "While", "With", "Within", "Without", "Ye", "ye", "without", "within", "with", "while", "whereas", "when", "upon", "up", "until", "unless", "under", "to", "throughout", "through", "though", "the", "so", "since", "past", "over", "outside", "out", "or", "onto", "on", "off", "of", "near", "like", "into", "inside", "in", "if", "from", "for", "except", "during", "down", "de", "by", "but", "beyond", "between", "beside", "beneath", "below", "behind", "before", "because", "at", "as", "around", "and", "an", "among", "although", "along", "against", "after", "across", "above", "about", "a", "subsequent to", "prior to", "owing to", "out of", "only if", "now that", "inside of", "in case", "even though", "even if", "because of", "aside from", "as to", "according to", "whether or not", "on account of", "in order that", "in front of", "by means of"}

on textItems from currTxt
	tell (count currTxt's text items) to if it > 4000 then tell it div 2 to return ¬
		my (textItems from (currTxt's text 1 thru text item it)) & ¬
		my (textItems from (currTxt's text from text item (it + 1) to -1))
	currTxt's text items
end textItems

on initialCap(currTxt)
	tell currTxt to if (count words) > 0 then tell word 1's character 1 to if it is in lowerStr then
		set AppleScript's text item delimiters to it
		tell my (textItems from currTxt) to return beginning & upperStr's character ((count lowerStr's text item 1) + 1) & rest
	end if
	currTxt
end initialCap

to capItems from currTxt against breakList
	repeat with currBreak in breakList
		set text item delimiters to currBreak
		if (count currTxt's text items) > 1 then
			set currList to my (textItems from currTxt)
			repeat with n from 2 to count currList
				set my currList's item n to initialCap(my currList's item n)
			end repeat
			set text item delimiters to currBreak's contents
			tell my currList to set currTxt to beginning & ({""} & rest)
		end if
	end repeat
	currTxt
end capItems

on modItems from currTxt against modList
	set currList to modList
	set currCount to (count modList) div 2
	repeat with currBreak in everyBreak
		set text item delimiters to currBreak
		if (count currTxt's text items) > 1 then repeat with n from 1 to currCount
			set text item delimiters to my currList's item n & currBreak
			if (count currTxt's text items) > 1 then
				set currTxt to textItems from currTxt
				set text item delimiters to my currList's item -n & currBreak
				tell currTxt to set currTxt to beginning & ({""} & rest)
			end if
		end repeat
	end repeat
	currTxt
end modItems

to changeCase of currTxt to caseType
	if (count currTxt's words) is 0 then return currTxt
	
	ignoring case
		tell caseType to set {upper_Case, lower_Case, sentence_Case, title_Case, mixed_Case} to {it is "upper", it is "lower", it is "sentence", it is "title", it is "mixed"}
	end ignoring
	
	if not (upper_Case or lower_Case or title_Case or sentence_Case or mixed_Case) then
		error "The term \"" & caseType & "\" is not a valid case type option. Please use \"upper\", \"lower\", \"sentence\", \"title\" or \"mixed\"."
	else if upper_Case then
		set n to 1
	else
		set n to -1
	end if
	
	considering case
		set tid to text item delimiters
		
		repeat with n from n to n * (count lowerStr) by n
			set text item delimiters to my alphaList's item n
			set currTxt to textItems from currTxt
			set text item delimiters to my alphaList's item -n
			tell currTxt to set currTxt to beginning & ({""} & rest)
		end repeat
		
		if sentence_Case then
			set currTxt to initialCap(modItems from (capItems from currTxt against sentenceBreak) against sentenceModList)
		else if title_Case or mixed_Case then
			set currTxt to initialCap(capItems from currTxt against whiteSpace)
			if mixed_Case then set currTxt to initialCap(capItems from (modItems from currTxt against mixedModList) against sentenceBreak)
		end if
		
		set text item delimiters to tid
	end considering
	currTxt
end changeCase

--start prgm
property finaltitle1 : ""
property finaltitle2 : ""

set unicodeTxt to (read ("/Users/username/desktop/ttbook.txt" as POSIX file) as Unicode text)
set {text:stringTxt} to unicodeTxt as string
searchReplace(stringTxt, "Next", "Listen!")
to searchReplace(thisText, searchTerm, replacement)
	set AppleScript's text item delimiters to searchTerm
	set thisText to thisText's text items
	set AppleScript's text item delimiters to replacement
	set thisText to "" & thisText
	set AppleScript's text item delimiters to {""}
	return thisText
end searchReplace
set finaltext to searchReplace(stringTxt, "Next", "Listen!")
set AppleScript's text item delimiters to "Listen!"
set thisText to finaltext's text items
set title1 to item 2 of thisText
set title2 to item 3 of thisText
set AppleScript's text item delimiters to {" "}
set title1text to text items of title1
set AppleScript's text item delimiters to {""}
set ftitle1 to title1text as text
set AppleScript's text item delimiters to {""}
set w2 to words of title2
set w1 to words of title1
set tid to AppleScript's text item delimiters
set AppleScript's text item delimiters to space
set title2 to w2 as string
set title1 to w1 as string
set AppleScript's text item delimiters to tid
set someText to title1
set finaltext1 to changeCase of someText to "mixed" (* "upper", "lower", "sentence", "title" or "mixed" *)
set someText to title2
set finaltext2 to changeCase of someText to "mixed" (* "upper", "lower", "sentence", "title" or "mixed" *)
get finaltext1 & ", " & finaltext2
try
	set finaltext1s to (finaltext1 as string) & return
	set finaltext1f to open for access "Nautilus HD:Users:username:Desktop:finaltext1.txt" with write permission
	set len to get eof finaltext1f
	write finaltext1s to finaltext1f starting at (len + 1)
	close access finaltext1f
on error e number n from finaltext1f to t partial result p
	try
		close access finaltext1f
	end try
	error e number n from finaltext1f to t partial result p
end try

finaltext1 is the title one I want to use for the song title and title 2 is the second title.
These are two different recording sessions.
Then I use this activated by Audio Hijack pro to change the file.

on process(theArgs)
	
	--Coerce args to be a list
	if class of theArgs is not list then
		set theArgs to {theArgs}
	end if
	
	--Into iTunes ye files shall go
	tell application "iTunes"
		repeat with TheFile in theArgs
			add TheFile to playlist "best knowla"
			set name of thisfile to my GetTitleFromFile()
		end repeat
		
	end tell
end process

on GetTitleFromFile()
	tell application "Finder"
		set the_file to (read ("/Users/username/desktop/finaltext1.txt" as POSIX file) as Unicode text)
		open for access the_file
		set title_text to (read the_file)
		close access the_file
		return title_text
	end tell
end GetTitleFromFile

I am sure there is a lot easier way of doing it but this is what I have so far.

ok, I just tried this as a script only in the script window.

set the_file to read file (("Nautilus HD:Users:username:Desktop:" as Unicode text) & "finaltext1.txt") as Unicode text

and I got chinese characters again!
any ideas?

Look at the text file yourself to see what is actually written. If it doesn’t look right, then the problem is most likely in writing the file.

This is the text in the finaltext1.txt file. “Author Author Part Ii Modern Classics”
when I use this code only and compile and run it

set the_file to read file (("Nautilus HD:Users:username:Desktop:" as Unicode text) & "finaltext1.txt") as Unicode text

this gives.
“?瑨潲?畴桯?å¡ç‰´â‰æ¤ ?摥牮?污獳楣?”

Well your longer script saves plain text to “finaltext1.txt”, so you won’t want to read that ‘as Unicode text’.

As I mentioned above, your GetTitleFromFile() handler in the shorter script reads “finaltext1.txt” as Unicode text and then uses what it’s read as a path to another file. You probably mean to read the title directly from “finaltext.1.txt”, in which case the Stefan/Bruce line is all you need for that. I’d guess that the shorter script should look something like this:

on process(theArgs)
	
	--Coerce args to be a list
	if class of theArgs is not list then
		set theArgs to {theArgs}
	end if
	
	--Into iTunes ye files shall go
	tell application "iTunes"
		repeat with TheFile in theArgs
			add TheFile to playlist "best knowla"
			set name of result to my GetTitleFromFile() -- Not 'name of thisfile'.
		end repeat
		
	end tell
end process

on GetTitleFromFile()
	return (read file ((path to desktop as Unicode text) & "finaltext1.txt"))
end GetTitleFromFile

If ‘theArgs’ contains more than one item, they’ll all have the same name in iTunes.

yes, that works really nicely. I am still having trouble with saving the title files. I am actually trying to make 2 files.
I copied the text and change the numbers to 2 but there was an error. I copied the text no inside the try statement but I’m not sure how to work out the error statement. Can you check 2 error statements at the same time or do you have to write 2 seperate try blocks?

try
	set finaltext1s to (finaltext1 as string) & return
	set finaltext1f to open for access "Nautilus HD:Users:paulendres:Music:Audio Hijack:paparimage:ttbooktitle:finaltext1.txt" with write permission
	set len to get eof finaltext1f
	write finaltext1s to finaltext1f starting at (len + 1)
	close access finaltext1f
	set finaltext2s to (finaltext2 as string) & return
	set finaltext2f to open for access "Nautilus HD:Users:paulendres:Music:Audio Hijack:paparimage:ttbooktitle:finaltext2.txt" with write permission
	set len to get eof finaltext2f
	write finaltext2s to finaltext1f starting at (len + 1)
	close access finaltext2f
on error e number n from finaltext1f to t partial result p
	try
		close access finaltext1f
	end try
	error e number n from finaltext1f to t partial result p
	
end try

Hi,

you need 2 separate try blocks because if the error occurs with finaltext2,
the error handler tries to close finaltext1f, or better a subroutine

Some notes:
¢ If the file to open is not an alias then the syntax is open for access file…
¢ If you don’t need all error informations (from, to, partial result), omit it
¢ EOF points to last character + 1, so no calculation is needed
¢ there is a “shortcut” to the Music Folder

set ttbooktitle to ((path to music folder as Unicode text) & "Audio Hijack:paparimage:ttbooktitle:")
set finaltext1s to (finaltext1 as string) & return
write_to_disk from finaltext1f into ttbooktitle & "finaltext1.txt"
if result is false then
	--  do something
end if

set finaltext2s to (finaltext2 as string) & return
write_to_disk from finaltext2f into ttbooktitle & "finaltext2.txt"
if result is false then
	--  do something
end if

on write_to_disk from theData into targetFile
	try
		set ff to open for access file targetFile with write permission
		write theData to ff starting at eof
		close access ff
		return true
	on error
		try
			close access file targetFile
		end try
		return false
	end try
end write_to_disk

PS: AppleScript works internally only with colon separated paths, POSIX paths are only necessary for the do shell script command
(though there are some rare exceptions e.g. the path to import photos into iPhoto can be a POSIX path)

Am I supposed to declare finaltext1f and finaltext2f before this script?
It says they are unkown.

Of course, you have to declare every variable before using it

ok, this is the completed text which does what I want it to do.
The titles are in the two seperate files.
but I have a question.

-- syntax : changeCase of someText to caseType
-- someText (string) : plain or encoded text
-- caseType (string) : the type of case required ("upper", "lower", "sentence", "title" or "mixed")

-- "upper" : all uppercase text (no exceptions)
-- "lower" : all lowercase text (no exceptions)
-- "sentence" : uppercase character at start of each sentence, other characters lowercase (apart from words in sentenceModList)
-- "title" : uppercase character at start of each word, other characters lowercase (no exceptions)
-- "mixed" : similar to title, except for definite and indefinite articles, conjunctions and prepositions (see mixedModList) that don't start a sentence

property lowerStr : "abcdefghijklmnopqrstuvwxyzáà âäãåæçéèêëíìîïñóòôöõøœúùûüÿ"
property upperStr : "ABCDEFGHIJKLMNOPQRSTUVWXYZÁÀÂÄÃÅÆÇÉÈÊËÍÌÎÏÑÓÒÔÖÕØŒÚÙÛÜŸ"
property alphaList : lowerStr's characters & reverse of upperStr's characters
property sentenceBreak : {".", "!", "?"}
property wordBreak : {space, ASCII character 202, tab}
property everyBreak : wordBreak & sentenceBreak
property whiteSpace : wordBreak & {return, ASCII character 10}
property currList : missing value
property sentenceModList : {"i", "i'm", "i'm", "i've", "i've", "I've", "I've", "I'm", "I'm", "I"} (* could be extended to include certain proper nouns, acronyms, etc. *)
property mixedModList : {"By Means Of", "In Front Of", "In Order That", "On Account Of", "Whether Or Not", "According To", "As To", "Aside From", "Because Of", "Even If", "Even Though", "In Case", "Inside Of", "Now That", "Only If", "Out Of", "Owing To", "Prior To", "Subsequent To", "A", "About", "Above", "Across", "After", "Against", "Along", "Although", "Among", "An", "And", "Around", "As", "At", "Because", "Before", "Behind", "Below", "Beneath", "Beside", "Between", "Beyond", "But", "By", "De", "Down", "During", "Except", "For", "From", "If", "In", "Inside", "Into", "Like", "Near", "Of", "Off", "On", "Onto", "Or", "Out", "Outside", "Over", "Past", "Since", "So", "The", "Though", "Through", "Throughout", "To", "Under", "Unless", "Until", "Up", "Upon", "When", "Whereas", "While", "With", "Within", "Without", "Ye", "ye", "without", "within", "with", "while", "whereas", "when", "upon", "up", "until", "unless", "under", "to", "throughout", "through", "though", "the", "so", "since", "past", "over", "outside", "out", "or", "onto", "on", "off", "of", "near", "like", "into", "inside", "in", "if", "from", "for", "except", "during", "down", "de", "by", "but", "beyond", "between", "beside", "beneath", "below", "behind", "before", "because", "at", "as", "around", "and", "an", "among", "although", "along", "against", "after", "across", "above", "about", "a", "subsequent to", "prior to", "owing to", "out of", "only if", "now that", "inside of", "in case", "even though", "even if", "because of", "aside from", "as to", "according to", "whether or not", "on account of", "in order that", "in front of", "by means of"}

on textItems from currTxt
	tell (count currTxt's text items) to if it > 4000 then tell it div 2 to return ¬
		my (textItems from (currTxt's text 1 thru text item it)) & ¬
		my (textItems from (currTxt's text from text item (it + 1) to -1))
	currTxt's text items
end textItems

on initialCap(currTxt)
	tell currTxt to if (count words) > 0 then tell word 1's character 1 to if it is in lowerStr then
		set AppleScript's text item delimiters to it
		tell my (textItems from currTxt) to return beginning & upperStr's character ((count lowerStr's text item 1) + 1) & rest
	end if
	currTxt
end initialCap

to capItems from currTxt against breakList
	repeat with currBreak in breakList
		set text item delimiters to currBreak
		if (count currTxt's text items) > 1 then
			set currList to my (textItems from currTxt)
			repeat with n from 2 to count currList
				set my currList's item n to initialCap(my currList's item n)
			end repeat
			set text item delimiters to currBreak's contents
			tell my currList to set currTxt to beginning & ({""} & rest)
		end if
	end repeat
	currTxt
end capItems

on modItems from currTxt against modList
	set currList to modList
	set currCount to (count modList) div 2
	repeat with currBreak in everyBreak
		set text item delimiters to currBreak
		if (count currTxt's text items) > 1 then repeat with n from 1 to currCount
			set text item delimiters to my currList's item n & currBreak
			if (count currTxt's text items) > 1 then
				set currTxt to textItems from currTxt
				set text item delimiters to my currList's item -n & currBreak
				tell currTxt to set currTxt to beginning & ({""} & rest)
			end if
		end repeat
	end repeat
	currTxt
end modItems

to changeCase of currTxt to caseType
	if (count currTxt's words) is 0 then return currTxt
	
	ignoring case
		tell caseType to set {upper_Case, lower_Case, sentence_Case, title_Case, mixed_Case} to {it is "upper", it is "lower", it is "sentence", it is "title", it is "mixed"}
	end ignoring
	
	if not (upper_Case or lower_Case or title_Case or sentence_Case or mixed_Case) then
		error "The term \"" & caseType & "\" is not a valid case type option. Please use \"upper\", \"lower\", \"sentence\", \"title\" or \"mixed\"."
	else if upper_Case then
		set n to 1
	else
		set n to -1
	end if
	
	considering case
		set tid to text item delimiters
		
		repeat with n from n to n * (count lowerStr) by n
			set text item delimiters to my alphaList's item n
			set currTxt to textItems from currTxt
			set text item delimiters to my alphaList's item -n
			tell currTxt to set currTxt to beginning & ({""} & rest)
		end repeat
		
		if sentence_Case then
			set currTxt to initialCap(modItems from (capItems from currTxt against sentenceBreak) against sentenceModList)
		else if title_Case or mixed_Case then
			set currTxt to initialCap(capItems from currTxt against whiteSpace)
			if mixed_Case then set currTxt to initialCap(capItems from (modItems from currTxt against mixedModList) against sentenceBreak)
		end if
		
		set text item delimiters to tid
	end considering
	currTxt
end changeCase

--start prgm
property finaltitle1 : ""
property finaltitle2 : ""

set uPath to "users/paulendres/Music/Audio Hijack/paparimage/ttbooktitle/ttbook.txt"
set fileSpec to uPath as POSIX file
set unicodeTxt to read fileSpec as Unicode text
set {text:stringTxt} to unicodeTxt as string
searchReplace(stringTxt, "Next", "Listen!")
to searchReplace(thisText, searchTerm, replacement)
	set AppleScript's text item delimiters to searchTerm
	set thisText to thisText's text items
	set AppleScript's text item delimiters to replacement
	set thisText to "" & thisText
	set AppleScript's text item delimiters to {""}
	return thisText
end searchReplace
set finaltext to searchReplace(stringTxt, "Next", "Listen!")
set AppleScript's text item delimiters to "Listen!"
set thisText to finaltext's text items
set title1 to item 2 of thisText
set title2 to item 3 of thisText
set AppleScript's text item delimiters to {" "}
set title1text to text items of title1
set AppleScript's text item delimiters to {""}
set ftitle1 to title1text as text
set AppleScript's text item delimiters to {""}
set w2 to words of title2
set w1 to words of title1
set tid to AppleScript's text item delimiters
set AppleScript's text item delimiters to space
set title2 to w2 as string
set title1 to w1 as string
set AppleScript's text item delimiters to tid
set someText to title1
set finaltext1 to changeCase of someText to "mixed" (* "upper", "lower", "sentence", "title" or "mixed" *)
set someText to title2
set finaltext2 to changeCase of someText to "mixed" (* "upper", "lower", "sentence", "title" or "mixed" *)
get finaltext1 & ", " & finaltext2


set ttbooktitle to ((path to music folder as Unicode text) & "Audio Hijack:paparimage:ttbooktitle:")
set finaltext1s to (finaltext1 as string) & return
write_to_disk from finaltext1 into ttbooktitle & "finaltext1.txt"
if result is false then
	--  do something
end if

set finaltext2s to (finaltext2 as string) & return
write_to_disk from finaltext2 into ttbooktitle & "finaltext2.txt"
if result is false then
	--  do something
end if

on write_to_disk from theData into targetFile
	try
		set ff to open for access file targetFile with write permission
		write theData to ff starting at eof
		close access ff
		return true
	on error
		try
			close access file targetFile
		end try
		return false
	end try
end write_to_disk

It seems like the command to “write_to_disk from finaltext1 into ttbooktitle & “finaltext1.txt”” writes to the file but I am unsure why it is necessary to do the “set finaltext1s to (finaltext1 as string) & return” command.

It’s not necessary at all, delete the lines :slight_smile: