Skip to content

Instantly share code, notes, and snippets.

@mikker
Last active August 31, 2019 09:00
Show Gist options
  • Save mikker/eb21cb9940d7d0d1c06c to your computer and use it in GitHub Desktop.
Save mikker/eb21cb9940d7d0d1c06c to your computer and use it in GitHub Desktop.
Open all links in Safari's Reading List in tabs.
#!/usr/bin/env ruby
# $ gem install CFPropertyList
require 'cfpropertylist'
path = File.expand_path '~/Library/Safari/Bookmarks.plist'
plist = CFPropertyList::List.new file: path
list = plist.value.value["Children"].value.select do |item|
if title = item.value["Title"]
title.value == 'com.apple.ReadingList'
end
end.first.value["Children"].value
bookmarks = list.map do |item|
item.value["URLString"].value
end.reverse
puts "Opening #{bookmarks.count} tabs "
bookmarks.each do |url|
`osascript -e 'tell application "Safari" to tell window 1 to make new tab with properties {URL:"#{url}"}'`
print '.'
end
puts ''
@sanssecours
Copy link

Thank you for providing this very useful script. I included a slightly modified version of the code below that also works if there are no open Safari windows. I also fixed some minor warnings reported by RuboCop.

#!/usr/bin/ruby

require 'cfpropertylist'

path = File.expand_path '~/Library/Safari/Bookmarks.plist'
plist = CFPropertyList::List.new file: path

list = plist.value.value['Children'].value.select do |item|
  title = item.value['Title']
  title.value == 'com.apple.ReadingList'
end.first.value['Children'].value

bookmarks = list.map do |item|
  item.value['URLString'].value
end.reverse

puts "Opening #{bookmarks.count} tabs"

`osascript -e 'tell application "Safari" to if windows is {} then reopen'`
bookmarks.each do |url|
  `osascript -e '
    tell application "Safari"
      tell window 1 to make new tab with properties {URL:"#{url}"}
    end tell'`
  print '.'
end

puts ''

@fabge
Copy link

fabge commented Aug 31, 2019

Thank you for writing this up!
I tried to execute the code like this:
image

Unfortunately I get following error:
image

Anything I'm doing wrong or is the script broken?
Best regards

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