【问题标题】:Quit an applescript application after the execution of a shell script执行 shell 脚本后退出 applescript 应用程序
【发布时间】:2012-07-02 09:46:21
【问题描述】:

我想知道 applescript 应用程序是否可以运行 shell 脚本,然后在 shell 脚本执行完成之前退出。在给定时间运行服务器时,这将很有用。不需要applescript一直在后台运行,有没有办法独立运行一个函数?

set server_name to text returned of (display dialog "Choose server." default answer "")
set success to do shell script "if [ -f \"/Users/jessefrohlich/Documents/Minecraft/" & server_name & "/minecraft_server.jar\" ]; then
    echo 1;
else
    echo 0;
fi"
if success is equal to "1" then
    do shell script "cd /Users/jessefrohlich/Documents/Minecraft/" & server_name & "
    java -Xmx1024M -Xms1024M -jar minecraft_server.jar"
else
    display dialog "Sorry, the file you chose is invalid."
end if

当上述作为应用程序运行时,它将正确启动服务器。但是,应用程序runScript.app 将继续运行。即使 applescript 被强制退出,服务器也会继续运行。有什么办法让它在服务器启动后自动退出?

谢谢

【问题讨论】:

    标签: shell applescript quit nonlinear-functions


    【解决方案1】:

    试试这个。祝你好运。

    -- "> /dev/null" redirects standout out to nowhere land
    --      - you can use some other file path if you want to capture its output
    -- "2>&1" redirects standard error to the same place as standard out
    --      - 2 stands for standard error
    --      - 1 stands for standard out
    --      - > is the redirect symbol
    --      - & changes redirect's output from a file to a file descriptor (in this case standard out)
    -- & the trailing & sends the process to the background
    
    do shell script "cd /Users/jessefrohlich/Documents/Minecraft/" & server_name & " java -Xmx1024M -Xms1024M -jar minecraft_server.jar > /dev/null 2>&1 &"
    

    【讨论】:

      【解决方案2】:

      您可以向您的 Applescript 添加一个条件,使其“忽略应用程序响应”,然后它会继续处理您的 AppleScript 中的任何其他内容,包括退出它。

      Apple 网站有详细信息:https://developer.apple.com/library/mac/#documentation/applescript/conceptual/applescriptlangguide/reference/ASLR_control_statements.html

      【讨论】:

      • ignoring application responses。此控制语句不适用于do shell script 命令。
      • 谢谢,我不知道。正确的方法是将脚本放在后台并将响应重定向到文件或 /dev/null。请参阅此处称为“其他问题”的部分:developer.apple.com/library/mac/technotes/tn2065/_index.html
      猜你喜欢
      • 1970-01-01
      • 2017-02-03
      • 2015-10-31
      • 1970-01-01
      • 2011-08-03
      • 2019-06-23
      • 1970-01-01
      • 2011-05-21
      • 1970-01-01
      相关资源
      最近更新 更多