2

I want to pass the filename of the file I rightclicked to a shell script through automator:

# encrypt file.txt to file.enc using 256-bit AES in CBC mode
openssl enc -aes-256-cbc -salt -in file.txt -out file.txt.enc

When I create a 'service' I can pass on the input 'as arguments', but how to I implement it in the code? It would be great if the file could be called 'file.enc' instead of 'file.txt.enc' if that is possible.

1
pass=$(osascript -e 'tell app (path to frontmost application as text)
text returned of (display dialog "Enter password:" default answer "")
end')
for f in "$@"; do
    printf %s "$pass" | openssl enc -aes-256-cbc -salt -in "$f" -out "${f%.*}.enc" -pass stdin
done
  • could you explain your code a little? And maybe submit a decrypt option :) – janniks Jan 28 '13 at 13:05
  • 1
    You can pass variables as input, but not both files and variables, so I used AppleScript for displaying the dialog. You can decrypt the files with openssl enc -d -aes-256-cbc -in file.enc -out file.txt. ${f%x*} deletes the shortest x* pattern from the end, like xcc in aaxbbxcc. for f in "$@" is the common way to loop through arguments: for f in "$*" and for f in $@ are often errors. – Lri Jan 29 '13 at 1:27
  • I can't recall how many problems I've had to fix due to an un-quoted $@ in for loops. Knowing when and which quotes to use is not an easy thing. – bmike May 29 '13 at 19:49

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .