明白了。感谢您提供了有用的AppleScript见解,帮助您找到了这个解决方案Zero。在from subprocess import Popen, PIPE, check_output, STDOUT
def runAppleScript(script):
"Returns the result of running string in osascript (Applescript)"
if hasattr(script, "encode"): #Assumes Python 3
script = script.encode("utf-8")
osa = Popen(["osascript", "-"], stdout=PIPE, stdin=PIPE, stderr=PIPE)
results, err = osa.communicate(script)
if err:
raise Exception(err)
return results.decode("utf-8")
def runBash(command):
output = check_output(command, stderr=STDOUT, shell=True)
return output
def getCurrentTerminalTTYS():
script = '''
tell application "Terminal"
return (tty of selected tab of front window)
end tell
'''
result = runAppleScript(script)
return result.strip()
def getPathForTTYS(ttys):
lsof = runBash('lsof').split('\n')
process = None
for line in lsof:
if ttys in line:
process = line.split()[1]
break
path = None
for line in lsof:
if 'cwd' in line and process in line:
path = ' '.join(line.split()[8:])
break
return path
def getCurrentTerminalPath():
ttys = getCurrentTerminalTTYS()
return getPathForTTYS(ttys)
它可以存储为字符串,使用
^{pr2}$