The code below is supposed to access a directory of my choice, cycle through the files (txt and jpg) in the chosen directory and then use the file name of each element of the directory to replace a dummy variable in an HTML file with the name of the file element and then rewrite the new HTML text to a new file whose name is the same as the original file element with an HTM suffix instead of JPG. It also ignores any files in the directory that are not JPG files.
My problem is that when I did the processing of a file without the loop, everything worked fine, but with the loop, the HTM file is created in the directory, but nothing is being written into the new HTM file. What am I doing wrong:/?
Thanks to everybody who created the code I’ve used searching Macscripter in this effort and thanks for taking the time to read this post.
set this_folder to (choose folder with prompt "Pick the folder containing the files to process:") as string
tell application "System Events"
set a_list to every file of folder this_folder
end tell
repeat with myfile in a_list
set n to name of myfile
if n is not equal to ".DS_Store" then
set SearchDelim to {"xxxxxx.jpg"}
set ReplaceDelim to {n}
display dialog SearchDelim
--set TheNewFile to " " as alias
set OldDelim to AppleScript's text item delimiters
--select the text, might work best to replace with a drag and drop handlet for this script.
set TheFile to (path to desktop as Unicode text) & "template.htm" as alias
set the file_extension to the last word of n
if file_extension = "jpg" then
set n to ((text 1 thru ((length of n) - 4) of n)) & ".htm"
set TheNewFile to this_folder & n
try --error handling to make sure that the file is closed if there is a problem.
open for access TheFile with write permission
set TheText to read TheFile
repeat with i from 1 to count of SearchDelim
set AppleScript's text item delimiters to item i of SearchDelim
--search items used as text item delimiters, line below returns a list of text between instances of the list item but not the list item's text.
set TextList to every text item of TheText
set AppleScript's text item delimiters to item i of ReplaceDelim
--replace items set up as text item delimiters which are inserted in between the list items returned above when it is coerced into a string below.
set TheText to TextList as string
end repeat
open for access TheNewFile with write permission
set eof TheNewFile to 0 --make sure we are writing to the beginning of the file
write TheText to TheNewFile
close access TheFile
close access TheNewFile
on error
close access TheFile
end try
end if
end if
end repeat
set AppleScript's text item delimiters to OldDelim
Model: iMac G-4
AppleScript: 2.2.1 (100.1)
Browser: Firefox 3.0.4
Operating System: Mac OS X (10.5)