AppleScript pass variables to shell script

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" &p &filename

end try

end adding folder items to


Script never gets the arguements. Thus, script does not execute correctly. What am I missing?

Try something like this:

do shell script ("/path/to/sript arguments")


I run this from Objective-C. I can't help with trying to concatenate a string in AppleScript. I know that is not as easy as it sounds.

Errors in folder actions will fail silently unless you trap them. You can use a try statement, but in your snippet you are ignoring any 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
AppleScript pass variables to shell script
 
 
Q