I am trying to get data from a text file called “test.txt” but am having some problems. I keep getting an applescript error saying “Can’t make “test.txt” into a file”. I’m new to applescript and don’t understand what I am doing wrong. I would appreciate any help, the script is listed below. Thanks.
set theFile to “test.txt”
set theFile to theFile as text
open for access theFile
set fileContents to (read theFile)
close access theFile
You have to specify the path to the file. This works for me:
set theFile to "[path_to]:test.txt"
--You don't need this line:
--set theFile to theFile as text
open for access theFile
set fileContents to (read theFile)
close access theFile
--you can of course include this to debug it if you're not using Script Debugger or Smile:
display dialog fileContents
I modified my script as suggested. My file is located on the root level. After running my script I still get the same error as before. Am I doing something wrong?
set theFile to “[/]:test.txt”
–You don’t need this line:
–set theFile to theFile as text
open for access theFile
set fileContents to (read theFile)
close access theFile
I use this script to place a path to a file or folder onto the clipboard:
property prompt0 : “Path to folder or file?”
property prompt1 : “Path to what folder?”
property prompt2 : “Path to what file?”
property button_list : {“Cancel”, “Folder”, “File”}
display dialog prompt0 buttons button_list default button “File”
set the_result to result
if button returned of the_result is “Folder” then
set item_path to (choose folder with prompt prompt1) as string
else
set item_path to (choose file with prompt prompt2) as string
end if
tell application “Finder”
activate
set the clipboard to (“”" & item_path & “”")
end tell
I save it as a Compiled Script and put it in the scripts menu but it may be run as an application. After running it and selecting a file on my desktop, I get a applescript path like this:
“Macintosh HD:Users:kel:Desktop:test”
The name of the file I chose is “test”. Notice that it starts from the hard disk and goes down toward the file. Each item in the path is separated by colons. Folder names end with colons while file names have no colons. Notice that this is a string. You can coerce string to an alias reference by adding alias:
alias “Macintosh HD:Users:kel:Desktop:test”
If the file does not exist, then you will get an error. Otherwise:
open for access alias “Macintosh HD:Users:kel:Desktop:test”
should work. Note that if you’re just reading the file then in OSX you can just read it without open for access: