def get_selection_from_user(cls, title, prompt, choices, default=None, multiple=False): '''Popup a dialog to let a user select a value from a list of choices. The main use for this function is to request information that you don't want showing up in Alfred's command history. ''' if default is None: default = '' if not isinstance(choices, (tuple, list)): choices = [choices] choices = '{{"{0}"}}'.format('","'.join(choices)) if multiple: multiple = 'with' else: multiple = 'without' with open(os.path.join(BASE_DIR, 'get_selection.scpt')) as sfile: script = sfile.read().format(default=default, prompt=prompt, title=title, choices=choices, multiple=multiple) stdout, stderr = _run_script(script) LOG.debug('stdout: %s', stdout) LOG.debug('stderr: %s', stderr) response = stdout.rstrip('\n').strip('"') button, sep, value = response.partition('|') if button == 'Cancel': return None return value
def get_confirmation(self, title, prompt, default='No'): '''Display a confirmation dialog''' with open(os.path.join(BASE_DIR, 'get_confirmation.scpt')) as sfile: script = sfile.read().format(p=prompt, t=title, d=default) stdout, stderr = _run_script(script) if len(stderr) > 0: raise Exception(stderr) return stdout.rstrip('\n').strip('"')
def get_from_user(self, title, prompt, hidden=False, value=None, extra_buttons=None): '''Popup a dialog to request some piece of information. The main use for this function is to request information that you don't want showing up in Alfred's command history. ''' if value is None: value = '' buttons = ['Cancel', 'Ok'] if extra_buttons: if isinstance(extra_buttons, (list, tuple)): buttons = extra_buttons + buttons else: buttons.insert(0, extra_buttons) buttons = '{%s}' % ', '.join(['"%s"' % b for b in buttons]) hidden = 'with hidden answer' if hidden else '' script = ''' on run argv tell application "Alfred 2" activate set alfredPath to (path to application "Alfred 2") set alfredIcon to path to resource "appicon.icns" in bundle ¬ (alfredPath as alias) try display dialog "{p}:" with title "{t}" ¬ default answer "{v}" ¬ buttons {b} default button "Ok" with icon alfredIcon {h} set answer to (button returned of result) & "|" & ¬ (text returned of result) on error number -128 set answer to "Cancel|" end end tell end run'''.format(v=value, p=prompt, t=title, h=hidden, b=buttons) stdout, stderr = self.run_script(script) response = stdout.rstrip('\n') button, sep, value = response.partition('|') return (button, value)
def osascript_tell(app, script): p = Popen(['osascript'], stdin=PIPE, stdout=PIPE) stdout, stderr = p.communicate( 'tell application "{}"\n{}\nend tell'.format(app, script)) return stdout.rstrip('\n')