如何检查的AppleScript如果一个应用程序正在运行,而无需启动它 - 通过osascript工具 [英] How to check in AppleScript if an app is running, without launching it - via osascript utility

查看:3359
本文介绍了如何检查的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).

如果您注释掉告诉 / 结束诉说行,它的行为,我期望:如果Safari浏览器没有运行,它不启动它,并打印未运行。因此,它的看起来的我像告诉是什么导致Safari浏览器推出,但它并不需要被实际执行,只要$ P在脚本$ psent ...?有一段时间,我想知道是否也许这是多么推荐应该工作,但因为它的的这样的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

这还是始终启动Safari浏览器,即使告诉是return语句后如有虚假块内! (但同样,这是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浏览器,文本编辑,iPhoto中,的AppleScript编辑器的iTerm,...

  • 非受影响的应用包括:谷歌浏览器,iTunes中,preVIEW,邮件,终端,地址簿,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调用)哪些查询为他们打开的标签页的网址,打开的浏览器;我已经得到了这一切工作正常的除了的,它总是启动Safari浏览器,无论是开放与否。我归结了不良行为上面显示的简单的测试案例。我不知道的任何方式,而不使用运行python的这个脚本 osascript ,除了 appscript ,我不想因为它不再开发/支持/推荐。

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.

编制上的告诉应用程序将AFAIK使应用程序启动的行为。

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

调用从从pre-编译的文件即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文件,并想用纯文本文件,那么你可以尝试把一个运行脚本命令,在的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

This script can be run from the command line with 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天全站免登陆