Forum Archive

How to open email Attachment with pythonista

rfausett

I'm trying to move from XCODE to PYTHONISTA and I could use a hand finding a way (in PYTHONISTA) to do the equivalent of long tapping an email attachment, selecting my XCODE built app and processing the file with the app.

cvp

@rfausett
Long press on your email attachment,
Select Pythonista
Select your app
Shortest code

import appex,os,shutil
fil = appex.get_file_path() 
local_path = os.path.expanduser('~/Documents/')
shutil.copy(fil,local_path)


djorge

Hi,
Where can i get some of those pythonista extension like Import Any File or Import file from web?

Thanks,
David

cvp

@djorge Sorry but it's a copy of my own scripts icons.
I'll clean my script and put a copy here.

cvp

@djorge You'll find hereafter my cleaned "import any file", it uses a File_Picker module of omz, you can find it here

# coding: utf-8
import appex
import console
import shutil
import os
import ui
import File_Picker
import unicodedata
import urllib.request, urllib.parse, urllib.error

def process():
    global my_back

    # called by sharing action or launcher
    if not appex.is_running_extension():
        return

    # Sharing: receive file to store in local folder
    fil = appex.get_file_path() 
    if fil == None:
        # no file shared
        url = appex.get_url()

        fil = urllib.request.urlopen(url)

        tot = fil.getheader('content-length')

        i = url.rfind('/') # index last /
        nam = url[i+1:] # right of last /


        block = 4096*1#6
        temp = os.path.join(os.path.expanduser("~"),'Documents/'+nam)   
        with open(temp, 'wb') as output:
            while True:
                data = fil.read(block)
                if data:
                    output.write(data)
                else:
                    break   # leave loop
        fil = temp
        local_file = nam

    else:
        # file shared
        temp = None
        local_file = os.path.basename(fil)

    # Select folder where to move the file
    to_dir = File_Picker.file_picker_dialog('select folder where to copy', multiple=False, select_dirs=True, file_pattern=r'^.*\.py$')
    if to_dir != None:
        w = '/Pythonista3/Documents/'
        i = to_dir.find(w)
        dir_loc = to_dir
        if i >= 0:
            dir_loc = to_dir[i+len(w):] 
        else:
            dir_loc = ''
    else:
        if temp:
            os.remove(temp)
        return

    # Remove accents of file name
    local_file = ''.join((c for c in unicodedata.normalize('NFD', local_file) if unicodedata.category(c) != 'Mn'))  
    try:
        local_file = console.input_alert('Import file\n'+fil+'\nin',  dir_loc, local_file,'ok')
        import_file = True
    except KeyboardInterrupt:
        import_file = False
    if not import_file:
        if temp:
            os.remove(temp)
        return

    local_path = os.path.join(os.path.expanduser("~"),'Documents/'+dir_loc) 
    local_path = os.path.join(local_path,local_file)    

    shutil.copy(fil,local_path)
    if temp:
        os.remove(temp)

def main():
    global my_back

    #----- Main code ------------------------
    console.clear()

    # Hide script
    #w, h = (540,620)
    my_back = ui.View(name='Import file')
    my_back.background_color='white'
    my_back.present('sheet',hide_title_bar=True)

    process()

    my_back.close()
    appex.finish()

if __name__ == '__main__':
    main()
djorge

@cvp thanks for sharing