Automator: Decode Decode64 selection and display output in a new TextEdit window

automatorterminal

I would like to display a service in a context menu for a selected text which is a base64 string. The service has to decode it to string with a base64 via terminal script and display an output in a new TextEdit window.

I have a service in an Automator app which has a script shell with input as arguments and command:

"$@" | base64 --decode

There is a connected "New TextEdit document action" to the shell script.

This display empty window

If I change the command to pbpaste | base64 –decode and I will copy a base64 string to clipboard then the service works correctly.

How to pipeline the input argument, which is a base64 selected string, to 'base64 –decode' command?

Best Answer

I know the question is about TextEdit, but for anyone who would rather do this in BBEdit, you can create a TextFilter to process the selected text.

#!/usr/bin/python

import sys
  
def decode64(myString):
    return myString.decode('base64')

input = sys.stdin.read()
print decode64(input)

If you want to use python3, it's even easier:

#!/usr/local/bin/python3

import sys, base64

input = sys.stdin.read()
print base64.b64decode(input)

Save this to: ~/Library/Application Support/BBEdit/Text Filters, then you can access it from the Text Filters menu.