MCPcopy Create free account
hub / github.com/chasingboy/Xtools / _run

Function _run

applescript/_run.py:7–22  ·  view source on GitHub ↗

run script file/string

(script, background=False, javascript=False)

Source from the content-addressed store, hash-verified

5from ._temp import _tempfile
6
7def _run(script, background=False, javascript=False):
8 """run script file/string"""
9 if not background:
10 args = ["osascript", "-l", "JavaScript", "-"] if javascript else ["osascript", "-"]
11 kwargs = dict(stdin=subprocess.PIPE,stdout=subprocess.PIPE,stderr=subprocess.PIPE)
12 else:
13 f = script if os.path.exists(script) else _tempfile()
14 if not os.path.exists(script):
15 open(f,'w').write(script)
16 args = ["osascript", "-l", "JavaScript", f] if javascript else ["osascript", f]
17 kwargs = {'stdout':open(os.devnull, 'wb'),'stderr':open(os.devnull, 'wb')}
18 proc = subprocess.Popen(args,**kwargs)
19 if not background:
20 cmd = open(script).read() if os.path.exists(script) else script
21 out, err = proc.communicate(input=cmd.encode("utf-8"))
22 return Result(proc.returncode,out, err)

Callers 1

appFunction · 0.90

Calls 2

_tempfileFunction · 0.85
ResultClass · 0.85

Tested by

no test coverage detected