Create a gist now

Instantly share code, notes, and snippets.

@westc /Readme.md
Last active May 17, 2017

What would you like to do?
Shortcut functions that make system supported dialogs work.

Dialog Box For Choosing Files

osa_dialogs.choose_file(prompt='Select a file:', file_types=[], multipleFiles=False)
  • Example
    # Select 1 or more images
    pprint(osa_dialogs.choose_file(prompt="Select 1 or more images:", file_types=['public.image'], multipleFiles=True))

Dialog Box For Choosing Folders

osa_dialogs.choose_folder(prompt='Select a folder:', multipleFolders=False)
  • Example
    # Select 1 or more folders
    pprint(osa_dialogs.choose_file(prompt="Select 1 or more folders:", multipleFolders=True))

Dialog Box With Input Box

osa_dialogs.input(message, title='', defaultValue='', icon=None)
  • Example
    print osa_dialogs.input('What is your name?', 'Name', 'Chris', icon='caution')

Dialog Box With Custom Buttons

osa_dialogs.prompt(prompt, title='', buttons=['OK'], default_index=None, cancel_index=None, icon=None)
  • Example
    # Show prompt
    print osa_dialogs.prompt('Hi, I hope you are doing well today!', 'Hello', ['Yes','No','Cancel'], default_index=0, cancel_index=2, icon='note')
import json, subprocess
def choose_file(prompt='Select a file:', file_types=[], multipleFiles=False):
args = [
'osascript',
'-e',
'%s(choose file%s with prompt "%s" of type{%s})' % (
'set theList to ' if multipleFiles else 'POSIX path of ',
' with multiple selections allowed' if multipleFiles else '',
json.dumps(prompt)[1:-1],
json.dumps(file_types)[1:-1]
)
]
if multipleFiles:
args += [
'-e',
'repeat with a from 1 to length of theList',
'-e',
'set item a of theList to POSIX path of item a of theList',
'-e',
'end repeat',
'-e',
'set AppleScript\'s text item delimiters to ";"',
'-e',
'theList as string'
]
output, error = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE).communicate()
output = output.strip()
return output.split(';') if len(output) else []
def choose_folder(prompt='Select a folder:', multipleFolders=False):
args = [
'osascript',
'-e',
'%s(choose folder%s with prompt "%s")' % (
'set theList to ' if multipleFolders else 'POSIX path of ',
' with multiple selections allowed' if multipleFolders else '',
json.dumps(prompt)[1:-1]
)
]
if multipleFolders:
args += [
'-e',
'repeat with a from 1 to length of theList',
'-e',
'set item a of theList to POSIX path of item a of theList',
'-e',
'end repeat',
'-e',
'set AppleScript\'s text item delimiters to ";"',
'-e',
'theList as string'
]
output, error = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE).communicate()
output = output.strip()
return output.split(';') if len(output) else []
def input(message, title='', defaultValue='', icon=None):
process = subprocess.Popen(
[
'osascript',
'-e',
'display dialog "%s" default answer "%s" with title "%s"%s' % (
json.dumps(message)[1:-1],
json.dumps(defaultValue)[1:-1],
json.dumps(title)[1:-1],
((' with icon %s' % icon) if icon in ['caution', 'note', 'stop'] else (' with icon POSIX file %s' % json.dumps(icon))) if icon else ''
),
'-e',
'text returned of result'
],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE
)
output, error = process.communicate()
return output.strip()
def prompt(prompt, title='', buttons=['OK'], default_index=None, cancel_index=None, icon=None):
process = subprocess.Popen(
[
'osascript',
'-e',
'display dialog "%s" buttons {%s}%s%s with title "%s"%s' % (
json.dumps(prompt)[1:-1],
json.dumps(buttons)[1:-1],
'' if default_index is None else (' default button %s' % json.dumps(buttons[default_index])),
'' if cancel_index is None else (' cancel button %s' % json.dumps(buttons[cancel_index])),
json.dumps(title)[1:-1],
((' with icon %s' % icon) if icon in ['caution', 'note', 'stop'] else (' with icon POSIX file %s' % json.dumps(icon))) if icon else ''
)
],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE
)
output, error = process.communicate()
output = ('Cancel' if cancel_index is None else buttons[cancel_index]) if error else output[output.index(':')+1:].strip()
try:
return buttons.index(output)
except Exception, e:
return -1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment