AppleScript: Take a file from a directory and find the file name and POSIX path. Next do a shell script passing the file name and path to the script

AppleScript

On adding folder items to this_folder after receiving added_items
     try
          set filename to name of this_folder
          set p to POSIX path of this_folder
          do shell script "path to script/.sh" &p &filename
     end try
end adding folder items to

Shell script never gets the arguments.
Have tried several boards and several variations of the code above, but no joy. What have I overlooked?


Errors in folder actions will fail silently unless you trap them. You can use a try statement, but in your snippet you are ignoring errors, such as:

  • AppleScript itself doesn't know about file names, so you need to use something that does, such as Finder;
  • The shell uses spaces as delimiters between arguments, so you need to make sure they are used as needed when appending the various text items;
  • File paths should be quoted or special characters (spaces, etc) otherwise escaped to prevent misinterpretations.

I don't know the purpose of passing the folder name in addition to the full path (also note that a list of the dropped items are passed to the handler in the added_items argument), but after fixing the above items, your snippet would be something like:

on adding folder items to this_folder after receiving added_items
    try
        tell application "Finder" to set filename to name of this_folder
        set p to quoted form of POSIX path of this_folder
        do shell script "/path/to/script " & p & space & quoted form of filename
    on error errmess number errnum
        display alert "Error " & errnum message errmess
    end try
end adding folder items to