I am trying to get active browser tab from browsers. I can do that for Chrome with this code:
tell application "Google Chrome"
set myURL to the URL of the active tab of the front window
end tell
For Safari, I am changing Chrome to Safari but it is not working at all
Is there anyone to help me about that?
This code finds Gmail tab among all windows and all tabs and makes focus to it.
set tabTitle to "Gmail"
tell application "Google Chrome"
activate
repeat with w in (windows)
set tabIndex to 0
repeat with t in (tabs of w)
set tabIndex to tabIndex + 1
if title of t contains tabTitle then
set (active tab index of w) to tabIndex
set index of w to 1
return
end if
end repeat
end repeat
end tell
For Safari it's:
tell application "Safari"
set myURL to the URL of the current tab of the front window
end tell
You also do not need to be so verbose, dropping all occurrences of the, e.g.:
set myURL to URL of current tab of front window
Does the same thing.
In Script Editor, look in the Library for Safari:
Script Editor > Window > Library > Safari
This is the answer i tried.. #user3439894
Related
New to Applescript.
Trying to make a script that will cycle through my tabs, printing each tab to PDF.
Starting from here...
Example of working Google Chrome Applescript
...but trying to generalize to work with all of Google Chrome, Firefox, and Safari. To start this modification, I began by looking here and here.
I'm getting tripped up right off the bat, noticing that a small change can make a big difference ...
NO ERROR:
tell application "Google Chrome"
set myWindow to front window
set myTabs to tabs in myWindow
--Do more stuff here...
end tell
ERROR:
set webBrowser to "Google Chrome"
tell application webBrowser
set myWindow to front window
set myTabs to tabs in myWindow
--Do more stuff here...
end tell
The error I'm getting is:
"Google Chrome got an error: Can’t make |tabs| of window id 1 into type specifier." The "offending object" in this error is "tabs of window id 1", and it's telling me that the expected type is "reference".
Any ideas how to proceed? Thanks for any help.
Justin
EDIT:
set webBrowser to "Firefox" -- or Safari
set pdfMenuItemTitle to "Save as PDF…" -- the title of the menu item you want
set myDelay to 0.2
tell application webBrowser to activate
tell application "System Events"
tell process webBrowser
-- open the print dialog
click menu item "Print…" of menu 1 of menu bar item "File" of menu bar 1
delay myDelay
--Do more stuff here...
end tell
end tell
No, the point is not at all that you cannot use a variable in the tell block of the application. It can be done, only it is not efficient from a programming point of view. Because then you need to tell AppleScript where to get from the terms like tabs used in the tell block.
Purely for demonstration purposes:
set webBrowser to (choose from list {"Google Chrome", "Safari", "FireFox"})
if webBrowser is false then return
set webBrowser to item 1 of webBrowser
tell application webBrowser
if webBrowser is "Google Chrome" then
using terms from application "Google Chrome"
set myTabs to tabs in front window
end using terms from
else if webBrowser is "Safari" then
using terms from application "Safari"
set myTabs to tabs in front window
end using terms from
else -- it is Firefox. It has only windows, not the tabs.
set myTabs to front window
end if
end tell
In practice, it is better not to be perverted, but to do it like this:
set webBrowser to (choose from list {"Google Chrome", "Safari", "FireFox"})
if webBrowser is false then return
set webBrowser to item 1 of webBrowser
if webBrowser is "Google Chrome" then
tell application "Google Chrome" to set myTabs to tabs in front window
else if webBrowser is "Safari" then
tell application "Safari" to set myTabs to tabs in front window
else -- it is Firefox. It has only windows, not the tabs.
tell application "Firefox" to set myTabs to front window
end if
I'm trying to reload all tabs of Chrome (i could have many windows open) of a certain page. This is what I did:
tell application "Google Chrome"
set {ids} to every window
repeat with id in ids
reload (tabs of window id whose URL contains "mypage.com")
end repeat
end tell
But I get Google Chrome got an error: Can’t make item 1 of window id 1 into type integer.
How do I do this?
The answer submitted by #wch1zpink won't work if there are any Chrome windows open that don't contain at least one tab with a mypage.com URL. Unfortunately, you need to iterate through the windows:
tell application "Google Chrome" to repeat with W in windows
reload (every tab in W whose URL contains "mypage.com")
end repeat
This works for me using the latest version of macOS Mojave and Chrome.
tell application "Google Chrome" to set allTabs to a reference to (tabs of windows whose URL contains "mypage.com")
tell application "Google Chrome" to reload allTabs
I have an AppleScript that will reload my safari browser every x amount of seconds:
activate application "Safari"
repeat
tell application "System Events" to keystroke "r" using command down
delay (5)
end repeat
As I use the "Split Screen" feature within macOS, I want to expand this code to refresh both Safari browsers.
Firstly, a better way to reload the current Safari window:
tell application "Safari" to tell the front document to set its URL to its URL
To do this for multiple open windows:
tell application "Safari" to repeat with D in every document
set D's URL to D's URL
end repeat
Stick that in a repeat loop, add the necessary delay, and you'll have continuously-refreshing browser windows:
tell application "Safari" to repeat while document 2 exists
repeat with D in every document
set D's URL to D's URL
end repeat
delay 5
end repeat
I am trying to write an applescript to break the active Finder tab into a new window. I am able to do this with Chrome using:
on moveActiveTabToNewWindow()
tell application "Google Chrome"
set theURL to URL of active tab of window 1
close active tab of window 1
make new window
set URL of active tab of window 1 to theURL
end tell
end moveActiveTabToNewWindow
However, as far as I can tell Finder tabs are not accessible via Applescript. Is this possible? I'm on OS X Mavericks 10.9.2.
Manipulating tabs is not accessible directly but you could do it manually. In other words we can close that tab and then open a new window and replicate what you saw in that tab, just like you did in your Google Chrome example.
Here's some code. This code is basic. Just like I got the "target" property from the Finder window, you could get other properties too and replicate them in the new window to really do a more complete job of replicating the tab. You'll probably want to duplicate the bounds and view options at least.
Good luck.
-- get the target of the front tab
tell application "Finder"
activate
tell window 1 to set t to target
end tell
-- close that tab
tell application "System Events"
keystroke "w" using command down
end tell
-- make a new window and set its target
tell application "Finder"
set w to make new Finder window at front
tell w to set target to t
end tell
How can I tell Chrome or Firefox to reload the document in the top window? Here's what I'm using for Safari:
osascript -e '
tell application "Safari"
activate
do JavaScript "history.go(0)" in document 1
end tell
'
Here's the code for Chrome:
tell application "Google Chrome"
tell the active tab of its first window
reload
end tell
end tell
Or more concisely:
tell application "Google Chrome" to tell the active tab of its first window
reload
end tell
I do not think Firefox or Chrome have special Applescript support, but you can send the keystrokes (Cmd-R) to refresh the page:
tell application "Firefox"
activate
tell application "System Events" to keystroke "r" using command down
end tell
Here's another way to do it in Safari without using JavaScript:
tell application "Safari"
tell its first document
set its URL to (get its URL)
end tell
end tell
Automator
Open Automator and choose a New Document
Choose Service
Set Service receives to no input
Choose Run AppleScript action from the action list.
Paste the following code in the script:
tell application "Google Chrome" to tell the active tab of its first window
reload
end tell
Save the the service, for example, using the name Chrome Refresh
System Preferences
Open System Preferences > Keyboard
In the Shortcuts tab, choose Services
Assign a new shortcut
The following answers above work well but using them result in DevTools refreshing in a new tab if it was the last tab/window in focus. I don't want DevTools to refresh in a new tab, I just want the first tab to refresh regardless of last focus/active and this worked well for me. Leaving for someone searching for this use case as well.
tell application "Google Chrome"
activate
tell application "System Events"
tell process "Google Chrome"
keystroke "r" using {command down, shift down}
end tell
end tell
end tell