简体   繁体   中英

Applescript open url from commandline in new window

there's some strange behavior in Applescript. I got this script

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

As you can see from the code, it should always open a new window. If Safari is already running it does not need to make a new window. It'll use the automatically opened window and just change the location.

When I run this with scripteditor everything works as expected. But when i call it from bash with:

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

it always acts like Safari was running. So if Safari isn't running it pops up two windows. One with the homepage and one with the location from cli.

What's different and how do you fix this?

If Safari is already running it does not need to make a new window.

Your code seems to be backwards. Under "if running of application "Safari" then you are telling it to make a new document when that's the case for using document 1. Just rearrange your code, it's backwards.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM