简体   繁体   English

如何使用 python + applescript 脚本在特定目录中打开 Mac 终端

[英]How to open Mac terminal at a specific directory using python + applescript script

    from applescript import tell 
 
    command = 'cd Downloads/"SSR Drive"'
    yourCommand = "PERF -i "+str(x)+" -u CFG.txt"
    tell.app( 'Terminal', 'do script "' + command + '"')
    tell.app( 'Terminal', 'do script "' + yourCommand + '"')

I am using this code to execute commands on Mac Terminal with my python script.我正在使用此代码通过我的 python 脚本在 Mac 终端上执行命令。

"command" tells the terminal to open the directory “command”告诉终端打开目录

"yourCommand" tells the terminal to execute a biopython module function. “yourCommand”告诉终端执行一个 biopython 模块 function。

But when I execute, the commands are executed on separate Terminal windows.但是当我执行时,命令在单独的终端 windows 上执行。 Resulting in "command" going to the directory, but then "yourCommand" is also executed in a new Terminal.导致“command”进入目录,但随后“yourCommand”也在新终端中执行。 So the directory change I specified with "command" doesn't take effect.所以我用“command”指定的目录更改没有生效。 Hence, "yourCommand" can't find the file.因此,“yourCommand”找不到该文件。

I read you can change the default for Terminal to open the current directory instead of the default directory, but that seems like a crude solution.我读到您可以更改终端的默认值以打开当前目录而不是默认目录,但这似乎是一个粗略的解决方案。

What I'd like to do is open Terminal on a specified directory and then run the "yourCommand" code in the terminal.我想做的是在指定目录上打开终端,然后在终端中运行“yourCommand”代码。

TIA TIA

Simply join the commands to be a single one with && .只需使用&&将命令连接成一个命令即可。

from applescript import tell 
 
command = 'cd Downloads/"SSR Drive"'
yourCommand = "PERF -i "+str(x)+" -u CFG.txt"
tell.app( 'Terminal', 'do script "' + command + ' && ' + yourCommand + '"')

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

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