|
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 |