Skip to content

Instantly share code, notes, and snippets.

@westc
Last active August 24, 2019 05:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save westc/27a3d079dea81337ccd3180096c96939 to your computer and use it in GitHub Desktop.
Save westc/27a3d079dea81337ccd3180096c96939 to your computer and use it in GitHub Desktop.
Shortcut functions that make system supported dialogs work.
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

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')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment