Request support for File Sharing intents in Scripting Shortcuts
Is your feature request related to a problem? Please describe.
Motivated by Termux integration support (and discussed in issue #501 ), I'd like a way for a Scripting Shortcuts to receive a File Sharing intent so that I can write scripts that process local files (or local file data). This feature would work in a manner similar to the Share files into a shortcut feature. That feature appears to be tied to the Response Handling of standard HTTPShortcuts.
Describe the solution you'd like
Personally, I'd like to use this feature in concert with Termux integration support, so that I can long tap on files and execute shell scripts, passing the file or file contents to the script. But I imagine there's still utility in being able to run arbitrary JavaScript code on files or file data.
I keep saying "file and file data or contents" because I'm not sure how android file sharing intents work--i don't know if it passes around a file pathspec, or the contents of the file. I suspect the latter, in order to avoid file permissions problems.
Assuming that's the case, it would be convenient if the new feature could include the ability to write the data to a temporary file. I can imagine this being either explicit in the shortcut definition (with a check box indicating that the data should be written out to a file) or implicit (availability of JavaScript functions that can read the data as if it were on standard input then write to a file.) I like the latter because it gives the shortcut author the option of writing to a file or keeping the file data in buffer memory. It also lets the author handle cleanup of the temporary file.
In my motivating use case, I'd write the data to temporary file, then utilize the runTermuxCommand function to pass the temp file to a shell script. Then I'd delete the temp file.
Actually, if there's a way to pipe data to the standard input of a shell script, I can see preferring that over temp files in some circumstances, but I don't know if that's technically possible, and even if it is, that would be an additional feature request.
Describe alternatives you've considered
Termux also supports handling of file sharing intents as described here. It's a bit awkward because it pops up an Edit dialog, and causes all shared files to be written into a specific folder. Furthermore, it's severely limited in that it will only run a single, pre-determined shell script (or symlink to preferred editor). So there's no way to define several different scripts/Shortcuts simultaneously. It's really only intended to be used to run a text editor.
Thanks for the consideration!
Thanks for the feature request.
I keep saying "file and file data or contents" because I'm not sure how android file sharing intents work--i don't know if it passes around a file pathspec, or the contents of the file. I suspect the latter, in order to avoid file permissions problems.
Access to the file system is quite restricted and abstracted on Android. When the user shares a file into an app, that app receives a temporary URI that allows the app to read the file. This URI is shortlived, meaning that the app must immediately read the data or else lose access to it. There is no way to get the absolute path on the file system out of this URI. There might not even be a file associated with it, as the shared data might come from something other than a file.
The HTTP Shortcuts app only has access to the file system through so called "scoped storage", through the Working Directories feature. This essentially means that the user grants read and write access to a specific directory, and the app then has full access to its contents, but does not know where on the actual file system this directory is and can not break out of it. Like a sandbox or separately mounted drive.
This means that the only suitable way for the HTTP Shortcuts app to send file data to Termux would be by storing it into a file within a directory that Termux also has access to. I don't know how Termux handles the Android file system, but I assume it should be possible to have a directory somewhere where both apps have access.
As such, this feature request boils down to 2 things:
- Allowing scripting shortcuts (or basically any shortcut type that supports scripting) to be able to receive shared files. Currently this is tied to file bodies and file parameters, which are only available in HTTP shortcuts.
- Allow Scripting to access the contents of shared files. There is already the
getDirectoryfunction, which can be used to access files in one of the "Working Directories", so probably the easiest would be to extend this function to also be able to access the temporary directory used for storing shared files.
Number 1 could also be done with the workaround of creating a dummy HTTP shortcut, which uses a file parameter but then uses the abort() function in its Scripting before the actual HTTP request is made.
Number 2 will need some investigation and refactoring probably, but should be doable. I'll look into it when I have the time.