Hey everyone, thanks in advance for any help you could give me:
I’m realtively new to applescipt, and would appreciate some help. I’m trying to creating a index file that will contain a simple text list containing the names of the files within a folder.
For some reason the following script always generates a blank file (forgive the lack of formatting):
set theFolder to “Macintosh HD:etc…”
set indexFile to “Macintosh HD:etc…:index”
tell application “finder”
list folder theFolder
set fileList to the result
open for access indexFile with write permission
set eof of indexFile to 0
write fileList to indexFile
close access indexFile
end tell
Any suggestions? In addition if there’s way of creating the index such that it is a html file containing a list of links to the files, that would be great! I’ll be using Fetch to mirror to folder to my web server, and wanted an easy way to have a page to keep track of the files within the folder
A few things, there’s no need to involve the Finder. When you use the “list folder” command, you need to specify a folder reference as either an alias or file specification, not just as a string. While you could then write that list to a file as an AppleScript list, it wouldn’t be that helpful to you if you wanted to read it manually so you should then convert that list to a return delimited string and then write it to the file. This script should help:
set the_folder to (choose folder) as alias
set the_log to (the_folder as string) & "index.txt"
set file_list to list folder the_folder without invisibles
tell (a reference to AppleScript's text item delimiters)
set {old_atid, contents} to {contents, return}
set {file_list, contents} to {"" & file_list, old_atid}
end tell
my write_to_file(the_log, file_list, false)
beep
on write_to_file(the_file, the_data, with_appending)
set the_file to the_file as file specification
try
open for access the_file with write permission
if with_appending = false then set eof of the_file to 0
write the_data to the_file starting at eof as (class of the_data)
close access the_file
return true
on error the_error
try
close access the_file
end try
return false
end try
end write_to_file
set webserver_url to "http://www.someserver.com/"
set the_folder to (choose folder) as alias
set the_log to (the_folder as string) & "index.html"
set {folder_name, container_name} to (my get_name_and_container(the_folder))
set html_header to "<html>
<head>
<title>" & folder_name & " Contents</title>
</head>
<body>
<p>List of files of folder " & folder_name & "</p>
<ul>
"
set file_list to list folder the_folder without invisibles
set body_string to {}
repeat with this_file in file_list
set this_file to (contents of this_file)
set end of body_string to {" <li><a href="", webserver_url, folder_name, "/", this_file, "">", this_file, "</a></li>", return}
end repeat
set html_footer to " </ul>
</body>
</html>"
my write_to_file(the_log, (html_header & (body_string as string) & html_footer), false)
beep
on write_to_file(the_file, the_data, with_appending)
set the_file to the_file as file specification
try
open for access the_file with write permission
if with_appending = false then set eof of the_file to 0
write the_data to the_file starting at eof as (class of the_data)
close access the_file
return true
on error the_error
try
close access the_file
end try
return false
end try
end write_to_file
on get_name_and_container(item_path)
set item_path to item_path as string
if item_path ends with ":" then
set {item_index, container_index} to {-2, -3}
else
set {item_index, container_index} to {-1, -2}
end if
tell (a reference to AppleScript's text item delimiters)
set {old_atid, contents} to {contents, ":"}
set {item_name, container_name} to {(text item item_index of item_path), "" & (text items 1 thru container_index of item_path)}
set contents to old_atid
end tell
if character -1 of container_name is not ":" then set container_name to container_name & ":"
return {item_name, container_name}
end get_name_and_container