holzschu / a-shell

A terminal for iOS, with multiple windows
BSD 3-Clause "New" or "Revised" License
2.69k stars 117 forks source link

Can we pass ios shortcuts input as stdin? #97

Open n8henrie opened 4 years ago

n8henrie commented 4 years ago

I had expected to effectively pipe text from shortcuts into a command (as stdin) and return the output to shortcuts. The stdout seems to be returned (at least in extension mode), but I can't figure out how to pipe in input

For example:

image

I've tried with <<< [Text] using the magic variable and still no luck.

Is this possible?

Thanks in advance, cool project!

personalizedrefrigerator commented 4 years ago

Hi! I've been trying to do this using the Base64 Encode shortcut action and Python's base64 library. Unfortunately, Python seems to need to run in-app, rather than in-extension. You might be able to do something similar using Lua, though!

Lua appears to successfully run in-extension. As such, putting a decode.lua in the $SHORTCUTS directory and running lua decode.lua [âš™ Base64 Encoded] | awk '{ print $2 }' could work!

Additionally, lua-users.org seems to have published a good base64-decoding script!

I hope this is helpful!


The rest of this response describes my difficulties using Python to create such a script. This might be a known issue. If so, please ignore the rest of this comment.


Unfortunately, running actions in-app opens the application and does not return output to the calling shortcut. Is this expected behavior? For context, I am trying to do something like this:

    Shortcut:
+---------------------------------+
📄 Text
    A test!
        ... of standard input
+---------------------------------+
            |
+---------------------------------+
âš™ Scripting
  [Encode] [📄 Text] with base64
+---------------------------------+
           |
+---------------------------------+
📄 Text
    python decode.py 'âš™ Base64 Encoded'
+---------------------------------+
           |
+---------------------------------+
⚪ A-Shell
    Execute shell command
    Command(s)       📄 Cmd
    Open the app to execute
        the commands?   [In-App]
    Show When Run     [No]
+---------------------------------+
           |
+---------------------------------+
🔎 Documents
  Show [⚪output] in Quick Look
+---------------------------------+

$SHORTCUTS/decode.py

import base64, sys

print(sys.argv)
if len(sys.argv) > 1:
    print(base64.b64decode(sys.argv[1]))

If run in-app, as shown, the application is shown, nothing is printed to the console, and Shortcuts displays a warning dialog that states, "No Items / The Quick Look action wasn't passed any items to preview." When run in-extension, I am told that Shortcuts, " Could Not Run Execute Command Your command failed with error code 1. Output was: Could not find platform independent libraries Could not find platform dependent libraries Consider setting $PYTHONHOME to [:] Fatal Python error: initfsencoding: unable to load the file system codec ModuleNotFoundError: no module named 'encodings'

Current thread 0x[hex string omitted] (most recent call first): "


As such, although it seems that base64-encoded input can be given to a-Shell, there appear to be difficulties when the application receiving this input needs to be run in-app.

I haven't tried this yet, but you might be able to use Lua! If Lua scripts can be run in-extension, then a command like this,

lua decode.lua [encoded text] | awk '{ print $2 }'

could work!

holzschu commented 4 years ago

Hi, if you run a shortcut in App, you can go back to the Shortcuts app by calling the command "open shortcuts://"