AppleScript in Editor vs. Launch Agent
Hello,
I have an AppleScript (script below) that runs fine in the AppleScript Editor. When I run the script using a Launch Agent (XML below) the script fails with no errors. I added some logging ("do shell script "logger -t sendername message"") to help with troubleshooting and a screenshot of that log activity is below.
The Red highlighted section is when the script runs as a result of the Plist. The Geen highlighted section is when the script runs in the AppleScript Editor. It appears to fail at the point where it enters the loop to do the deletions in the collection.
I can't for the life of me figure out why this is happening. The logged in user is the Administrator and I assume the Launch Agent (saved in ~/Library/LaunchAgents) is running as the logged-in user. But this would also be the case for the AppleScript Editor, correct?
AppleScript:
property kFileList : {}
set deletedFiles to 0
set source_folder to "Soft Proofs:SoftProofs"
set t to "SoftProofsCleanupScript"
set m to "Searching..."
do shell script "logger -t " & t & space & m
my createList(source_folder as alias)
set m to "Done searching, beginning delete..."
do shell script "logger -t " & t & space & m
with timeout of 600 seconds
set m to "Beginning delete operations."
do shell script "logger -t " & t & space & m
repeat with theItem in kFileList
set theFileName to the quoted form of (POSIX path of ((source_folder & ":" & theItem) as alias))
try
do shell script "rm -rf " & theFileName
set deletedFiles to deletedFiles + 1
on error errMsg
do shell script "logger -t " & t & space & "Error: " & errMsg
end try
end repeat
do shell script "logger -t " & t & space & "Deleted " & deletedFiles & " files."
end timeout
return kFileList
on createList(mSource_folder)
set oldestAllowedDate to (current date) - 2 * days
set item_list to ""
tell application "System Events"
set item_list to get the name of every disk item of mSource_folder
end tell
set item_count to (get count of items in item_list)
repeat with i from 1 to item_count
set the_properties to ""
set the_item to item i of the item_list
set the_item to ((mSource_folder & the_item) as string) as alias
tell application "System Events"
set file_info to get info for the_item
end tell
if visible of file_info is true then
if modification date of file_info is less than oldestAllowedDate then
set file_name to displayed name of file_info
set end of kFileList to file_name
if folder of file_info is true then
my createList(the_item)
end if
end if
end if
end repeat
end createList.plist XML:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Disabled</key>
<false/>
<key>Label</key>
<string>com.thgraphics.CleanupSoftProofs</string>
<key>ProgramArguments</key>
<array>
<string>osascript</string>
<string>/Volumes/System/Users/admin/Documents/SoftProofsCleanupScript.scpt</string>
</array>
<key>RunAtLoad</key>
<true/>
<key>StartInterval</key>
<integer>1800</integer>
</dict>
</plist>