from my last post, you might have seen that i wanted to go around a timeout error with .Mac.
so what i am trying to do is edit a file. right now it is doing it directly on the .mac servers which is not a good solution. what i want to do now is that the file downloads itself first. then it edits the file which has been downloaded in some temporary place. after having edited it, it uploads it back up to the original location. the duplicate then get’s deleted.
so - how would i do this if i have this script here:
on add_meta()
get_folder_alias()
set meta_description_stuff to contents of text field "tf_site_description" of tab view item "tvi_add_meta" of tab view "tv_add_meta" of window "main"
set meta_tags_stuff to contents of text field "tf_site_tags" of tab view item "tvi_add_meta" of tab view "tv_add_meta" of window "main"
set meta_description to "<meta name=\"description\" content=\"" & meta_description_stuff & "\">"
set meta_tags to " <meta name=\"keywords\" content=\"" & meta_tags_stuff & "\">"
set meta_code to meta_description & return & meta_tags
set search_code_delimiters to "<title>"
set replace_code_delimiters to meta_code & return & " <title>"
tell application "Finder"
try
set the_site_files to (files of entire contents of folder the_site_folder whose name contains ".html") as alias list
on error
set the_site_files to (files of entire contents of folder the_site_folder whose name contains ".html") as alias as list
end try
end tell
repeat with aFile in the_site_files
set file_contents to read aFile as «class utf8»
if contents of file_contents contains "<meta name=\"description\" content=\"" then
set file_contents to read aFile as «class utf8»
set top_meta_offset to offset of "<meta name=\"description\" content=\"" in file_contents
set bottom_meta_offset to offset of "<title>" in file_contents
set replace_existing_metas to text (top_meta_offset) thru (bottom_meta_offset + 6) of file_contents
set new_contents to do_replace(replace_existing_metas, replace_code_delimiters, file_contents)
set file_ref to (open for access aFile with write permission)
set eof file_ref to 0
write new_contents to file_ref as «class utf8»
else
set file_contents to read aFile as «class utf8»
set new_contents to do_replace(search_code_delimiters, replace_code_delimiters, file_contents)
set file_ref to (open for access aFile with write permission)
set eof file_ref to 0
write new_contents to file_ref as «class utf8»
end if
end repeat
end add_meta
you can avoid the timeout error by using the shell to gather the files. I’ve optimized your script a bit.
The do shell script line at the end, which is commented out, moves the file to the location of destinationFile and deletes the original
on add_meta()
get_folder_alias()
set meta_description_stuff to contents of text field "tf_site_description" of tab view item "tvi_add_meta" of tab view "tv_add_meta" of window "main"
set meta_tags_stuff to contents of text field "tf_site_tags" of tab view item "tvi_add_meta" of tab view "tv_add_meta" of window "main"
set meta_description to "<meta name=\"description\" content=\"" & meta_description_stuff & "\">"
set meta_tags to " <meta name=\"keywords\" content=\"" & meta_tags_stuff & "\">"
set meta_code to meta_description & return & meta_tags
set search_code_delimiters to "<title>"
set replace_code_delimiters to meta_code & return & " <title>"
set the_site_files to paragraphs of (do shell script "find " & quoted form of POSIX path of the_site_folder & " -name '*.html'")
repeat with aFile in the_site_files
set file_ref to (open for access (POSIX file aFile) with write permission)
set file_contents to read file_ref as «class utf8»
if file_contents contains "<meta name=\"description\" content=\"" then
set top_meta_offset to offset of "<meta name=\"description\" content=\"" in file_contents
set bottom_meta_offset to offset of "<title>" in file_contents
set replace_existing_metas to text (top_meta_offset) thru (bottom_meta_offset + 6) of file_contents
set new_contents to do_replace(replace_existing_metas, replace_code_delimiters, file_contents)
else
set new_contents to do_replace(search_code_delimiters, replace_code_delimiters, file_contents)
end if
set eof file_ref to 0
write new_contents to file_ref as «class utf8»
close access file_ref
-- do shell script "mv " & quoted form of aFile & space & quoted form of POSIX path of destinationFile
end repeat
end add_meta
Note: You should add also some error handling regarding the read/write part of the script
i trust your script, i just want to know how it actually works.
so this part searches all the files:
set the_site_files to paragraphs of (do shell script "find " & quoted form of POSIX path of the_site_folder & " -name '*.html'")
is this like quoting a url? and then getting the contents?
and then it edits the file in some temporary saving and then edits them in there and then rewrites the files. so its not actually copying the files or what? it is just avoiding this replace&search directly in the file?
so the shell script moves it back up aint it? like terminal command “mv”
the result of the do shell script line is a list containing the (POSIX) paths of the found files (all files with .html ending)
The script opens the files at the specified locations, does the changings and writes the data back to the disk (overwriting the files at the same location).
I don’t know, where the files are located in, the folder the_site_folder is defined somewhere else