set importFolder to "Macintosh HD:Users:jamcrae:Desktop:watched folder:New Manifests:" as alias
set doneFolder to "Macintosh HD:Users:jamcrae:Desktop:watched folder:Imported Manifests:" as alias
set picFolder to importFolder as string
set picList to list folder picFolder without invisibles
repeat with eachFile in picList
set fileRef to (open for access file "test File.txt")
set myData to (read fileRef as list using delimiter return)
close access fileRef
myData
But I actually want myData to be the information from eachFile not from “test File.txt”
If I may the line to read:
set fileRef to (open for access file eachFile)
then the next line in the script gives me an “end of file error.”
Why does it work with that hard coded “test File.txt”, but not with the variable eachFile?
JA
The ‘list folder’ command only returns the names of the items in a folder, whereas you need the full paths or aliases in the rest of your script.
The ‘as’ parameter for the ‘read’ command is to tell the command how to interpret the data in the file, not to specify the final form of the result. For your purposes, it should be either ‘as text’ or ‘as Unicode text’. (Obviously, you have to know in advance what sort of text the file contains.) The ‘using delimiter’ parameter will ensure that the final result is a list.
You should only use ‘as list’ (the parameter) when you’ve saved an AppleScript list to the file. The ‘read’ command then knows that some of the data have to be interpreted as the list structure.
EOF errors often occur when an error on a previous run has prevented a file from closing. You could try quitting Script Editor (after saving the script!), which closes all its open files, and then launching it again.
If you’re reading each file just once, you don’t need to open and close it for access. Not doing so makes single reads slightly faster and saves having to set up error traps to ensure that the file is closed again if anything goes wrong.
set importFolder to "Macintosh HD:Users:jamcrae:Desktop:watched folder:New Manifests:" as alias
set doneFolder to "Macintosh HD:Users:jamcrae:Desktop:watched folder:Imported Manifests:" as alias
set picFolder to importFolder as Unicode text
tell application "Finder"
try
set picList to every file of folder picFolder as alias list
on error number -1700
set picList to first file of folder picFolder as alias as list
end try
end tell
repeat with eachFile in picList
-- picList contains aliases, so need for 'file' before 'eachFile'.
set myData to (read eachFile as text using delimiter return)
myData
end repeat
The ‘try’ block above gets round a long-standing Finder bug where ‘as alias list’ errors if only one item matches the reference.
Learn something every day. Never encountered this construct before: ‘set myData to (read eachFile as text using delimiter return)’ Saves a lot of ASTID fiddling after the fact.
But, if I remember rightly, it’s not quite as fast, because the text is edited bit by bit as it comes off the disk. All else being equal, it’s probably easiest and fastest to read the whole file in one go and then get the ‘paragraphs’ of the result.
Understood, but I do actually need to use the name of the file a little bit later in the script. If I go with your method can I still pull the name of the file out easily?
This makes sense but I’ll need to read it over again to get it in my head
I wish I knew this one while I was working yesterday - would have saved some grief LOL
Very useful, thanks. Makes sense to me.
argh… but now this line (which appears just a few steps later) stopped working.
move document eachFile of folder picFolder to folder doneFolder
Applescript error can’t make data into type. Isn’t eachFile a document or file anymore??
eachFile is already a reference to a file, so you don’t need to put document in front of it. I don’t think document is a valid class anyway. Your other variables are strings, so you should place the ‘folder’ in front of them.
Yes indeed. If you’re still using the repeat variable ‘eachFile’ at that point, where eachFile is a reference to an alias in a list, the best combination of speed and simplicity would be:
set fileName to name of (info for eachFile)
‘info for’ is a command from the StandardAdditions that returns a record containing information about the file. The line above returns the ‘name’ property of that record. Alternatively, you could use either the Finder or System Events, which would take slightly (ie. unnoticeably) longer, but would be convenient if you were already using these applications for something else:
tell application "Finder"
set fileName to name of eachFile
end
There’s also a very fast textual way, but it’s probably more bother than it’s worth for your present purposes:
set eachFilePath to eachFile as Unicode text
if (eachFilePath ends with ":") then
set i to -2
else
set i to -1
end
set astid to AppleScript's text item delimiters
set AppleScript's text item delimiters to ":"
set fileName to text item i of eachFilePath
set AppleScript's text item delimiters to astid
I see from your latest post that you might not currently need this information after all, but it’s always useful to know.
Nigel, that is so very useful and clean. many thanks.
I’ve posted the full working script. I’m sure it’s far from tidy, so if anyone has anything they’d like to point out. Please do, and thanks for the help so far.
JA