Applescript is great but some things just drive me nuts…
I’m trying to read the contents of a file
set fileLoc to "~/.aFolder/another/file.lua"
set fileContents to (read fileLoc)
It gives me "Can’t make “~/.aFolder/another/file.lua” into type file. Whatever that means. I look around and it seems like it has something to do with POSIX vs. Applescript (old mac os style) file naming conventions.
So I try to convert it first…
set fileLoc to "~/.aFolder/another/file.lua"
set newPath to POSIX file fileLoc
set fileContents to (read newPath)
But I basically get the same error.
Using the terminal is easy and intuitive…
set fileLoc to "~/.aFolder/another/file.lua"
set fileContents to do shell script "cat " & fileLoc
But after looking for way too long for a pure applescript solution that is probably easier (yet not obvious), I post here.
Expanding the tilde character is a shell function, and AppleScript isn’t a shell. Use something like this:
set fileLoc to "~/.aFolder/another/file.lua"
if fileLoc begins with "~" then
set fileLoc to (POSIX path of (path to home folder)) & text 3 thru -1 of fileLoc
end if
Using a command called cat to read a file is intuitive? Could have fooled me…
Maybe intuitive isn’t the best word. At least it is simple and consistent. “cat” or “less” don’t seem like the best choice either but it’s a language that I can at least write without googling for 5 minutes per line because I can’t figure out whether I’m way off or just forgot a preposition or something.