I am trying to write a script I can attach to any folder that when I copy or save an item into it, it will make a copy into a separate folder. Both of these folders exist on a NAS. This script seems to work fine if the folders are on my local machine. When I test this the finder locks up and I have to relaunch. I found a logging function I have added so I can debug and its showing “Connection is invalid”.
-- Function to log messages to a file
on logMessage(message)
try
set logFilePath to (POSIX path of (path to desktop)) & "script_log.txt"
-- Open the log file for writing (create it if it doesn't exist)
set logFile to open for access logFilePath with write permission
-- Move the pointer to the end of the file
set eof of logFile to (get eof of logFile)
-- Write the message with a timestamp
write (get current date) & ": " & message & return to logFile
-- Close the log file
close access logFile
on error errMsg -- Catch errors while writing to the file
display dialog "Error logging message: " & errMsg
end try
end logMessage
on adding folder items to this_folder after receiving added_items
try
-- Set the log file path
set the alert_message to "Copy items to input folder?"
display dialog the alert_message buttons {"Yes", "No"} default button 2 with icon 1 giving up after dialog_timeout
set the user_choice to the button returned of the result
if user_choice is "Yes" then
tell application "Finder"
set theLocation to ("/Volumes/Art/Input") as POSIX file
repeat with an_item in added_items
duplicate an_item to theLocation with exact copy
end repeat
end tell
end if
on error errMsg number errNum
logMessage("Error " & errNum & ": " & errMsg) -- Log the error
end try
end adding folder items to