Safari new tab next to current one + tab-home - macos

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

Related

Why does Script editor not like this edit?

I got his script:
set vURL to URL of current tab of window 1
end tell
tell application "Google Chrome"
if windows ≠ {} then
make new tab at the end of window 1 with properties {URL:vURL}
else
make new window
set URL of (active tab of window 1) to vURL
end if
activate
end tell
I'd really like to be able to just quickly open the tab I'm using in Safari in Firefox beause...
I'm told have to add more text here so, Skip this paragraph. ... a site I use a lot has some great features in Firefox but it can be slow to load so I like to switch backwards and forwards between the two.
Why won't it work if I change "Google Chrome" to "Firefox":
tell application "Safari"
set vURL to URL of current tab of window 1
end tell
tell application "Firefox"
if windows ≠ {} then
make new tab at the end of window 1 with properties {URL:vURL}
else
make new window
set URL of (active tab of window 1) to vURL
end if
activate
end tell
Thanks
You can't mix and match scripting terms - the scripting terminology (if any) for a given application is entirely up to the developer and is unique to that application. There also isn't a standard or common practice for term names, so any similarity between scripting terms of different applications would be purely coincidental and wouldn't necessarily provide the same functionality.
Looking at the scripting dictionaries for Google Chrome vs Safari and Firefox:
Firefox doesn't really have a scripting dictionary, only a default suite is available. Specifically, a window does not have either
tab, active tab, or URL elements (URL here would also be
assumed to be from StandardAdditions)
Safari windows do not have an active tab property.
You will need to target each application with its own tell statement, using the terminology specific to that application. Also note that trying to do something like using a variable to hold the application name will not work, since the scripting terminology for each application is looked up when the script is compiled.
(Edit from comments)
To open the URL of the current Safari tab with Firefox (version 69.0.1 in Mojave), you can do something like:
tell application "Safari" to set theURL to (get URL of current tab of front window)
tell application "Firefox"
activate
open location theURL
end tell

Error getting the number of tabs in "Terminal" window via Applescript on OSX El Capitan

Basically, I want to change the theme of bash when I open new windows, not new tabs, and then have tabs of a window share the same theme; while themes of separate windows are determined randomly.
After some digging, I found out an applescript which sets the theme of the current tab of Terminal window. I created a
/usr/local/terminal-color.scpt as:
on run argv
tell application "Terminal" to set current settings of selected tab of front window to some settings set
end run
And I have added the following statement to my bash profile:
osascript /usr/local/terminal-color.scpt
Now this script, of course, runs with every new instance of the bash. I cannot do anything about that from bash_profile. However, I should be able to differentiate a new window or a new tab from the applescript itself. Therefore, I am looking for an if statement, which would let the script run only when new windows are created. Something like:
on run argv
if index of selected tab is 0
tell application "Terminal" ....
end if
end run
But I cannot figure out how to achieve this looking at the applescript documentation and scripting dictionary of the terminal application. Help please
Update
I try editing the script as follows:
set tabNum to number of tabs of front window
if tabNum = 1 then
tell app ...
this won't work either giving an error tabs of window 1 doesn’t understand the “count” message
My approach was correct but I had a simple mistake of trying to get tab or window data before choosing an application scope. To put in simple words, first tell the application, then ask about its properties. Here's the code that worked:
on run argv
tell application "Terminal"
if number of tabs of front window = 1 then
set current settings of selected tab of front window to some settings set
end if
end tell
end run
Even Better
Improving the previous script; this one not randomly chooses a theme, it iterates through your available terminal themes according to the windows you have already open. You can also set your default theme that will be set on your first launch of the terminal. In my case, it was the 5th one in the settings set. Here goes the code:
tell application "Terminal"
if number of tabs in front window = 1 then
set defaultThemeOffset to 5
set allThemes to number of settings set
set allWindows to number of windows
set themeIndex to (defaultThemeOffset + allWindows) mod allThemes
set current settings of selected tab of front window to settings set themeIndex
end if
end tell

Applescript, Chrome: Make new tab at specified index

I have this simple Applescript, which reopens active tab in new tab
tell application "Google Chrome"
tell first window
set theUrl to URL of active tab
set myTab to make new tab at end of tabs
set URL of myTab to theUrl
end tell
end tell
So I can open tab at end of tabs or at beginning of tabs, but how can I open new tab at specified index of tabs. I'd like to open it as next tab after active tab.
You can use the term at after tab (number); like this
tell application "Google Chrome"
tell first window
set n to active tab index
set theUrl to URL of active tab
set myTab to make new tab at after tab n with properties {URL:theUrl}
end tell
end tell

Applescript to open specific Terminal Style Windows

Ok, so the thing is. I want to have different style terminal windows for each job. Each one will have a different job, i.e. , one will connect through ssh to a site, other window to other place, etc.
So I guess this could be done with some Applescripting?
The thing would be to have some applescripts that opens a different terminal window. And then add each applescript to a shortcut.
Any ideas?
Thanks :)
How about setting up a Window group in terminal ?
Open all Terminal windows you want --> Shell --> Show Inspector. Under Settings you can change the theme of each terminal window.
Window --> Save Windows as Group
In preferences set the startup option to display the group.
http://img18.imageshack.us/img18/9681/screenshot20111018at110.png
http://img542.imageshack.us/img542/9681/screenshot20111018at110.png
If you want to use Applescript to set a window's theme you first need to get the id's of all the themes you have using this applescript :
set a to {}
tell application "Terminal"
repeat with i from 1 to count settings set
set temp to {settings set i's name, settings set i's id}
set end of a to temp
end repeat
a
end tell
This will output an array of id # and the theme's name. Next to create a new window use the following :
tell application "Terminal"
set a to do script "" -- creates new window
set a's current settings to (settings set id <one of the id #>)
end tell
tell application "System Events" to tell process "Terminal" to click menu bar 1's menu bar item "Shell"'s menu 1's menu item "New Window"'s menu 1's menu item "Grass"
tell application "Terminal"
set win to do script
set win's current settings to settings set "Basic"
end tell

Open URL in new Safari tab with AppleScript

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

Resources