Searching inside your bookmarks

Whether via AppleScript or App is there a way to search information inside your bookmarks similar to that of a search engine? I have 100s of bookmarks and when searching them I would need to know the title, most of the time the information I need is not displayed in the title. Any ideas?

Yes I have an idea.

You can have a look at the bookmarks.plist file inside the Safari folder of the Library folder of your home folder.

Just open it in an edtior, if you don’t have propertlist edtior, or something more convenient.

You should be able to parse the bookmarks with the property lists suite of System Events.

You didn’t mention which browser you’re using. Safari?

I have researched this problem but haven’t started implementing anything yet.

I think your best bet may be working with MySQL from the Terminal or a do shell script command.

Hello.

It may be too much data for System Events propertylist suite to handle for all I know.

I have some follow up questions, given that the answer for ccstone’s browser question is Safari:

What do you anticipate to do with the data, once you have them, ist there just to look at the titles, with the url’s on one line, and maybe launch them?.

I am not sure if I can manage or are willing to provide the exact information about which book mark “container” and which folder/ subfolder the bookmark resides in.

Hello.

Just for the hell of it, I figured I could play a little bit with sed, and some shell scripting for starters, and I am at leas able to get results in a terminal window, by now, I’ll come back with something Applescriptish a little bit later.

You can use a commandline like below in a terminal window, once you have followed the instructions below.
You’ll need a utility called plutil in order to accomplish this, hopefully it is installed on your machine as a standard utility. Execute the command which plutil, in a terminal window to confirm that it is there.

Here is the command line you can use in a terminal window: (It will all get better later. :slight_smile: )

 ./showit |./bookmark.sed |tr -s '        ' |tr -d '      ' |grep yoursearchterm

Both the scripts below, must be saved under the names showit and bookmark, you need to execute the command:
chmod u+x showit and chmod u+x bookmark, in order to make them executable.

Both files must be saved as utf8 No BOM.

showit:

#!/bin/bash plutil -convert xml1 ~/Library/Safari/Bookmarks.plist -o -
bookmark.sed

#!/usr/bin/sed -nf :t /<key>URIDictionary<\/key>/ { n /<dict>/ { n /<key>[Tt]itle<\/key>/ { n s_<string>\([^<][^<]*\)<\/string>_\1_ h n n /<key>URLString<\/key>/ { n s_<string>\([^<][^<]*\)<\/string>_\1_ H x s/\n/-|-/p b t } } } }
Edit

It would be nice to know, how you’d use it, before scripting it in AppleScript, you can get “full reports”, and queries with regular expressions, but the best I think I can do is to let you load a bookmark in a new tab/window in Safari.
Using this scheme, I can’t figure out which folder the bookmark came from, by using the scheme, but hopefully this solves 80% of your troubles.

Hello.

I thought some more about it, and given that bookmarks are somewhat static in nature, once they have been created, I settled for rendering them on a web page in safari, with the script above with some small modifications.

I have tried to render it nicely, links are shown fully out, under the title, so it should be easy to search with Safari’s search function.

The script bundle can be found here
Here is the final applescript:

-- © 2013 McUsr and put in public domain
set pt to POSIX path of (path to resource "Scripts")
do shell script "(echo \"<ul type=\"square\">\"; " & quoted form of (pt & "showit") & " |tr -d '\\t' | " & quoted form of (pt & "bookmark3.sed") & " ; echo \"</ul>\" ) | " & quoted form of (pt & "html_wrap") & " >|/tmp/bookmarks.html ; open /tmp/bookmarks.html "

Showit from the post above is unchanged.

Bookmarks is in its third revision:

[code]#!/usr/bin/sed -nf

2013 © McUsr and put in public domain.

:t
/URIDictionary</key>/ {
n
// {
n
/[Tt]itle</key>/ {
n
i\

  • \
    \
    		s_<string>\([^<][^<]*\)<\/string>_<dt><p>\1</p></dt>_p
    		n
    		n
    		/<key>URLString<\/key>/ {
    			n
    			s_<string>\([^<][^<]*\)<\/string>_<dd><a href="\1">\1</a></dd>_p
    		i\
    
    \
  • \
    			b t
    		}
    	}
    }
    

    }[/code]
    I added html_wrapper, that wraps the preamble, and the postscript around the produce.

    [code]#!/bin/bash

    - © 2013 McUsr and put in public domain

    echo ‘’
    echo ‘’
    echo ‘’
    echo ’ ’
    echo ’ Wrapper’
    echo ‘’
    echo ‘’
    cat -
    echo ‘’
    echo ‘’
    '[/code]
    You’ll find those in the tummy of the script-bundle, and if you haven’t got plutil on your system, then I think I can provide a workaround.

    All those shell scripts resides in the Contents/Resources/Scripts folder of SafarBookMark.scptd

    The script bundle can be found here

    Edit

    I see now, that this satisfies the need for OP of seaching for bookmarks, but I’ll be back tomorrow with a sorted version of the bookmarks list, by title. :slight_smile:

    Well done! I look forward to deconstructing this.

    Hello.

    Thanks.

    I was in luck with plutil, which was actually quite the versatile tool. :slight_smile:

    You have to play with sed to grasp it, if you don’t know it. I recommend you google “MacMahon sed”, and plays with it through the examples of the document you’ll find.

    Sed can have nested search patterns, ( you can think of it as a wheel turning round, with two shoe-boxes, a loop with those two variables, regexps, for addressing and substitutes, and around 10 one letter commands, and labels.

    I don’t parse the xml, I cheat, I know that one tag follows the other, so that when I have gotten what I want, I "short circut and branch back to start. (If the Bookmarks file, which is maintained by Safari had been malformed, then I believe plutil would have said so, by exiting with an error code, effectively sabotaging the whole pipeline.

    It beats me, that at least the way I extract the data, can be used for pretty much anything, when it comes to getting data out of application generated .plist files.

    Edit
    And for the presentation part, it may be feasible to include any bookmarks fields, and then you can provide a link to that, instead of the html anchor which is suitable for html, and thereby jump right into the data of the application. An Omnifocus task for instance, (It goes without saying that for the latter method to work, you must be using a protocol the application supports).

    Hello.

    I got some time to finish off the SafariSortedBookMarks today.

    The one basic change, is that I now use the first initial script (with the -|-), and then sorts the items, sorry, there will be no folding her, as sort may be a bit grumpy about non-standard characters.) Anyways, after it is sorted, it is then processed through the sed-script below.

    [code]#!/usr/bin/sed -nf

    2013 © McUsr and put in public domain.

    i\

  • \
    \

    s_^(.)(-|-)(.)_

    \1

    \

    \3

    _p i\
    \
  • \[/code] And the command-line looks like this:
    -- © 2013 McUsr and put in public domain
    set pt to POSIX path of (path to resource "Scripts")
    do shell script "(echo \"<ul type=\"square\">\"; " & quoted form of (pt & "showit") & " |tr -d '\\t' | " & quoted form of (pt & "bookmark2.sed") & " |/usr/bin/sort |" & quoted form of (pt & "bookmark4.sed") & " ; echo \"</ul>\" ) | " & quoted form of (pt & "html_wrap") & " >|/tmp/bookmarks.html ; open /tmp/bookmarks.html "
    
    
    

    I have made a script bundle that you can download from here.