Folder Action - How to set a variable file name

I am trying to write a Folder Action Script. I need this script to take the new item added to a folder, and encrypt the file using GPG. I need to know how to set up a variable for the name of the file added to the folder. The file names will always be different and I need the encrypted file to have the same name as the unencrypted, with the addition of .gpg after file name. Below is a copy of what I have already. I can encrypt a single file using only the name I have specified just fine. The variable is what is giving me problems.

Folder action script:

on adding folder items to testing
tell application “Terminal”
open “~/desktop/gpg.sh”
end tell
end adding folder items to

GPG.sh file:

gpg --output ~/Desktop/Encrypts/testfile.doc.pgp -e -r helpdesk@henrywurst.com ~/Desktop/testing/testfile.doc

HWIhelpdesk,

It generally goes like this.

on adding folder items to thisFolder after receiving theItems

end adding folder items to

where “thisFolder” is the variable for the folder that the script is attached to and “theItems” is a variable list of all items added to the folder. So in your script “testing” would be the variable for the folder that the script is attached to.

PreTech

so the variable “theItems” would automatically get the file name of the added items?

also… when running the GPG command: gpg --output ~/Desktop/Encrypts/testfile.doc.pgp -e -r helpdesk@henrywurst.com ~/Desktop/testing/testfile.doc i would need the variable to be included in the output and source file names. this may be a question to ask GPG’s support but if anyone knows how i could do this it would be appreciated.

The variable “theItems” would be a list of all files that were added. If added seven items, “theItems” would be essentially the same as:

set theItems to {file1,file2,file3,file4,file5,file6,file7}

The list would be a list of alias’ of the files added.

As far as the gpg deal… I haven’t a clue. I don’t mess with terminal much and don’t even know what gpg is. I’m sure someone here can help with that part.

PreTech

GREAT!!! Thanks Jacques. That seemed to work just fine. Only problem is that the Output folder is the same folder that the Folder Action is set to so everytime it encrypts a file and puts it in that folder it runs the Folder Action again. I ended up having to cancel the Folder Action to get it to stop adding new encrypted files. LOL

I think I can fix that though by just modifying the output folder to something that does not have a Folder Action script setup. Thank you for your help. It works like a charm.

EDIT:

Ok I lied. I dont seem to be able to change the output directory for GPG. I tried numerous combinations of the following path and none seemed to work:

on adding folder items to thisFolder after receiving theItems
	repeat with i in theItems
	set thisFile to quoted form of POSIX path of i
	do shell script "/usr/local/bin/gpg --output /users/localadministrator/Desktop/Encrypts " & thisFile & "'.pgp' -e -r helpdesk@henrywurst.com " & thisFile
	end repeat
end adding folder items to

I need the GPG output directory to be the users/localadministrator/desktop/encrypts folder.

Thanks againg Jacques. I am so new to this Scripting business. Hell I’m still new to the whole MAC thing. PC’s are my main area but I have to support some MAC’s here at work and they needed some scripting done. Thanks again for doing my job for me :lol:

Ok Jacques…LOL one more question. What would be the easiest way to change the destination folder from users/localadministrator/desktop/encrypts to an FTP site. The FTP site would not be mounted, I would have to use the Terminal commands: ftp ftp://username:password@ftp.url.com

Jacques,

I think i want to use something more along the line of the first script you posted here:


on adding folder items to thisFolder after receiving theItems
     repeat with i in theItems
     set thisFile to quoted form of POSIX path of i
     set destination to quoted form of ("/users/localadministrator/Desktop/testing/" & (get name of (info for i)) & ".pgp")
          do shell script "/usr/local/bin/gpg --output " & thisFile & "'.pgp' -e -r helpdesk@henrywurst.com " & thisFile
          do shell script "curl -T " & destination & " ftp://username:password@ftp.url.com"
     end repeat
end adding folder items to

Now this works fine, but the “destination” folder is the same folder that has the Folder action scipt set and when the file is encrypted with the gpg command and added to the same folder the whole process starts over again. i end up getting testfile.doc.gpg, testfile.doc.gpg.gpg, testfile.doc.gpg.gpg.gpg, etc. My question is, how would I use an “If exists” command to check if the "thisFile & “.pgp” file already exists and if it does then to end the script?

Thanks again for your help.

Thanks again Jacques. That worked great. I couldnt figure out the right syntax to check if the extension .pgp already existed or not.

I just wanted to thank you two for this topic - I was looking for a way to set up a folder on my desktop that I could just drop (or save) stuff to and have it encrypted with gpg and with a little tweaking and reading what you guys have done this is exactly what I needed. Again, thanks!

Nathaniel @ http://www.barsmart.com

on adding folder items to thisFolder after receiving theItems
   repeat with i in theItems
       set thisFile to quoted form of POSIX path of i
       set destination to quoted form of ("/users/NAME/Desktop/Safe/" & (get name of (info for i)) & ".pgp")
 if name extension of (info for i) is not "pgp" then end
       do shell script "/usr/local/bin/gpg --output " & destination & " -e -r NAME@EMAIL.COM " & thisFile
   end repeat
end adding folder items to