我如何等待壳脚本完成?

发布于 2025-02-02 23:48:04 字数 874 浏览 5 评论 0原文

我正在AppleScript中使用命令行应用程序,并且我需要脚本等待该过程在继续之前运行。 (是的,我希望终端打开并查看应用程序运行。)

我基于现有问题

activate application "Terminal"
tell application "System Events"
    repeat until visible of process "Terminal" is true
    end repeat
end tell

tell application "System Events" to keystroke "app_name option"
tell application "System Events" to keystroke return
        
delay 2
        
    set thePID to do shell script "app_name option > /dev/null 2>&1 & echo $!"
        
    repeat
        do shell script "ps ax | grep " & thePID & " | grep -v grep | awk '{ print $1 }'"
        if result is "" then exit repeat
        delay 2
    end repeat

-- more code...

所有内容都在我的脚本中起作用,除了我需要等待命令行应用程序完成的部分。

我该如何完成这项工作?从开始设置thepid的零件到底是什么?

I'm using a command-line app in an AppleScript, and I need the script to wait for the process to finish running before continuing. (And yes, I want the terminal to open and to see the app run.)

I wrote this code, based on an existing question:

activate application "Terminal"
tell application "System Events"
    repeat until visible of process "Terminal" is true
    end repeat
end tell

tell application "System Events" to keystroke "app_name option"
tell application "System Events" to keystroke return
        
delay 2
        
    set thePID to do shell script "app_name option > /dev/null 2>&1 & echo $!"
        
    repeat
        do shell script "ps ax | grep " & thePID & " | grep -v grep | awk '{ print $1 }'"
        if result is "" then exit repeat
        delay 2
    end repeat

-- more code...

Everything is working in my script, except for the part where I need it to wait for the command-line app to complete.

How can I make this work? What exactly does the part starting with set thePID do?

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

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

发布评论

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

评论(1

偷得浮生 2025-02-09 23:48:04

为了使其正常工作,我删除了:

set thePID to do shell script "app_name option > /dev/null 2>&1 & echo $!"

然后在下重复我更改

do shell script "ps ax | grep " & thePID & " | grep -v grep | awk '{ print $1 }'"

为::

do shell script "ps ax | grep app_name | grep -v grep | awk '{ print $1 }'"

现在,我的脚本按照我的需求工作!

问题是我在脚本中通过按键调用该应用程序,然后通过运行设置shell脚本“ app_name option> /dev /dev /null 2> amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; & echo $!”

我认为答案可能是告诉应用程序“系统事件”以“键” app_name选项>/dev/null 2>& 1& echo $!”

我正在运行该程序的第二个实例,因为我不清楚如何正确利用我链接到的旧帖子中给出的示例。

因此,我四处搜索如何在终端中获取运行程序的pid,并且看到grep app_name是难题的常见作品。与PS -AX ...相同,

因此我遵循自己的假设,删除了要求该程序再次运行的变量,然后将该程序的名称插入grep替换变量的字符串的一部分。它起作用了。

To get this to work properly, I removed:

set thePID to do shell script "app_name option > /dev/null 2>&1 & echo $!"

Then under repeat I changed:

do shell script "ps ax | grep " & thePID & " | grep -v grep | awk '{ print $1 }'"

to:

do shell script "ps ax | grep app_name | grep -v grep | awk '{ print $1 }'"

And now my script works as I wanted it to!

The issue was that I was calling the app by keystroke in the script, and then later basically running a separate instance of it by running set thePID to do shell script "app_name option > /dev/null 2>&1 & echo $!".

I assume the answer may be something like tell application "System Events" to keystroke "app_name option > /dev/null 2>&1 & echo $!".

I was running a second instance of the program since I was unclear about how to properly utilize the example given in that old post I linked to.

So I searched around for how to get the PID of running program in the terminal, and I saw that grep app_name was a common piece to the puzzle. Same with ps -ax...

So I followed my own assumption, removed the variable that called for the program to run again, and simply inserted that program's name into the grep part of the string replacing the variable. And it worked.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文