如何告诉终端在窗口中执行脚本,我刚刚打开

发布于 2025-02-01 17:34:50 字数 356 浏览 4 评论 0原文

我尝试了这个

tell application "Terminal"
    set custom to open "/Users/jredfox/Desktop/h.terminal"
    do script "/Users/jredfox/Desktop/test.sh" in custom
    activate
end tell

只是说失败了。我不想使用硬码窗口1,因为窗口1并不总是是我刚刚打开的自定义窗口。

我的目标是用自定义配置文件打开终端,然后在其中运行一个脚本。我是AppleScript的新手,不明白为什么这不起作用。我想使用刚刚打开的窗口,而不是窗口1,该窗口有时会失败,并且不支持多个窗口

I tried this

tell application "Terminal"
    set custom to open "/Users/jredfox/Desktop/h.terminal"
    do script "/Users/jredfox/Desktop/test.sh" in custom
    activate
end tell

this just says failed. I don't want to hard code window 1 because window 1 isn't always going to be the custom window I just opened.

My Goal is to open up the terminal with a custom profile then run a script inside of it. I am new to AppleScript and don't understand why this isn't working. I want to use the window that just got opened not window 1 which fails sometimes and doesn't support multiple windows

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

余生再见 2025-02-08 17:34:50

编程时,一个有用的习惯是关注您添加任何陌生代码后实际发生的事情。在AppleScript中执行此操作的快速方法是Result变量。命令的返回值转到结果,但您也可以明确设置它。 结果中的值在脚本编辑器的默认窗口的底部显示。

例如,我仅在您的脚本的这一部分粘贴了一个调整:

tell application "Terminal"
    set result to open "/Users/jredfox/Desktop/h.terminal"
end tell

执行时间太长了,结果是是缺失值。因此,马上,我们知道什么都不会起作用。但为什么?

我从“文件”菜单中选择了“打开字典……”,然后选择终端。终端似乎使用open命令,这意味着默认情况下它需要一个苹果路径,而不是posix路径。

set result to open "Macintosh HD:Users:jredfox:Desktop:h.terminal"

好吧,它立即开始打开,但即使是result不正确,这也无济于事。我认为Open命令似乎没有返回对新窗口的引用,正如您所期望的那样。

尝试result sa多一点,(在字典中读取do script中有一个返回值);哦,如果您愿意假设h.terminal将始终在不到一秒钟内成功打开,因此将是窗口1;我将您的脚本重新启动到以下:

tell application "Terminal"
    activate
    open "Macintosh HD:Users:jredfox:Desktop:h.terminal"
    delay 1
    set custom to (do script "/Users/jredfox/Desktop/test.sh" in window 1)
end tell

One helpful habit when programming is to keep an eye on what actually happens after you add any unfamiliar code. The quick way to do this in AppleScript is with the result variable. Commands' return values go to result, but you can also explicitly set it for clarity. The value in result is shown at the bottom of the default window in Script Editor.

For example, I pasted in just this portion of your script, with a tweak:

tell application "Terminal"
    set result to open "/Users/jredfox/Desktop/h.terminal"
end tell

Execution took waaay too long, and the result was missing value. So right off the bat we know nothing else is going to work. But why?

I chose "Open Dictionary…" from the "File" menu, and chose Terminal. It seems Terminal uses the open command from the Standard Suite, which means it by default wants an AppleScript path, not a POSIX path.

set result to open "Macintosh HD:Users:jredfox:Desktop:h.terminal"

Well, it opened instantly now, but even that doesn't help as result isn't right. It seems the open command does not return a reference to the new window, as I believe you were expecting.

Experimenting with results a bit more, (and reading in the dictionary that there is a return value from do script); oh, and if you're willing to assume that h.terminal will always open successfully in less than one second, and will therefore be window 1; I rejiggered your script to the following:

tell application "Terminal"
    activate
    open "Macintosh HD:Users:jredfox:Desktop:h.terminal"
    delay 1
    set custom to (do script "/Users/jredfox/Desktop/test.sh" in window 1)
end tell
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文