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.

pass=$(osascript -e 'tell app (path to frontmost application as text)
text returned of (display dialog "Enter password:" default answer "")
for f in "$@"; do
    printf %s "$pass" | openssl enc -aes-256-cbc -salt -in "$f" -out "${f%.*}.enc" -pass stdin
  • 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. – user495470 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

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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