Applescript在新窗口中从命令行打开url

时间:2012-12-10 16:33:21

标签: macos bash applescript

Applescript中有一些奇怪的行为。我有这个脚本

property tmpUrl:"http://www.example.com"
on run argv
  if(count argv) > 0 then
    set tmpUrl to item 1 of argv
  end if
  if running of application "Safari" then
    tell application "Safari"
      activate
      make new document with properties{URL:tmpUrl}
    end tell
  else
    tell application "Safari"
      activate
      set URL of document 1 to tmpUrl
    end tell
  end if
end run

从代码中可以看出,它应该始终打开一个新窗口。如果Safari已经运行,则无需创建新窗口。它将使用自动打开的窗口,只需更改位置。

当我使用scripteditor运行时,一切都按预期工作。但是当我从bash中调用它时:

osascript web_win_open.applescript "http://www.stackoverflow.com"

它总是像Safari一样运行。因此,如果Safari没有运行,它会弹出两个窗口。一个是主页,另一个是cli的位置。

有什么不同,你如何解决这个问题?

1 个答案:

答案 0 :(得分:1)

  

如果Safari已在运行,则无需打开新窗口。

您的代码似乎是倒退的。在“如果运行应用程序”Safari“然后你告诉它在使用文档1的情况下创建一个新文档。只需重新排列代码,它就会倒退。

相关问题