Open URL in new Safari tab with AppleScript - macos

Is it possible to use AppleScript to open a link in a new tab in Safari?

This will work:
tell application "Safari"
tell window 1
set current tab to (make new tab with properties {URL:"http://www.stackoverflow.com"})
end tell
end tell

I think this also does what you asked for, but it is much shorter and is less browser-specific:
do shell script "open http://www.webpagehere.com"
This will open the specified URL in your default browser. And if you explicitly want to open it in Safari, use this:
do shell script "open -a Safari 'http://www.webpagehere.com'"

This should usually create a new tab and focus it (or focus an existing tab if the URL is already open):
tell application "Safari"
open location "http://stackoverflow.com"
activate
end tell
It opens a new window if "Open pages in tabs instead of windows" is set to never though.
tell application "System Events" to open location doesn't work with some URLs that contain non-ASCII characters:
set u to "http://ja.wikipedia.org/wiki/漢字"
tell application "System Events" to open location u
--tell application "Safari" to open location u
--do shell script "open " & quoted form of u
This opens a new tab even when new pages are set to open in windows:
tell application "Safari"
activate
reopen
tell (window 1 where (its document is not missing value))
if name of its document is not "Untitled" then set current tab to (make new tab)
set index to 1
end tell
set URL of document 1 to "http://stackoverflow.com"
end tell
tell application "System Events" to tell process "Safari"
perform action "AXRaise" of window 1
end tell
set index to 1 doesn't raise the window, but it makes the window appear as window 1 to System Events, which can AXRaise it.

I've been using the following script to open hundreds of docs into tabs in a single window.
tell application "Safari"
tell window 1
make new tab with properties {URL:"http://foo.com/bar"}
make new tab with properties {URL:"http://foo.com/baz"}
end tell
end tell
But that no longer works in Safari 5.1 on Lion. It would open the new tab, but it wouldn't load the URL provided in the properties glob. I modified it to the following, which now works:
tell application "Safari"
tell window 1
set URL of (make new tab) to "http://foo.com/bar"
set make new tab to "http://foo.com/baz"
end tell
end tell

Code:
tell application "System Events"
tell application "Safari" to activate
tell process "Safari"
click menu item "New Tab" of menu "File" of menu bar 1
end tell
end tell
tell application "Safari"
set URL of document 1 to "http://www.stackoverflow.com/"
end tell
One problem is that this only works if the system's language is set to English.

It's been a while since a new answer's been posted here. I think this is the optimal way to do this. It will open Safari if it's not open, create a new window if there are no windows open, and add the tab to the current (or newly created) window.
tell application "Safari"
activate
try
tell window 1 to set current tab to make new tab with properties {URL:theURL}
on error
open location theURL
end try
end tell

I can't comment :-/ so I will answer to say that Tim's answer (above) works as of OS X 10.8.5. This one-line version of his script also works:
tell window 1 of application "Safari" to set current tab to (make new tab with properties {URL:"http://www.stackoverflow.com"})
Arrgh -- the one line is overflowing. Here it is without the code tags:
tell window 1 of application "Safari" to set current tab to (make new tab with properties {URL:"http://www.stackoverflow.com"})

I ended up using automator to do this which was much easier and it works.

You can try following approach:
//make Safari window active and topmost
tell application "Safari" to activate
//start communication with Safari
tell application "Safari"
tell window 1
//create new tab and open specified URL
tab with properties {URL:"https://url.com"})
//make tab active
set visible to true
end tell
end tell
Also u can combine usage of Apple script within FastScript (free for 10 shortcut)
To add your script - just save script in /Library/Scripts. After you will be able to set some shortcut for new script.
If you want to open new Window than new tab u can play within next:
tell application "System Events"
tell process "Safari"
click menu item "New window" of menu "File" of menu bar 1
end tell
end tell
Note: you need to allow AppleScript to use specialCapabilities in security settings in this case.

Not the shortest solution but also works, and not only in English ...
tell application "Safari"
activate
end tell
tell application "System Events"
set frontmost of process "Safari" to true
keystroke "t" using {command down}
end tell
set myURL to "anyurl.html"
delay 2
tell application "Safari" to set the URL of the front document to myURL

Worked for me in Safari v.11
tell application "Safari"
tell window 1
make new tab with properties {URL:"https://twitter.com"}
end tell
end tell

I found a way to open a new tab in the background with Safari.
tell application "Safari"
set the URL of (make new tab in window 1) to "your.url.net"
end tell
During the time I wrote this answer I made this
tell application "Safari"
try
display dialog "Website URL" default answer "" buttons {"OK", "Annuler"} default button 1
set theURL to text returned of result
set netProto to "https://"
if theURL contains netProto then
set the URL of (make new tab in window 1) to theURL
else
set the URL of (make new tab in window 1) to netProto & theURL
end if
end try
end tell
New version
tell application "Safari"
repeat
try
display dialog "Website URL" default answer "" buttons {"OK", "Annuler"} default button 1
set theURL to text returned of result
if theURL is "" then exit repeat
set netProto to "https://"
if theURL contains netProto then
set the URL of (make new tab in window 1) to theURL
else
set the URL of (make new tab in window 1) to netProto & theURL
end if
display dialog "Do you want to open a new tab?" buttons {"Yes", "No"} default button "Yes"
if button returned of result is "No" then exit repeat
end try
end repeat
end tell
Any suggestions will be appreciate
Best regards

Related

Applescript to open Chrome bookmark folder & make tab frontmost

Absolute newbie to Applescript here:-
I'm trying to figure out a script which will open multiple URL's in a sub-folder in my bookmarks bar . . . and then make the first URL in the sub-folder, the frontmost window.
At present, the script I'm using makes the "last" tab the frontmost window.
Here's the script:
tell application "System Events" to (name of processes) contains "Google Chrome"
set chromeRunning to result
tell application "Google Chrome"
activate
if chromeRunning then
make new window
set myBMFolder to bookmark folder "sub-folder" of bookmark folder "parentfolder" of bookmark folder "Bookmarks Bar"
set bmURLs to URL of bookmark items of myBMFolder
repeat with aUrl in bmURLs
open location aUrl
end repeat
end if
end tell
Any help greatly appreciated.

How to open a tab 2 on Google Chrome or Microsoft Edge if it doesn't exist?

I am new to AppleScript and tried an AppleScript solution to open up a tab and load a webpage:
tell application "Google Chrome" -- or "Microsoft Edge"
tell window 1
tell tab 2
set URL to "https://google.com/"
end tell
end tell
end tell
But if tab 2 doesn't exist, it won't do anything.
I then tried to solve it by checking whether an URL exist in any tab:
tell application "Google Chrome"
repeat with w in windows
set i to 1
repeat with t in tabs of w
if URL of t starts with "https://mail.google" then
set active tab index of w to i
set index of w to 1
return
end if
set i to i + 1
end repeat
end repeat
open location "https://mail.google.com/mail/u/0/#inbox"
end tell
But it is sort of an overkill, and it opens up a new tab, rather than opening up a tab 2.
I then tried
tell application "Google Chrome" -- or "Microsoft Edge"
tell window 1
open tab 2 -- added this line
tell tab 2
set URL to "https://google.com/"
end tell
end tell
end tell
but no matter it is open tab 2 or activate tab 2 or start tab 2, it wouldn't work. How can we make it work and, in general, how to find out the vocabularies or verbs that we can use?
open tab 2 is the problem. If a browser window exists and you want to create a new tab and set its URL, then:
tell application "Google Chrome" to ¬
if exists front window then ¬
make new tab at end of ¬
tabs of front window ¬
with properties ¬
{URL:"https://www.example.com"}
If a browser window exists and you want to change the URL of tab 2, then:
tell application "Google Chrome" to ¬
if exists front window then ¬
tell front window to ¬
if (exists tab 2) then ¬
tell its tab 2 to ¬
set its URL to ¬
"https://www.example.com"
Notes:
The example AppleScript code works with Microsoft Edge too.
These examples can be expanded into normal block format using if statements to determine if Google Chrome and or Microsoft Edge exists, or are already running, and if running whether or not a window exists, etc. You just need to code it according to your needs/wants.
Have a look at AppleScript Language Guide
In Script Editor have a look at the Library for the AppleScript dictionary of any application you want to write code for. From the Window menu... Library     ⇧⌘L
The example AppleScript code below shows an example of coding for the existence of either Google Chrome or Microsoft Edge and whether or not they are running when you can then write code based on the conditions. As written it favors Google Chrome but if it does exist then Microsoft Edge runs its code. To favor Microsoft Edge change the primary if statement structure to accommodate.
tell application "System Events"
set existsGoogleChrome to exists file "/Applications/Google Chrome.app"
set existsMicrosoftEdge to exists file "/Applications/Microsoft Edge.app"
end tell
if existsGoogleChrome then
if running of application "Google Chrome" then
tell application "Google Chrome"
# Do Something
end tell
else
tell application "Google Chrome"
# Do Something Else
end tell
end if
else
if existsMicrosoftEdge then
if running of application "Microsoft Edge" then
tell application "Microsoft Edge"
# Do Something
end tell
else
tell application "Microsoft Edge"
# Do Something Else
end tell
end if
end if
end if

