Forum Archive

dialogs.text_dialog: Select text in range

lukaskollmer

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.

cook

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

omz

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.