这是我的原始脚本。它将返回Safari的当前url。
NSAppleScript *scriptURL= [[NSAppleScript alloc] initWithSource:@"tell application \"Safari\" to return URL of front document as string"];如果我想在要求脚本返回URL之前检查Safari浏览器是否打开,该怎么办?
下面是我在applescript编辑器中的操作。因此,此脚本将检查Safari是否正在运行。这在applescript编辑器中有效
tell application "Safari"
if it is running then
//return url code here
end if
end tell我现在需要的是通过使用‘[NSAppleScript alloc:’从我的可可应用程序中直接调用脚本。‘
我试过了,但不起作用
NSAppleScript *scriptURL= [[NSAppleScript alloc] initWithSource:@"tell application \"Safari\" if it is running to return URL of front document as string"];发布于 2011-09-07 12:05:31
为什么会这样呢?这是糟糕的AppleScript语法。
有一些方法可以在不求助于AppleScript的情况下做到这一点,但目前它已经足够了。通过使用C转义序列\n插入换行符,可以在嵌入式脚本中包含多行:
NSString *source = @"tell application \"Safari\"\nif it is running then\nreturn URL of front document as string\nend if\nend tell";您还可以通过将一个字符串常量放在另一个字符串常量之后来拆分字符串常量,这使其更易于阅读:
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对象中。
发布于 2013-07-25 00:34:40
操作员的AppleScript one线性代码错误。其中的AppleScript文本应该是有效的:
NSAppleScript *scriptURL= [[NSAppleScript alloc] initWithSource:@"tell application \"Safari\" to if it is running then return URL of front document as string"];发布于 2013-07-25 01:59:19
正如dougscripts (+1)所指出的,但我想让它更清楚地说明为什么OP尝试的NSAppleScript中的单线性AppleScript语法不起作用。
老实说,我建议进行一次编辑,结果以3比2输掉了比赛
OP的NSAppleScript代码:
NSAppleScript *scriptURL= [[NSAppleScript alloc] initWithSource:@"tell application \"Safari\" if it is running to return URL of front document as string"];因为语法错误,所以不起作用。
正确的语法应该是:
NSAppleScript *scriptURL= [[NSAppleScript alloc] initWithSource:@"tell application \"Safari\" to if it is running then return URL of front document as string"];在下面以粗体显示的部分代码中有两处更改。
\"Safari\“如果运行的是,则返回到
https://stackoverflow.com/questions/7328696
复制相似问题