You could assign a shortcut to a script like this:
tell application "Google Chrome"
repeat with t in tabs of windows
tell t
if URL starts with "http://www.youtube.com" then
execute javascript "player = document.querySelectorAll('#watch7-player embed')[0]
if (player) {
player.getPlayerState() == 1 ? player.pauseVideo() : player.playVideo()
} else { // if youtube.com/html5 is enabled
document.querySelectorAll('.html5-player-chrome > button:first-child')[0].click()
}"
exit repeat
end if
end tell
end repeat
end tell
For Safari, replace execute javascript
with do JavaScript
.
Here is an AppleScript that should help you. Open AppleScript Editor and save this as a script. I have modified the source that I found here to support taking arguments on the command line.
Use it like this:
osascript new_window.scpt http://www.google.com http://www.stackoverflow.com
Of course, replace the URLs above with your own URLs.
new_window.scpt
on run argv
tell application "Safari"
if (count argv) = 0 then
-- If you dont want to open a new window for an empty list, replace the
-- following line with just "return"
set {first_url, rest_urls} to {"", {}}
else
-- `item 1 of ...` gets the first item of a list, `rest of ...` gets
-- everything after the first item of a list. We treat the two
-- differently because the first item must be placed in a new window, but
-- everything else must be placed in a new tab.
set {first_url, rest_urls} to {item 1 of argv, the rest of argv}
end if
make new document at end of documents with properties {URL:first_url}
tell window 1
repeat with the_url in rest_urls
make new tab at end of tabs with properties {URL:the_url}
end repeat
end tell
activate
end tell
end run
You could even create an alias for this in Terminal and be able to use it easier. I would add the following to ~/.bash_profile
:
alias newwindow='osascript /path/to/new_window.scpt'
Call newwindow
whatever you want. Save .bash_profile
and restart Terminal for it to work.
In case anyone is looking for a similar solution for Google Chrome, here is a different take on the same idea.
chrome_new_window.scpt
on run argv
tell application "Google Chrome"
if (count argv) = 0 then
make new window
else
tell (make new window)
set URL of active tab to item 1 of argv
repeat with the_url in the rest of argv
open location the_url
end repeat
end tell
end if
set active tab index of first window to 1
activate
end tell
end run
Best Answer
To get the URL or Title of a Tab within Google Chrome via the command line, in a Terminal you can use
osascript
to either execute AppleScript statements or a programfile.To see what is available in the Google Chrome AppleScript Dictionary, the dictionary can be opened and reviewed in the Script Editor using the File > Open Dictionary... command.
Here's a simple example to get the URL of the first Tab of the frontmost Google Chrome window:
For the Title under the same conditions:
To get them both at the same time, under the same conditions:
Depending on exactly what it is you're trying to accomplish the statements can be more complex and handle multiple Tabs and or Windows, etc. Although this is where using
osascript
to call a programfile verses using-e statement
will probably be easier.Here's the output from Terminal when this page is the target Tab: