Welcome!
This is the community forum for my apps Pythonista and Editorial.
For individual support questions, you can also send an email. If you have a very short question or just want to say hello — I'm @olemoritz on Twitter.
Is it possible to read a file, say txt file, from other app?
-
Let say that we have a file named 'a.txt' saved in goodreader app. Can we read this file from pythonista without importing it? Using "Dropbox file picker" is th only way I figured out to import a file to pythonista...
-
It depends on whether goodreader supports a Share Sheet in a real way.
Does GoodReader have the "share" button? If so, try clicking it, then "Run Pythonista Script". Then, click on Scratchpad and type
import appex print appex.get_attachments()
then press play. If goodreader actually supports sharing, then you will see a file of the form /private/..... you should be able to then use
text=open(appex.get_attachments()[0]).read() open('a.txt','w').write(text)
then it should be there in pythonista when you return to the app. This of course can all be put into a script that you add to your app extension list of saved scripts to avoid some of the tedium.
-
Yes, GoodReader opens a share sheet when you tap "Open in ...".
@JonB, if you want to copy a file, use
shutil.copy
. Much shorter than manually opening the two files (and you don't risk any accidental bytes-to-text conversions for binary files). -
The reason you cannot read from another app (without jailbreak) is that all apps in iOS are sandboxed, and cannot access much content outside of their own app bundle.
-
In this case, appex delivers neither a file handle nor a file name but instead delivers the contents of the file as a str.
https://github.com/cclauss/Ten-lines-or-less/blob/master/appex_local_copy.py should give you a local copy.
-
@ccc Really?
>>> import appex >>> appex.get_attachments() [u"/var/mobile/Containers/Data/Application/E2D89A6E-FD16-4428-BAC4-2E01276796E6/Documents/What's new in GoodReader 4.11.pdf", u"/var/mobile/Containers/Data/Application/E2D89A6E-FD16-4428-BAC4-2E01276796E6/Documents/What's new in GoodReader 4.11.pdf"]
(Don't ask me why the same path is listed twice though.)
-
i suspect for textfiles, goodreader returns the path as one attachment, and the url as another. I noticed this when safari does a Save As (in my case, i was saving a png, and i got the file contents on one, and file path in another. My script just uses get url, and urllib.urlretrieve, which seems to work both for private pathnames and internet urls. .
-
This makes a little more sense... but only a little.
>>> import appex >>> import json >>> print(json.dumps(appex.get_input(), indent=4)) [ { "attachments": [ { "com.adobe.pdf": "/var/mobile/Containers/Data/Application/E2D89A6E-FD16-4428-BAC4-2E01276796E6/Documents/What's new in GoodReader 4.11.pdf", "public.file-url": "/var/mobile/Containers/Data/Application/E2D89A6E-FD16-4428-BAC4-2E01276796E6/Documents/What's new in GoodReader 4.11.pdf" } ] } ]
-
My mistake... I was reading a file shared from Editor, not from GoodReader.
import appex, shutil if appex.is_running_extension(): file_path = appex.get_file_path() if file_path: shutil.copy(file_path, '.')
-
To the original question - it would be cool if there was a way to open a file from a document provider in another app. Sort of as described in this blog post, where 1Writer is using the document provider from Working Copy to open a file in-place (i.e. without making a new copy):
http://www.rassoc.com/gregr/weblog/2016/01/15/using-working-copy-with-1writer-on-ipad-pro/
-
Thank you very much every body fir all of your grat suggestion...they are really a great help.
-
Or see Save Attachment