I’m trying to access a specific line of a text file that I have in /Contents/Resources. I can display an image from the folder without any problem, but can’t get it to open a text file. The code I have is:
if name of theObject is equal to "solution" then
set solutionpath to POSIX path of (path to me) & "Contents/Resources/solutions.txt" as string
open for access file solutionpath
set solutions to read solutionpath
close access solutionpath
set rebussolution to item 1 of solutions
end if
The button is “solution” and when clicked I want it to open solutions.txt and display the line currently set a item 1, but it will later be changed to a variable set elsewhere in the program. When it stops throwing up errors at this point I’ll have it display the resulting text string, probably in a dialog. Every time I click the button I get told:
Seems to silly that it doesn’t work. It seemed like it would be such a simple thing!
Edit:
Okay, so I’ve made some progress. I can get it to do what I want it to when the file is on the desktop, but I just can’t get it to open a file stored within the app itself. This code seems to work great, but the commented out line I just can’t get to work:
if name of theObject is equal to "solution" then
--set solutionpath to POSIX path of (path to me) & "Contents/Resources/solutions.txt"
set solutionpath to (path to desktop as string) & "solutions.txt" as alias
set solutions to read solutionpath
set solutions to paragraphs of solutions
set rebussolution to item newrebus of solutions
display dialog rebussolution
end if
You should try to use built-in functions as much as possible. The bundle object for your application (“main bundle”) has a handful of commands that return paths common to all asstudio apps. Using this will always return the correct value, so you don’t risk messing up by trying to hack together a path by creating it manually. Not that there aren’t times where you need to manually construct a path, but in this case the “resource path” command returns exactly what you’re looking for, so all you need to do is append your file name and you’re golden. Also, realize that you don’t have to open or close files to read from them. That’s only necessary when writing.
I recommend defining your path only once, possibly in a separate initialization handler, and saving it in a persistent variable. It cleans up your code significantly, especially if you’re going to be making frequent or repeated calls to the file you’re reading from or writing to. You probably also want to add in some error-checking, as you’ll throw an error if the file is corrupt or doesn’t exist.
property pathDataFile : ""
on clicked theObject
set dataFileContent to read (POSIX file pathDataFile)
log dataFileContent
set dataFileLine2 to paragraph 2 of dataFileContent
log dataFileLine2
end clicked
on launched theObject
set pathResources to (resource path of main bundle) as string
log pathResources
set pathDataFile to (pathResources & "/Data.txt") as string
log pathDataFile
end launched