Troubleshooting: how to see errors from folder action scripts

applescriptfolder-action

I am trying to set up a "drop" folder: i.e. A watched folder with an associated Apple Script in "Folder Action Scripts".

The script is fairly simple. It defines a source link (the watched folder) and a destination path (a folder with the same parent as the watched folder) and runs a python script on the dropped file using the two variables defined as python arguments:

on adding folder items to this_folder after receiving added_items
    set dropFolder to quoted form of POSIX path of "IN/" -- use relative path
    set destinationFolder to quoted form of POSIX path of "OUT/" -- use relative path
    try
        repeat with EachItem in added_items
            set ItemInfo to info for EachItem
            if not folder of ItemInfo then
                set FileExtensionOfItem to name extension of ItemInfo
                if FileExtensionOfItem is "txt" then
                    set theBaseName to my getBaseNameOf(ItemInfo)
                    set pythonArg1 to theBaseName + "/packageElement"
                    set pythonArg2 to destinationFolder
                    set run_cmd to "python parser.py " + pythonArg1 + " " + pythonArg2
                    tell application "Terminal" -- pass file name to python using BASH from within this script
                        activate
                        do script run_cmd
                    end tell
                end if
            end if
        end repeat
    end try
end adding folder items to

There is some problem – likely minor in the script. Or perhaps there is a file permissions error?

I normally would trace my code using some simple technique like display dialog ...; however, I can not see these dialogues if they are run from the watched folder.

My question is this: What tools can we use to troubleshoot a folder action script such as this?

Best Answer

I've found it useful to put the folder action code into a template that can also be run as an applet, droplet, or from the Script Editor. It can then be used for testing and/or manually run for chosen files:

on run -- applet or from the Script Editor
    doStuff for (choose file with multiple selections allowed)
end run

on open droppedItems -- droplet
    doStuff for droppedItems
end open

on adding folder items to theFolder after receiving newItems -- folder action
    doStuff for newItems
end adding folder items to

to doStuff for someItems -- do stuff with the file items
    try
        repeat with anItem in someItems
            -- whatever
        end repeat
    on error errmess number errnum
        display alert "Error " & errnum message errmess
    end try
end doStuff