简体   繁体   English

使用python更改Mac的终端主题

[英]Change mac's terminal theme using python

How exactly can I change the theme of a mac terminal using python. 我如何才能使用python更改mac终端的主题。 I have a command line program and I want to have a specific theme for the terminal (other than the basic theme) as my command-line program starts. 我有一个命令行程序,并且我希望在命令行程序启动时为终端设置一个特定的主题(基本主题除外)。

You can use the Python subprocess module to call an AppleScript : 您可以使用Python subprocess模块来调用AppleScript

#!/usr/bin/python

import subprocess

def asrun(ascript):

    osasc = subprocess.Popen(['osascript', '-'],
    stdin = subprocess.PIPE, stdout=subprocess.PIPE)
    return osasc.communicate(ascript)[0]

def asquote(astr):

    ascrpt = astr.replace('"', '" & quote & "')
    return '"{}"'.format(ascrpt)

ascript = '''
tell application "Terminal"
    activate
    set current settings of tabs of windows to settings set "Pro"
end tell
'''

asrun(ascript)

This will change all of the windows and tabs you currently have open. 这将更改您当前打开的所有窗口和选项卡。 If you want it do change just one and not the others, or change the window when you launch terminal that's fairly easy to do. 如果您希望只更改一个而不更改其他内容,或者在启动终端时更改窗口,这很容易做到。 It's just a matter of determining which window or tab you want to change and how you are calling the script in the first place. 只需确定要更改的窗口或选项卡以及首先调用脚本的方式即可。 This should give you an idea though of the basic means of how it works — so I've left this example fairly minimal so you can understand the basics of it. 这应该使您对它的工作原理有了一个基本的了解-因此,我将本示例的内容保持在最小限度,以便您可以理解其基本知识。

To change the profile, substitute "Pro" with any profile name (even custom versions you've created) that are listed in Terminal.app. 要更改配置文件,请用Terminal.app中列出的任何配置文件名称(甚至是您创建的自定义版本)替换“ Pro”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM