I haven’t scripted anything in about 10 years so be gentle.
I’ve developed a very simple script that parses a text file which is contains a list of file names, then prompts the user for a parent directory to search, and then copies them to a specific location. When I run the script the first time it works fine. After that, it never works again. I seem to remember something about “resetting” the script. Make sense?
Welcome back to the fold, but I’m afraid you’ll have to show us the script to get a guess for why it only works once. Don’t be embarrassed; you’ve already said that you haven’t written a script in 10 years.
sure, np
--- clear the deck
set shot_list to ""
set end_count to ""
set source_dir to ""
set folder_contents to ""
set folder_count to ""
set destination_dir to ""
set text_string to ""
set zin_num to ""
set end_count to ""
set discipline to ""
set shot_name to ""
set filename_to_copy to ""
set folder_name to ""
set full_path to ""
set error_log to ""
---------------------set everything up-----------------------
set shot_list to read (choose file) as {list} using delimiter {return}
set end_count to (number of items in shot_list)
set source_dir to choose folder with prompt "Choose Source Folder to Search"
set folder_contents to (list folder source_dir invisibles "false" as text)
set folder_count to (number of items in folder_contents)
set destination_dir to choose folder with prompt "Choose Destination to Copy to"
tell application "Finder"
set the error_log to ((path to desktop) as text) & "ZIN FINDER LOG.txt"
open for access file the error_log with write permission
write "-----------------------------------" & (return) & date string of (current date) & " " & time string of (current date) & (return) to file the error_log starting at eof
close access file the error_log
end tell
--- now parse the file for each line-----------------------
repeat with i from 1 to end_count
---and then parse each line/column
set text item delimiters to tab
set text_string to item i of shot_list as text
--concatenate the shot name, discipline, and zin number file
set shot_name to text item 1 of text_string
set discipline to "lighting" as string
set zin_num to text item 2 of text_string
set filename_to_copy to shot_name & "_" & discipline & "_" & zin_num & ".mov" as string
-- -end concatenate---------------------------
--- repeatedly search source_dir----------
repeat with n from 1 to folder_count
set folder_name to item n of folder_contents as text
set full_path to source_dir & folder_name
set text item delimiters to ""
tell application "Finder"
if exists file (full_path & ":" & filename_to_copy as string) then
duplicate file (full_path & ":" & filename_to_copy as string) to destination_dir with replacing
set the error_log to ((path to desktop) as text) & "ZIN FINDER LOG.txt"
open for access file the error_log with write permission
write "Found " & filename_to_copy & " at " & folder_name & (return) to file the error_log starting at eof
close access file the error_log
exit repeat
else
end if
end tell
set text item delimiters to return
end repeat
end repeat
tell application "Finder"
set the error_log to ((path to desktop) as text) & "ZIN FINDER LOG.txt"
open for access file the error_log with write permission
write "-----------------------------------" & (return) to file the error_log starting at eof
close access file the error_log
end tell
-- compare list to copied contents--------
repeat with i from 1 to end_count
set text item delimiters to tab
set text_string to item i of shot_list as text
--concatenate the shot name, discipline, and zin number file
set shot_name to text item 1 of text_string
set discipline to "lighting" as string
set zin_num to text item 2 of text_string
set filename_to_copy to shot_name & "_" & discipline & "_" & zin_num & ".mov" as string
-- end concatenate
set text item delimiters to ""
tell application "Finder"
if exists file (destination_dir & ":" & filename_to_copy as string) then
else
set the error_log to ((path to desktop) as text) & "ZIN FINDER LOG.txt"
open for access file the error_log with write permission
write "Could not find" & filename_to_copy & (return) to file the error_log starting at eof
close access file the error_log
end if
end tell
set text item delimiters to return
end repeat
tell application "Finder"
set the error_log to ((path to desktop) as text) & "ZIN FINDER LOG.txt"
open for access file the error_log with write permission
write "-----------------------------------" & (return) & date string of (current date) & " " & time string of (current date) & (return) & (return) to file the error_log starting at eof
close access file the error_log
end tell
display dialog "All done. Check Log"
--- clean everything up
set shot_list to ""
set end_count to ""
set source_dir to ""
set folder_contents to ""
set folder_count to ""
set destination_dir to ""
set text_string to ""
set zin_num to ""
set end_count to ""
set discipline to ""
set shot_name to ""
set filename_to_copy to ""
set folder_name to ""
set full_path to ""
set error_log to ""
Hi,
I guess the text item delimiters are set to an unexpected value.
It’s always recommended to save the state of the delimiter, change it, run your code and restore it afterwards.
The clear the deck parts are actually not necessary unless the script expects certain values.
For example all variables in the set everything up are defined properly.
But you could define the variable error_log once at the beginning of the script
or use a handler, the Finder is not needed to write into a text file.
property error_log : ""
--
set error_log to (path to desktop as text) & "ZIN FINDER LOG.txt"
--
writeLog about "-----------------------------------"
--
writeLog about "Could not find"
on writeLog about msg
try
set dataStream to open for access file error_log with write permission
write msg & return to dataStream starting at eof
close access dataStream
on error
try
close file error_log
end try
end try
end writeLog
Note: It’s also recommended to use the pointer (dataStream) to write into the text file and add some error handling to close the file reliably