Take the 2-minute tour ×
Ask Different is a question and answer site for power users of Apple hardware and software. It's 100% free, no registration required.

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.

share|improve this question
add comment

1 Answer 1

up vote 1 down vote accepted
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
share|improve this answer
    
could you explain your code a little? And maybe submit a decrypt option :) –  Nekcihc 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
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.