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.


User uploaded file

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>

Posted on Aug 28, 2013 12:09 PM

Reply
9 replies

Aug 28, 2013 12:43 PM in response to Brian Dieckman

from man rm:


-f Attempt to remove the files without prompting for confirma-

tion, regardless of the file's permissions. If the file does

not exist, do not display a diagnostic message or modify the

exit status to reflect an error. The -f option overrides any

previous -i options.



using -f suppresses error messages, so whatever error you're getting will not trigger the try block. I suspect you're getting a permissions error, but I'll need to look closer.

Aug 29, 2013 7:28 AM in response to twtwtw

twtwtw,


In the "Compatibility" section of man rm, it states "The rm utility differs from historical implementations in that the -f option only masks attempts to remove non-existent files instead of masking a large variety of errors."


Whatever the case, when I remove the "f" from the command, it runs and completes sucessfully via LaunchAgent, so there must not be any permissions errors.


Could AppleScript Editor be using a different shell than the LaunchAgent? (The implementation of rm seems to differ.)


-Brian

Aug 29, 2013 8:39 AM in response to K T

The logged in user (The only user configured on the machine aside from root) uses /bin/bash.


I suppose the safest thing to do is to specify that shell in my plist's ProgramArguments?


<?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>/bin/bash</string>
        <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>


-Brian

Aug 29, 2013 9:22 AM in response to Brian Dieckman

ok. Well, I cleaned up your code a bit and replaced the call to rm with a System Events delete command (on the simpler is usually better paradigm). see if you get the same problem with this script.


property kFileList : {}


set deletedFiles to 0

set source_folder to "Soft Proofs:SoftProofs"


my writeLog("Searching...")


my createList(source_folder as alias)


my writeLog("Done searching, beginning delete...")


with timeout of 600 seconds

my writeLog("Beginning delete operations.")


repeat with theItem in kFileList

try

tell application "System Events"

deletetheItem

end tell

set deletedFiles to deletedFiles + 1


on error errMsg

my writeLog(errMsg)


end try


end repeat


my writeLog("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 kFileList to kFileList & (every file of mSource_folder whose visible is true and modification date is less than oldestAllowedDate)

set folderList to every folder of mSource_folder whose visible is true and modification date is less than oldestAllowedDate

repeat with thisFolder in folderList

my createList(thisFolder)

end repeat


end tell

end createList


on writeLog(txt)

do shell script "logger -t SoftProofsCleanupScript " & txt

end writeLog

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

AppleScript in Editor vs. Launch Agent

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.