omz:forum

    • Register
    • Login
    • Search
    • Recent
    • Popular

    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.


    dialogs.text_dialog: Select text in range

    Pythonista
    dialogs
    3
    3
    2673
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • lukaskollmer
      lukaskollmer last edited by

      I'm working on a script to upload files to the internet. Currently, I use dialogs.text_dialog to request a filename.

      The problem is, that, since I pass the current filename with the text parameter, I always have to select only the filename (and not the extension as well).

      Is there a way to tell dialogs to select a specific range in the text?
      If it doesn't exist right now, @omz please consider adding this to your todo list.

      PS: I already started working on a solution with objc_util, but I'd prefer to keep this simple.

      1 Reply Last reply Reply Quote 0
      • cook
        cook last edited by

        I'm a little confused about what you're trying to do...so I'm probably wrong :)

        If you're getting a file name/path and then putting that's default text in the text_dialogue, and then trying to select just the name of the file....
        You could use os.path to get just the file name before you throw it into the text_dialogue

        1 Reply Last reply Reply Quote 0
        • omz
          omz last edited by

          When you look at the source code of the dialogs module, you'll see that the text_dialog() function is actually very simple, and you could make your own version of it like this:

          import ui
          import os
          from dialogs import _TextDialogController
          
          def my_text_dialog(title='', text='', font=('<system>', 16), autocorrection=None, autocapitalization=ui.AUTOCAPITALIZE_SENTENCES, spellchecking=None, done_button_title='Done', selected_range=None):
          	c = _TextDialogController(title=title, text=text, font=font, autocorrection=autocorrection, autocapitalization=autocapitalization, spellchecking=spellchecking, done_button_title=done_button_title)
          	if selected_range is not None:
          		c.view.selected_range = selected_range
          	c.view.present('sheet')
          	c.view.begin_editing()
          	c.view.wait_modal()
          	return c.text
          
          filename = 'myfile.txt'
          base_name, extension = os.path.splitext(filename)
          my_text_dialog('Test', filename, selected_range=(0, len(base_name)))
          

          This is mostly the same code as the original text_dialog(), just with an additional selected_range parameter.

          1 Reply Last reply Reply Quote 1
          • First post
            Last post
          Powered by NodeBB Forums | Contributors