Safari new tab next to current one + tab-home

I've used the applescript from these link:
https://daringfireball.net/2018/12/safari_new_tab_next_to_current_tab
These applescript was used to open new tab in safari next to current active one, the problem is that the new tab opened is just a blank one, not the usual one with tab-home with bookmarks.
Any way to modify this?
tell application "Safari"
tell front window
set _old_tab to current tab
set _new_tab to make new tab at after _old_tab
set current tab to _new_tab
end tell
end tell
tell application "Safari"
tell front window
set _old_tab to current tab
set _new_tab to make new tab at after _old_tab
set current tab to _new_tab
set URL of current tab to {"favorites://"}
end tell
end tell

AppleScript Clicking On dialog box

In PCSX, (ps1 emulator), i'm trying to automate the steps to play an iso. So, i'm doing this:
set thepath to path to me
set thesecondpath to POSIX path of thepath
set thethirdpath to "Contents/PSX/ROMS/img.bin"
set thefourthpath to "/Contents/PSX/PCSX.app"
set thefifthpath to thesecondpath & thefourthpath
set theultimatepath to thesecondpath & thethirdpath
tell application thefifthpath
activate
tell application "System Events"
keystroke "i" using {command down}
keystroke theultimatepath
delay 1.0
tell process "PCSX"
click button "Go"
end tell
key code 53
end tell
end tell
Running from the AppleScript Editor won't work. I made it to work running from the App it creates. PCSX and the img.bin are inside the Generated Package.
after pressing command+i, it opens a "Go to the folder" dialog, and i need to click Go and then Open
But doing this way, it won't find the dialog box. What am i doing wrong?
If Go and Open are the default buttons, try:
tell application "System Events"
keystroke return
delay 2
keystroke return
end tell
Although I don't have PCX installed here is an example of how to click the Go button from Finder's Go to Folder command.
tell application "System Events"
tell process "Finder"
click button "Go" of window "Go to Folder"
end tell
end tell
The reason your script won’t work from AppleScript Editor is that the “me” in “path to me” is the application that ran the AppleScript. When you are running the AppleScript in AppleScript Editor, that means AppleScript Editor itself. When you saved your AppleScript as a script application and ran it, the path to me pointed to your script application, because it was running its own AppleScript.
Also, this is incorrect:
tell process "Finder"
click button "Go" of window "Go to Folder"
end tell
The “Go” button is not on the window “Go to Folder.” It’s on a sheet which is attached to a Finder window which has the name of whatever folder is currently being viewed. So you have to describe the button as being on sheet 1 of window 1:
tell application "System Events"
tell process "Finder"
click button "Go" of sheet 1 of window 1
end tell
end tell
… but keep in mind that in another app, a similar looking button on a sheet may be on sheet 1 of group 1 of group 2 of window 3. UI Scripting is complicated.

Script for adding new KeyboardShortcuts on MacOs (Leopard)

Is it possible to add KeyboardShortcuts in MacOs (Leopard) using shell or other programmatic way? Basically, something to automate the steps of opening Keyboard&Mouse in SystemPreferences, selecting the last tab "KeyboardShortcuts", Clicking "+" to add a new one and filling the info.
Thank you
The following AppleScript should do the trick, with 3 variables:
app_name: name of an application that you want to assign the shortcut to, e.g. Safari
menu_title: exact menu name to execute
keystrokes: the actual shortcut
tell application "System Preferences"
activate
set current pane to pane "com.apple.preference.keyboard"
end tell
tell application "System Events"
tell process "System Preferences"
tell window "Keyboard"
click button 3 of tab group 1
tell sheet 1
click pop up button 1
click last menu item of menu 1 of pop up button 1
keystroke "/Applications/" & app_name & ".app"
keystroke return
keystroke return
delay 1
keystroke menu_title
keystroke tab
keystroke last item of keystokes using rest of reverse of keystokes
delay 1
click button "Add"
end tell
end tell
end tell
end tell
The code is referencing the following site:
http://www.rngtng.com/2010/10/29/applescript-to-create-keyboard-shortcuts/

Resources