Ask Different is a question and answer site for power users of Apple hardware and software. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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
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 "")
for f in "$@"; do
    printf %s "$pass" | openssl enc -aes-256-cbc -salt -in "$f" -out "${f%.*}.enc" -pass stdin
share|improve this answer
could you explain your code a little? And maybe submit a decrypt option :) – Nekcihc Jan 28 '13 at 13:05
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 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.