do shell script not working

Hello everyone, in the past days I’ve been trying to use a folder action script to decrypt a file. The command works great in Terminal but doesn’t as an AS.

Without the Shebang (#!) at the beginning, the error reported is “File not found”, using it generates no error, but nothing else happens.

I’ve tested do shell with other commands and saw that works, the problem must be related to my syntax, but I can’t find what’s wrong.

This is the working Terminal command:

sudo gpgwrap -p /Users/user/pw.txt gpg -o /Users/user/Documents/msg/test.txt -d /Users/user/Documents/msg/22590.emlx

and this is the script:

on adding folder items to this_folder after receiving this_item
    set outpath to "/Users/user/Documents/msg/test.txt"
    set fpath to POSIX path of this_item
    set cmd to "#!/bin/sh cd /usr/local/bin;./gpgwrap -p /Users/user/pw.txt gpg -o " & outpath & " -d " & fpath
    
    try
        
    do shell script cmd password "mypw" with administrator privileges
    on error the error_message number the error_number
    display dialog "si è verificato un errore: " & the error_number & ". " & the error_message buttons {"OK"} default button 1
        
    end try
    
    
end adding folder items to

I’d really appreciate some help.

Luca Materassi

Ciao Luca,

the applescript equivalent to the line above is:

set outpath to quoted form of "/Users/user/Documents/msg/test.txt" -- using quoted form is recommended as a precaution, if there are space characters in the path
set fpath to quoted form of "/Users/user/Documents/msg/22590.emlx"
do shell script "/usr/local/bin/gpgwrap -p '/Users/user/pw.txt' gpg -o " & outpath & " -d " & fpath password "¢¢¢¢" with administrator privileges

Note: testing folder actions is annoying because you don’t get any error message.
these_items is a list of aliases, so you must take one certain item or use an repeat loop to process all
like this

on adding folder items to this_folder after receiving these_items
    set outpath to quoted form of (POSIX path of (path to documents folder) & "test.txt") -- points to /Users/currentUser/documents/test.txt"
    set fpath to quoted form of POSIX path of (item 1 of these_items)
    try
        do shell script "/usr/local/bin/gpgwrap -p '/Users/user/pw.txt' gpg -o " & outpath & " -d " & fpath password "mypw" with administrator privileges
    on error the error_message number the error_number
        display dialog "si è verificato un errore: " & the error_number & ". " & the error_message buttons {"OK"} default button 1
    end try
end adding folder items to

I usually test folder actions with this syntax of your script to get any (error) reply


-- on adding folder items to this_folder after receiving these_items
set these_items to choose file with multiple selections allowed without invisibles
tell application "Finder" to set this_folder to alias "Disk:path:to:folder:"
set outpath to quoted form of (POSIX path of (path to documents folder) & "test.txt") -- points to /Users/currentUser/documents/test.txt"
set fpath to quoted form of POSIX path of (item 1 of these_items)
try
    do shell script "/usr/local/bin/gpgwrap -p '/Users/user/pw.txt' gpg -o " & outpath & " -d " & fpath password "mypw" with administrator privileges
on error the error_message number the error_number
    display dialog "si è verificato un errore: " & the error_number & ". " & the error_message buttons {"OK"} default button 1
end try
-- end adding folder items to

Thanks Stefan, tomorrow morning I’ll try and let you know.

I tried modifyng the script as you suggested but I only get the same error : No such file or Directory. I really don’t understand why the script doens’t find gpgwrap which IS in /usr/local/bin.

Luca