0

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?

4
  • What is in "$@"? If this is just the text to decode you need to do something with it at the left side of the pipe (e.g. echo $@ | ...) or use base64 --decode <<< "$@"
    – nohillside
    Aug 25 '20 at 13:16
  • @nohillside I missed echo. $@ should represent a selected text but it looks like this is a wrong placeholder for an input argument. How should I reference to an input argument? Aug 25 '20 at 13:46
  • Please edit your question then to reflect your actual code. It may even help to add a screenshot of the Automator flow.
    – nohillside
    Aug 25 '20 at 13:54
  • Also: where is the input for the input argument coming from?
    – nohillside
    Aug 25 '20 at 13:56
1

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.

1
  • I'm going to have to ask the same question and then answer it just for BBEdit, aren't I? <rolls eyes>
    – benwiggy
    Sep 27 '20 at 11:41
0

if your Automator Service/Quick Action is set to e.g. Service receives selected text in any application / Workflow receives text in any application, then in the Run Shell Script action with Pass input: as arguments set, then the following line of code will return the decrypted text to an e.g. New TextEdit Document action:

Note: The use of terms separated with a / is to denote the difference between Automator prior to macOS Mojave and from it onwards.

base64 --decode <<< "$@"

As an alternative to using the Run Shell Script action and the New TextEdit Document action, by just using a Run AppleScript action to accomplish the task:

on run {input, parameters}
    
    set encodedString to input as string
    
    set decodedString to ¬
        (do shell script "base64 --decode <<<" & encodedString's quoted form)
    
    tell application "TextEdit" to ¬
        make new document with properties {text:decodedString}
    
end run
4
  • echo $1 | base64 --decode works as well Aug 27 '20 at 7:36
  • @Adobels, Yes that too works, however, when working with a string (the selected text), use a here string <<< as it eliminates the needless use of: echo |. Aug 27 '20 at 20:41
  • Why both commands works 1. base64 --decode <<< "$1" 2. base64 --decode <<< $1 ? Aug 28 '20 at 7:59
  • 1
    @Adobels, as a general rule, variables should be double-quoted to prevent globbing and word splitting. In other words, it's a best practice and more safe. Aug 28 '20 at 15:32
0

In Automator, you can set up a workflow as follows:

enter image description here

Pass in base64 data to stdin, as shown, and this will decode the data and write it into the front TextEdit window (opening the app and window, if required). This will overwrite the contents of that window; you can set it to append instead, or add an extra step to open a fresh window first.

11
  • $@ represents an input argument. I have found one in a blog post. I think that a correct way to access an argument is $1. Aug 27 '20 at 7:35
  • @Adobels: $@ returns the list of all input arguments; $1 returns the first input argument. I'm not sure which case the OP is trying to handle, but you're right that $1 would be better for a single input. But I suppose I should update it to handle multiple inputs correctly. I'll do that a bit later. Aug 27 '20 at 14:16
  • Selected text is an input to the shell script so I am interested only about single argument input. $@ I didn't new the difference between $@ and $1. thx Aug 27 '20 at 15:23
  • Did you not understand the question? The OP states "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." -- Your answer takes a file, not selected text, as its input and replaces text, not opening it in a new TextEdit document. Aug 27 '20 at 20:53
  • 1
    @user3439894: You ought to think beyond the OP. Screenshots are helpful to a lot of people. Aug 27 '20 at 22:30

You must log in to answer this question.

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