Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to import/export files with shortcuts? #74

Open
ghost opened this issue Jun 12, 2020 · 12 comments
Open

How to import/export files with shortcuts? #74

ghost opened this issue Jun 12, 2020 · 12 comments

Comments

@ghost
Copy link

ghost commented Jun 12, 2020

Hi, I’m new to a-Shell and am currently having trouble transferring files in and out of the app using shortcuts.

For exporting a file out I’m using the a-Shell ‘Get File’ action, followed by Apple’s Save File action. However this results in an error saying the file could not be found.

For importing a file into a-shell I’m using the Apple Get File action followed by the a-Shell ‘Put File’ action. This completes without error, however I don’t see the file in a-Shell when doing an ‘ls’.

What am I doing wrong?

Also, I think it would be great if there was some in-depth documentation explaining how to get a-Shell specific tasks completed.

Thanks!

@holzschu
Copy link
Owner

Hi,
The short answer is: Shortcuts are running in their own directory, $SHORTCUTS. So, from the App or from the File explorer, you are not seeing the same files. The solution is to move files to and from this directory, or to start the Shortcut with a cd ~/Documents/ command.

The long answer is that Shortcuts running "In Extension" and Shortcuts running "In App" are, from Apple's point of view, two different applications, with two different ~ directories. $SHORTCUTS is a middleground that they can both access. This also means that cd ~/Documents/ will move you to two different places depending on whether the shortcut is run in App or in Extension.

@ghost
Copy link
Author

ghost commented Jun 13, 2020

Thank you! Got it all sorted out with Importing/Exporting. After I did an import with Shortcuts, I did an “ls $SHORTCUTS” and saw the file.

@nipunbatra
Copy link

Hi @holzschu
I am having some issues with files and thought to write here for better documentation.

I am trying to do the following:

  1. User gives input in shortcuts
  2. The input is a number which is passed to Execute shell command
  3. The command is a simple Python script that accepts the input as a command line argument and processes it. In this case I am creating a trivial factorial program
  4. Next, I’d like the shortcut to display back the output from the Python program

Steps 1, 2, and 3 are easy.

I am running Step 2 (Execute shell command In the App) as Python does not run otherwise.

I am not sure how best to do step 4.

One option is to redirect the output of Python to a temporary text file, read the text file via Get File and then display the output in alert/quick look.

Other option would be to get stdout directly from the executed path.

I don’t have any clue about the latter.

Regarding the former, I am getting stuck. I am able to create the temp text file but unable to read it back in shortcuts.

The following is an image of the shortcut.

Shortcuts-failed

The following is the link to the shortcut: https://www.icloud.com/shortcuts/76a296293d3749cf9aab2b647734e437

The following is the content of factrial.py

import math
import sys
print(sys.argv)
num = int(sys.argv[1])
print(f"The factorial of {num} is {math.factorial(num)}")

My questions:

  • What do I need to change in order to get temp.txt to be read in shortcuts app?
  • Can we directly get stdout from executing shell command back to shortcuts?

@holzschu
Copy link
Owner

I think you need to add "open shortcuts://" to the list of commands to get back to the Shortcuts app once all commands are executed in the a-Shell app. Once you're there, either "cat temp.txt" executed in extension or get File should work.

We get the stdout of all shell commands executed in Extension in shortcuts, but for commands executed in App (as all those that use Python), we need the "open Shortcuts://" to get back to Shortcuts.

@nipunbatra
Copy link

Thanks @holzschu I got it to work :)

I wrote about it here: https://nipunbatra.github.io/blog/setup/2021/06/14/setup-ipad.html#shortcuts

Is there an easier way? Also, I had to add the delay of 1 second. Otherwise, sometimes it didn’t work for me as “temp.txt” would not be found.

@holzschu
Copy link
Owner

I don't think there's an easier way. Shortcuts that run "in Extension" don't get access to the files in app main dir, so "open Shortcuts://" is the best way. You could probably wait in the app instead of in the shortcut, with "sleep 0.1" before "open Shortcuts://".

@ifuchs
Copy link

ifuchs commented Jun 15, 2021 via email

@nipunbatra
Copy link

Thanks, this is super useful. I have modified my post to include the pbcopy based version too.

Post: https://nipunbatra.github.io/blog/setup/2021/06/14/setup-ipad.html#shortcuts

I am pasting the images here as well in case anyone wants to use

Using pbcopy

Shortcut-2

Using files

Shortcut-Working

@ifuchs
Copy link

ifuchs commented Jun 15, 2021 via email

@ifuchs
Copy link

ifuchs commented Jul 3, 2021

I feel as though I've been down this path before but how do I do a Get File from a Shortcut and get back a file that is in the apps directory?

@NorthIsUp
Copy link

Is there any chance of getting a shortcut command "wait for a-shell commands to finish"?

I'm using a-shell to run scripts that download files that take between seconds and minutes.

@holzschu
Copy link
Owner

holzschu commented Jun 9, 2023

If I understood the question correctly, you can do that by launching a set of commands with Shortcuts: first, your download command, then "open shortcuts://" to go back to Shortcuts and continue the shortcut execution.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants