Search code examples
applescriptsubroutine

Problem with Getting info for item (applescript)


I am a pretty good applescripter, and would like some help with this. I am making a recycle bin application, similar to that of the Windows XP Recycle Bin. It is of course a droplet. When I drop items onto the application, the application starts a subroutine designed to check whether the size limit of the Recycle Bin (Trash) was exceeded. However, when I try to get the info for the items in the Trash, the error message that comes up is: "Finder got an error. File item 1 wasn't found." I really need help :( The subroutine is below:

on check() 
tell application "Finder"
    set the total_size to 0
    set the trash_items to get every item in trash
    if the trash_items is not {} then
        repeat with i from 1 to the count of items in trash
            set this_info to get info for item i of trash --ERROR ON THIS LINE
            set the total_size to the total_size + (size of this_info)
        end repeat
        try
            set the second_value to the free_space / (RBPFMS / 100)
            if the total_size is greater than the second_value then
                display alert "Size Limit Exceeded" message "The Recycle Bin cannot receive any more items because it can only use " & RBPFMS as string & " of your hard drive." buttons {"OK"} default button 1
                return false
            else
                return true
            end if
        on error
            set RBP to ((path to startup disk) as string) & "Recycle Bin Properties"
            display dialog "Error: You have modified the properties file for the Recycle Bin. Do not modify the properties file. It is there to store information that is used to determine the properties for the Recycle Bin." with title "Property File Modified" with icon 0 buttons {"OK"} default button 1
            set the name of RBP to "DELETE ME"
            error number -128
        end try
    end if
end tell
end check

Solution

  • The error is caused by the expression info for item i of trash. The subexpression item i of trash returns a (Finder) item object. The info for command however requires an alias or file reference to the file (see AppleScript language guide).

    There are two ways to fix the expression. Explicitly cast the item to an alias, i.e.:

    repeat with i from 1 to the (count of items) in trash
      set this_info to get info for (item i of trash as alias)
      set the total_size to the total_size + (size of this_info)
    end repeat
    

    Or instead of using the info for command, simply use the Finder item's size property:

    repeat with i from 1 to the (count of items) in trash
      set the total_size to the total_size + (size of item i)
    end repeat
    

    Be sure to have both RBPFMS and free_space declared as global variables in the check function:

    on check()
        global RBPFMS
        global free_space
        ...
    end
    

    Another bug: put parentheses around RBPFMS as string in the display alert statement:

    display alert "Size Limit Exceeded" message "The Recycle Bin cannot receive any more items because it can only use " & (RBPFMS as string) & " of your hard drive." buttons {"OK"} default button 1