Opening a new Safari window with Alfred AppleScript opens 2 windows - applescript

When I run this script from Alfred (using the text "nsafari", and Safari is quit (not in the dock), two Safari windows will pop up. When I run it from the Script Editor, it will sometimes open up two windows, but sometimes not. (This also happens with my new Safari window script as well).
if application "Safari" is running then
tell application "Safari"
make new document
activate
end tell
else
tell application "Safari" to activate
end if
Why does it open two windows only from Afred? And how do I make it only create one?

I don't know why, but after two years, the problem I reported above does not seem to happen any more...
(and yes this is not a very satisfying answer)

Related

Applescript open app in certain desktop without switching focus

I am trying to make a simple AppleScript to open Spotify on my computer. I have assigned Spotify to always open on Desktop 2. However, every time it opens, it switches my focus from whatever desktop I am currently using to Desktop 2 where Spotify is being opened. Is there any way for me to prevent this, that is, for the app to open without switching my focus to a different desktop?
Current code:
tell application "Spotify"
activate
delay 3
playpause
end tell
I have also tried to open Spotify using commands like open -g -a 'Spotify', but this just opens the app in the background and still switches desktops.
Thanks for the help.

AppleScript to Launch 1Password mini

I use 1Password to store my passwords, and fluid.app to create a few site-specific apps/browsers
While 1Password integrates well with Chrome, Safari and other browsers, it doesn't play nicely with Fluid apps, which is where my troubles lie.
I have therefore created a shortcut (with Keyboard Maestro) to run an applescript to open the mini, when I am in Fluid apps. However, I can't seem to get the applescript to launch the mini. Here are my attempts:
tell application "1Password mini" to launch does nothing, though AppleScriptEditor doesn't report any errors
tell application "1Password mini" to open nothing, though AppleScriptEditor doesn't report any errors
tell application "1Password mini" to activate is pointless, as 1Password mini is already activated
tell application "1Password" to open opens the main 1Password application, not the mini, which is what I need.
Interestingly, when I run (1), (2), and (3), AppleScriptEditor automatically replaces 1Password mini with 2BUA8C4S2C.com.agilebits.onepassword-osx-helper, which doesn't clarify things for me at all.
Does anybody have any clue how this can be solved?
I'm on OSX 10.10.2 (Yosemite), if it matters
I work for AgileBits, the developer of 1Password.
Looks like you found a solution, but here's another option for you:
open location "x-onepassword-helper://search/"
That AppleScript will open 1Password mini ready to search.
And if you're able to get the current website's domain from Fluid, you could even open 1Password Mini with a search term already filled in:
open location "x-onepassword-helper://search/twitter.com"
I hope that helps!
The solution was quite simple, with a little UI scripting:
tell application "System Events" to tell process "1Password mini"
tell menu bar item 1 of menu bar 1 to click
end tell

In applescript, is there any way to specify which desktop a file is opened in?

When my system boots I would like to run applescript that opens files in three different desktops/spaces.
First Space: Mail and Things (my to do list program)
Second Space: Textmate and a Safari for my first project
Third Space: Textmate and a Safari for my second project
First, in Mission Control I created two more desktops which will remain there the next time my system boots unless they are manually removed. Instead of creating one long script, I chained three applescripts (boot1, boot2 and boot3) to break it up into simpler blocks of code. At the end of boot1 you will see:
run script file "<drive name>:Users:<username>:boot2.scpt"
In boot2 and boot3 you will see a bunch of delay lines. One thing I dislike about applescript is that it often starts processing the next command before the OS finishes responding to the prior one. This causes inconsistencies and errors. Delays are a hack to force things to slow down. They help, but even when you use them things are still a bit dicey. In boot2.script:
# this emulates the keyboard shortcut to move to desktop 2
# there doesn't seem to be any way to modify an `open` command to open a file on desktop 2
tell application "System Events"
delay 2
# key code 19 is the key code for the number 2.
# <cntl> 2 is the shortcut to get to desktop 2
key code 19 using control down
end tell
tell application "TextMate"
activate
# 'sites' is the name of the directory my projects are in
open "/users/<username>/sites/project1/"
end tell
tell application "Terminal"
activate
do script "cd /users/<username>/sites/project1/"
delay 2
do script "rails s" in front window
delay 2
tell application "System Events" to tell process "Terminal" to keystroke "t" using command down
tell application "System Events" to tell process "Terminal" to keystroke return
delay 2
do shell script "open -a Safari http://localhost:3000"
end tell
OK... so this mostly works to get desktop 2 in place except for inconsistencies when the delays aren't long enough. Boot3.script is almost the same as boot2 but when trying to open an application on desktop 3, because there is a window on desktop 2 the system jumps back to that desktop. This is the next problem. How do I overcome that?
2305491 is no longer relevant because space preferences are gone.
Thanks.
Boot3.script is almost the same as boot2 but when trying to open an application on desktop 3, because there is a window on desktop 2 the system jumps back to that desktop.
There is an option in the Mission Control Preferences called "When switching to an application, switch to a Space with open windows for the application". Uncheck this.
OK... so this mostly works to get desktop 2 in place except for inconsistencies when the delays aren't long enough.
Better solution is always something like this
repeat until something exists
delay 0.1
end repeat

Why does the safari app gets launched when I open apple script?

I have an applescript. The script goes like this..
tell application "Safari"
open location "http://www.google.com" -- mentions the perticular webpage to be loaded
activate --makes the Safari application the front most application
end tell
Whenever I open this script, the Safari application gets launched. To be very clear, I'm not running this application instead I am just opening this script in applescript editor.
Can anyone please explain me why the Safari is getting launched. The Safari gets launched but it will be hidden or say it does not put up any window. [Neither it does load any pages in the background, it just gets launched in the dock and a dot saying that the app is launched will be present].
Once I run the script then the safari puts up the window and loads the desired web-page.
Please Help.
In general, applescript must launch an application to learn what commands it understands. Over time applescript has gotten better at not launching applications for this task, but some apps still get launched. iPhoto is one I notice gets launched when I open applescripts that use it. As such it's not something you can avoid.
However, in your case there may be a fix. The "open location" command is a generic applescript command. You do not need to tell Safari to execute that command. Therefore you can probably change your script to this and avoid the launching issue. The open location command should just open the link in your default browser which I assume in your case is Safari. It should also automatically activate it for you.
Good luck.
open location "http://www.google.com"

Google Chrome doesn't open URL with AppleScript

What I have set up is a AppleScript that asks you what website you want to open with a couple options, to make this simple, I only list one option, Google. This is the current code:
if the_button = "Google" then
site = "http://www.google.com"
tell application "Google Chrome"
launch
tell window 1
make new tab with properties {URL:site}
repeat while loading of active tab
delay 0.1
end repeat
end tell
activate
end tell
It opens a new tab in Google Chrome, but doesn't put anything in the URL bar or reload/load or anything. I've tried this: Script Safari 5.1 to open a tab But, nothing's working. I'm using Mac OS X Lion 10.7.4 and Google Chrome version 21.0.1180.79. Please help!
It works for me, but not when Chrome doesn't have open windows or if all windows are minimized. You could add a reopen command to open a new default window or unminimize a window in those cases.
set site to "http://www.google.com"
tell application "Google Chrome"
reopen
activate
tell window 1
make new tab with properties {URL:site}
end tell
end tell
Another option might be to use something like do shell script "open " & quoted form of "http://google.com" & " -a Google\ Chrome". It's often closer to the default behavior of browsers.
Can't you just do this?
tell application "Google Chrome"
activate
set theSite to "http://www.google.com/"
open location theSite
end tell
The “open location” command is part of Standard Additions and is available to all Mac apps. It’s the default way to open a website in any Mac app. You might notice that Safari’s AppleScript dictionary doesn’t even include a way to open a website — it simply uses the standard “open location” command which you can find in the Standard Additions dictionary.

Resources