/serve/save_script.py

https://github.com/tdamdouni/Pythonista · Python · 50 lines · 44 code · 1 blank · 5 comment · 0 complexity · 969ad2dce5f6998631d1295206d5869c MD5 · raw file

  1. # http://omz-forums.appspot.com/pythonista/post/5862307733176320
  2. # https://github.com/mncfre/Save-Script/blob/master/save_script.py
  3. ''' Save-Script
  4. # This is a python script that work in conjunction with a workflow (you need Workflow for ios) to enable open scripts .py (Only .py for now) and save it directly on Pythonista The workflow link is: https://workflow.is/workflows/8cdee57f79664205a6a565c9cbdb3d48
  5. # Updated: Script updated to ask destiny extension, using the @JonB suggestion, so if you have a .pyui change its extension to .py then "open in" then in pythonista select .pyui extension, its not perfect but its all I have now.
  6. # Workflow: https://workflow.is/workflows/8cdee57f79664205a6a565c9cbdb3d48
  7. # This script allows you to copy a .py script to the iOS clipboard and then use Open In...
  8. to have that script saved in Pythonista. This requires both the Workflow and Pythonista apps
  9. and the workflow at https://workflow.is/workflows/8cdee57f79664205a6a565c9cbdb3d48 '''
  10. import clipboard
  11. import console
  12. import os
  13. import sys
  14. def save(filename, text, ext):
  15. root, _ = os.path.splitext(filename)
  16. extension = ext
  17. filename = root + extension
  18. filenum = 1
  19. while os.path.isfile(filename):
  20. filename = '{} {}{}'.format(root, filenum, extension)
  21. filenum += 1
  22. #print(finalname)
  23. with open(filename,'w') as f:
  24. f.write(text)
  25. #clipboard.set(filename)
  26. return filename
  27. def main():
  28. resp = console.alert('Alert!', 'Choose File Extension', '.py', '.pyui', hide_cancel_button=False)
  29. if resp==1:
  30. ext = '.py'
  31. elif resp==2:
  32. ext = '.pyui'
  33. text = clipboard.get()
  34. assert text, 'No text on the clipboard!'
  35. filename = sys.argv[1]
  36. console.clear()
  37. print('Wait a Moment Please!')
  38. filename = save(filename, text, ext)
  39. console.set_font('Futura', 16)
  40. print('Done!\nFile Saved as:\n' + filename)
  41. if __name__ == '__main__':
  42. main()