首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用[[NSAppleScript alloc] initWithSource检查app是否正在运行:

如何使用[[NSAppleScript alloc] initWithSource检查app是否正在运行:
EN

Stack Overflow用户
提问于 2011-09-07 11:54:28
回答 3查看 1.3K关注 0票数 0

这是我的原始脚本。它将返回Safari的当前url。

代码语言:javascript
复制
NSAppleScript *scriptURL= [[NSAppleScript alloc] initWithSource:@"tell application \"Safari\" to return URL of front document as string"];

如果我想在要求脚本返回URL之前检查Safari浏览器是否打开,该怎么办?

下面是我在applescript编辑器中的操作。因此,此脚本将检查Safari是否正在运行。这在applescript编辑器中有效

代码语言:javascript
复制
tell application "Safari"
        if it is running then

            //return url code here
        end if
    end tell

我现在需要的是通过使用‘[NSAppleScript alloc:’从我的可可应用程序中直接调用脚本。‘

我试过了,但不起作用

代码语言:javascript
复制
NSAppleScript *scriptURL= [[NSAppleScript alloc] initWithSource:@"tell application \"Safari\" if it is running to return URL of front document as string"];
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2011-09-07 12:05:31

为什么会这样呢?这是糟糕的AppleScript语法。

有一些方法可以在不求助于AppleScript的情况下做到这一点,但目前它已经足够了。通过使用C转义序列\n插入换行符,可以在嵌入式脚本中包含多行:

代码语言:javascript
复制
NSString *source = @"tell application \"Safari\"\nif it is running then\nreturn URL of front document as string\nend if\nend tell";

您还可以通过将一个字符串常量放在另一个字符串常量之后来拆分字符串常量,这使其更易于阅读:

代码语言:javascript
复制
NSString *source =
    @"tell application \"Safari\"\n"
        "if it is running then\n"
            "return URL of front document as string\n"
        "end if\n"
    "end tell";

C编译器将把这些字符串常量粘合到一个NSString对象中。

票数 4
EN

Stack Overflow用户

发布于 2013-07-25 00:34:40

操作员的AppleScript one线性代码错误。其中的AppleScript文本应该是有效的:

代码语言:javascript
复制
NSAppleScript *scriptURL= [[NSAppleScript alloc] initWithSource:@"tell application \"Safari\" to if it is running then return URL of front document as string"];
票数 1
EN

Stack Overflow用户

发布于 2013-07-25 01:59:19

正如dougscripts (+1)所指出的,但我想让它更清楚地说明为什么OP尝试的NSAppleScript中的单线性AppleScript语法不起作用。

老实说,我建议进行一次编辑,结果以3比2输掉了比赛

OP的NSAppleScript代码:

代码语言:javascript
复制
NSAppleScript *scriptURL= [[NSAppleScript alloc] initWithSource:@"tell application \"Safari\" if it is running to return URL of front document as string"];

因为语法错误,所以不起作用。

正确的语法应该是:

代码语言:javascript
复制
NSAppleScript *scriptURL= [[NSAppleScript alloc] initWithSource:@"tell application \"Safari\" to if it is running then return URL of front document as string"];

在下面以粗体显示的部分代码中有两处更改。

\"Safari\“如果运行的是,则返回到

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/7328696

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档