Skip to content

Instantly share code, notes, and snippets.

@bron84
Created June 15, 2016 06:12
Show Gist options
  • Select an option

  • Save bron84/18c35954c23ae31685c748f39e4f8c23 to your computer and use it in GitHub Desktop.

Select an option

Save bron84/18c35954c23ae31685c748f39e4f8c23 to your computer and use it in GitHub Desktop.
save_py3.py
# coding: utf-8
import appex, console
import ui
import os.path
import os
import clipboard
import gc
import shutil
def loadFolder(sender): #Populate a new list view with the contents of a selected folder and push to the navigation view
selection = sender.items[sender.selected_row]
path = os.path.join(sender.path, selection)
new_view = ui.TableView()
new_view.name = selection
new_data = ui.ListDataSource(next(os.walk(path))[1])
new_data.action = loadFolder
new_data.path = path
new_view.data_source = new_data
new_view.delegate = new_data
sender.tableview.navigation_view.push_view(new_view)
def find_current_view(nv): #Find the currently open folder when the select button is pushed
for v in gc.get_objects():
if hasattr(v, 'navigation_view') and v.navigation_view == nv and not v.superview:
return v
return None
def makeSelection(sender): #Select the currently open folder and move on to the saving process
current_view = find_current_view(picker).data_source.path
picker.close()
clipboard.set(current_view)
return current_view
def folder_picker(): #Initialize the folder picker
# home_dir = os.getcwd().split('Documents/')[0] + 'Documents/'
home_dir = os.getcwd().split('Documents/')[0] + '/'
top = ui.TableView()
top.name = '~/Documents'
top_data = ui.ListDataSource(next(os.walk(home_dir))[1])
top_data.action = loadFolder
top_data.path = home_dir
top.data_source = top_data
top.delegate = top_data
select_button = ui.ButtonItem(action = makeSelection, title = 'Select')
main = ui.NavigationView(top)
main.right_button_items = [select_button]
main.current_view = home_dir
return main
def save(folder):
if appex.is_running_extension():
sFp = appex.get_file_path()
if sFp:
console.hud_alert('Saving...')
print('Destination folder: ', os.path.join(folder, os.path.basename(sFp)))
shutil.copy(sFp, folder)
console.hud_alert('Saved')
if __name__ == '__main__':
picker = folder_picker()
picker.present('sheet')
picker.wait_modal()
folder = clipboard.get()
save(folder)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment