Skip to content

Instantly share code, notes, and snippets.

@8bitDesigner
Last active February 26, 2024 07:11
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 8bitDesigner/8895649 to your computer and use it in GitHub Desktop.
Save 8bitDesigner/8895649 to your computer and use it in GitHub Desktop.
My magnum opus: a script to open and close tabs in Chrome
# The name of the folder that'll contain or bookmarks
set myFolderTitle to "Work Bookmarks"
# And here we take every bookmark in that folder, open it in
# a new tab, and delete it
tell application "Google Chrome"
set myFolder to first item of ¬
(every bookmark folder in every bookmark folder whose title is myFolderTitle)
# Currently Chrome crashes when adding a bookmark to an empty folder,
# so I'm leaving a stub bookmark in there which I won't be opening
set myBookmarks to myFolder's bookmark items ¬
whose URL is not "http://google.com/"
repeat with mark in myBookmarks
set {URL:myUrl} to mark
make new tab with properties {URL:myUrl} at end of first window's tabs
delete (every bookmark item in myFolder whose URL is myUrl)
end repeat
end tell
# Here we define a list of URL fragements. We're going to
# bookmark and close any tabs that contain these fragments
set urls to {¬
"github.com/fullscreeninc", ¬
"fullscreen.dev", ¬
"fullscreen.tpondemand.com"}
# The name of the folder that'll contain or bookmarks
set myFolderTitle to "Work Bookmarks"
tell application "Google Chrome"
set myTabs to {}
set otherBookmarks to first item of ¬
(every bookmark folder whose title is "Other Bookmarks")
set myFolders to ¬
(every bookmark folder in otherBookmarks whose title is myFolderTitle)
# Iterate over every tab in Chrome, if it matches a URL in our
# list of URLs, add the tab to our `myTabs` list
repeat with myTab in every tab of every window
repeat with myUrl in urls
if myTab's URL contains myUrl then
set end of myTabs to myTab
end if
end repeat
end repeat
# If we already have a "Work Bookmarks" folder, grab a ref to it
if (count myFolders) > 0 then
set myFolder to first item of myFolders
# Otherwise, create it now
else
set myFolder to make new bookmark folder ¬
with properties {title:myFolderTitle} ¬
at end of otherBookmarks
end if
# Create a new bookmark for each tab, and then close it
repeat with myTab in myTabs
set {title:myTitle, URL:myUrl} to myTab
make new bookmark item ¬
with properties {title:myTitle, URL:myUrl} ¬
at beginning of myFolder's bookmark items
close myTab
end repeat
end tell
@8bitDesigner
Copy link
Author

And the reason for hacking this together? I wanted a "Go Home" Automator action, and corresponding "Go to Work" action:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment