Skip to content

Add copy url #4

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 24 additions & 4 deletions chrome.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
#!/usr/bin/env osascript -l JavaScript

/**
* A JXA script and an Alfred Workflow for controlling Google Chrome (Javascript for Automation).
* A JXA script and an Alfred Workflow for controlling Google Chrome (Javascript for Automation).
* Also see my "How I Navigate Hundreds of Tabs on Chrome with JXA and Alfred" article at [1]
* if you're interested in learning how I created the workflow.
* [1] https://medium.com/@bit2pixel/how-i-navigate-hundreds-of-tabs-on-chrome-with-jxa-and-alfred-9bbf971af02b
* [1] https://medium.com/@bit2pixel/how-i-navigate-hundreds-of-tabs-on-chrome-with-jxa-and-alfred-9bbf971af02b
*/

ObjC.import('stdlib')
Expand Down Expand Up @@ -48,7 +48,7 @@ function run(argv) {
function chromeControl(argv) {
if (argv.length < 1) { usage() }

// --ui flag will cause the questions to be asked using a
// --ui flag will cause the questions to be asked using a
// Chrome dialog instead of text in command line.
let uiFlagIdx = argv.indexOf('--ui')
if (uiFlagIdx > -1) {
Expand Down Expand Up @@ -83,6 +83,10 @@ function chromeControl(argv) {
if (argv.length !== 2) { usage() }
const arg = argv[1]
focus(arg)
} else if (cmd === 'copy') {
if (argv.length !== 2) { usage() }
const arg = argv[1]
copy(arg)
} else {
usage()
}
Expand Down Expand Up @@ -182,6 +186,22 @@ function focus(arg) {
chrome.activate()
}

// Copy a specific tab
function copy(arg) {
let { winIdx, tabIdx } = parseWinTabIdx(arg)
let urlToTitle = {}
let selectedTab = tabIdx;

chrome.windows().forEach((window, winIdx) => {
window.tabs().forEach((tab, tabIdx) => {
if (tabIdx === selectedTab) {
urlToTitle = tab.url();
}
})
})
println(urlToTitle)
}

// Close duplicate tabs
function dedup() {
let urls = {}
Expand Down Expand Up @@ -295,4 +315,4 @@ function parseWinTabIdx(arg) {
}

return { winIdx, tabIdx }
}
}
Binary file modified integrations/Alfred/Chrome Control.alfredworkflow
Binary file not shown.