如果应用程序正在运行,如何在不启动它的情况下检查 AppleScript - 通过 osascript 实用程序 [英] How to check in AppleScript if an app is running, without launching it - via osascript utility

查看:32
本文介绍了如果应用程序正在运行,如何在不启动它的情况下检查 AppleScript - 通过 osascript 实用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑以下 AppleScript:

Consider the following AppleScript:

on is_running(appName)
    tell application "System Events" to (name of processes) contains appName
end is_running

set safRunning to is_running("Safari")
if safRunning then
    tell application "Safari"
      -- Stuff I only want executed if Safari is running goes here.
    end tell
    return "Running"
else
    return "Not running"
end if

问题:当我通过 osascript 命令行实用程序运行它时,如果 Safari 没有运行,它会启动并且脚本报告正在运行".这不是我想要或期望的行为.请注意,在 AppleScript 编辑器中运行时,它会按预期/预期工作.

The problem: when I run this via the osascript command line utility, if Safari is not running, it gets launched and the script reports "Running". This is not the behaviour I desire or would expect. Note that it works as desired/expected when run within AppleScript Editor.

这是 osascript 错误/已知问题吗?或者它是否出于某种原因而有意为之?任何人都可以让它按预期工作吗?(顺便说一句,我运行的是 OSX 10.7.5;我看不到如何让 osascript 报告版本号).

Is this an osascript bug / known issue? Or is it somehow intended behaviour for reasons I'm missing? Can anyone get it to work as desired? (BTW I'm running OSX 10.7.5; I can't see how to get osascript to report a version number).

如果您注释掉 tell/end tell 行,它的行为与我预期的一样:如果 Safari 未运行,则不会启动它,并且打印未运行".所以在我看来似乎就像 tell 是什么导致 Safari 被启动,但它不需要实际执行,只是出现在脚本中......?有一段时间我想知道这是否就是 tell 应该如何工作,但由于它在 AppleScript 编辑器中像这样工作,我想不是......

If you comment out the tell / end tell lines, it behaves as I'd expect: if Safari is not running, it doesn't launch it, and prints "Not running". So it seems to me like the tell is what's causing Safari to be launched, but it doesn't need to be actually executed, just present in the script...? For a while I wondered if maybe this was just how tell is supposed to work, but since it doesn't work like this in AppleScript Editor, I guess not...

事实上,这是另一个具有类似行为的更疯狂的版本:

In fact, here's another, madder, version with similar behaviour:

on is_running(appName)
    tell application "System Events" to (name of processes) contains appName
end is_running

set safRunning to is_running("Safari")
return safRunning
if false then
    tell application "Safari"
    end tell
end if

即使 tell 在 return 语句之后的 if false 块中,这仍然总是启动 Safari!(但同样,这在 AppleScript 编辑器中很好.)

This still always launches Safari, even though tell is inside an if false block after the return statement! (But again, this is fine in AppleScript Editor.)

顺便说一句,这种行为不仅限于 Safari,而且也不普遍:

BTW, this behaviour isn't limited to Safari, but it also isn't universal:

  • 受影响的应用包括:Safari、TextEdit、iPhoto、AppleScript Editor、iTerm、...
  • 未受影响的应用包括:Google Chrome、iTunes、Preview、Mail、终端、地址簿、Echofon...

那么,有没有人对我如何解决或解决此问题有任何想法?它是 osascript 错误吗?还是我遗漏了 AppleScript 的语义?

So, does anyone have any ideas about how I might fix or route around this? Is it an osascript bug? Or am I missing something about AppleScript's semantics?

对于上下文:我正在尝试编写一个脚本(从某些 Python 中嵌入/调用),该脚本在打开的浏览器中查询已打开的任何选项卡的 URL;我一切正常除了它总是启动 Safari,无论它是否打开.我已经将这种不良行为归结为上面显示的简单测试用例.除了 appscript 之外,我不知道有什么方法可以在不使用 osascript 的情况下从 python 运行这个脚本,我不想使用它,因为 它不再被开发/支持/推荐.

For context: I'm trying to write a script (to be embedded/called from some python) which queries open browsers for the URLs of any tabs they have open; I've got it all working fine except that it always launches Safari, whether it's open or not. I've boiled down that undesirable behaviour to the simple test case shown above. I'm not aware of any way to run this script from python without using osascript, other than appscript, which I don't want to use because it's no longer developed/supported/recommended.

非常感谢所有输入/见解!

Many thanks for all inputs / insights!

推荐答案

我怀疑你得到这个的原因是因为每次你用 osascript 从命令行调用脚本时,脚本都会被编译.

I suspect the reason you are getting this is because each time you call the script from the command line with osascript the script is being compiled.

tell 应用程序上编译 的行为将使应用程序启动.

The act of compiling on a tell application will afaik make the app launch.

使用 osascript 从预编译文件(即scpt)从命令行调用脚本不会导致此行为,因为无需进行编译.

Calling the script from the command line with osascript from a pre-compiled file i.e .scpt does not cause this behaviour because the is no compiling to be done.

但是从纯文本 (.txt,.sh) 文件中调用它会启动应用程序.

But calling it from a plain text (.txt,.sh ) file will so the app will launch.

如果您不想使用 .scpt 文件而想使用纯文本文件,那么您可以尝试将 run script 命令放入 applescript 中的技巧.

If you do not want to use a .scpt file and want to use a plain text file then you could try the trick of putting a run script command in the applescript.

on is_running(appName)
    tell application "System Events" to (name of processes) contains appName
end is_running

set safRunning to is_running("Safari")
if safRunning then
    run script "tell application \"Safari\" 
    
open location \"http://google.com\"
     
    end tell"
    return "Running"
else
    return "Not running"
end if

运行脚本中的脚本仅在需要时才编译.在我的示例中,您需要转义引号等任何字符.

The script in the run script is only compiled when needed. You will need to escape any characters like quotes as in my example.

如果你先把脚本写在一个普通的applescript文档中,然后编译它来检查错误会更容易.

It will be easier if you write the script in a normal applescript document first and compiled it to check for errors.

然后将其复制到纯文本文件中.

Then copy it to the plain text file.

更新 **

我上面使用的方法来自一个旧脚本,在我在这里回答之前,我曾经用来解决这个问题.

The method I used above was from a old script I had used to solved this issue a while before I answered here.

答案有效,并没有试图优雅.;-)

The answer works and is not trying to be elegant. ;-)

我实际上喜欢下面的user1804762方法.因为它确实有效,但感觉答案不够清楚,所以我将举一个使用它的例子.

I actually like user1804762 method below. As it does work but feel the Answer is not clear enough so I will give an example on using it.

set appName to "Safari"

if application appName is running then
    
    tell application id (id of application appName)
        
        open location "http://google.com"
    end tell
    return "Running"
else
    return "Not running"
end if

这个脚本可以用 osascript

示例:

osascript/Users/USERNAME/Desktop/foo.scpt

请注意,该脚本已保存为已编译的脚本.这将正常工作,您也可以将其保存并用作纯文本脚本.

Notice that the script is saved as a compiled script. This will work ok and you can also save and use it as a plain text script.

osascript/Users/USERNAME/Desktop/foo.applescript

这篇关于如果应用程序正在运行,如何在不启动它的情况下检查 AppleScript - 通过 osascript 实用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