0

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?

1
  • I tried in applescript editor and from terminal and got the same behavior in both cases: if Safari is running, a new window is opened; if it's not running the url is opened in the original window. Commented Dec 10, 2012 at 18:06

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Ha ... I was looking for some shell problem. You are right! +1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.