I keep a bunch of cheat-sheets & other various notes in Stickies.app, but I have less frequently needed notes in various other kinds of text files and it seemed that whenever I needed to find a certain command that I was sure I’d recorded somewhere, I could never find it. Well, I discovered the other day that Spotlight does not search the contents of Stickies notes. I read a lot about it in various blogs. I even tried to use a set of XCode 4.2 instructions in XCode 6 to create a Spotlight Importer for the Stickies database, but quickly discovered I was in over my head, so I set about constructing a hack to easily save all the stickies as backup text files that would be indexed and searchable by Spotlight.
The catch to this trick is the fact that I have various Sticky Notes bound to specific Mission Control desktops (some of which I hide under the dock with a huge font – to use as desktop labels) and the code for grabbing the stickies content required access to the visible window. I had first experimented with cycling through the desktops with key code trickery, but discovered a more efficient way – the Window menu item! Selecting a window in the window menu changes desktops implicitly, so I used the getAppInterface.scpt script (google shows that it’s not out there anymore, so if anyone requests it, I’ll post) to elucidate the menu interface and came up with this script.
set fileName to “”
set stickyNum to 0
set windowMenuListStart to 8
set destFldr to (choose folder with prompt “Choose a destination folder:”) as text
set mydestFldr to POSIX path of destFldr
tell application “Stickies” to activate
tell application “System Events”
tell process “Stickies”
set winMenuList to name of every menu item of menu 1 of menu bar item “Window” of menu bar 1
repeat with winMenuItemNum from windowMenuListStart to (count winMenuList)
click menu item winMenuItemNum of menu “Window” of menu bar item “Window” of menu bar 1 of application process “Stickies” of application “System Events”
set awindow to name of front window
set noteContent to value of text area 1 of scroll area 1 of window awindow
set stickyNum to stickyNum + 1
set fileName to “stickies” & “_” & stickyNum & “.txt” as string
set theFile to mydestFldr & fileName
do shell script “/bin/echo ” & quoted form of noteContent & ” > ” & quoted form of theFile
A few notes about the script… It needed a delay in order for mission control to be able to keep up with the script getting the front window. If you end up with duplicated and skipped notes, you might need to increase the number of seconds in the delay.
You can only run this script when the screensaver and screen lock is inactive, so putting it in a cron job is not a viable option. It will cause mission control to change desktops and you may not end up on the desktop you started on when it’s all done. I tried valiantly to find a way to determine the current desktop number but it looks like that is squirreled away in the code for the Dock and it very difficult to extract. What you might do to somewhat automate this so that you don’t have to periodically run it manually is use something like ScriptSaver to run the script just before the screen saver comes on. I have not tried this though and I don’t even know if ScriptSaver is still around.
One last note: if you try to debug an issue by adding dialog windows with messages, note that this will affect the “front window” function.
Once you’ve backed up your Stickies as text files and Spotlight has had time to index them, you will then see search results in the documents section with file names like “stickies_4.txt”. If you don’t want to wait for Spotlight to do the indexing on its own time, you can go to the Terminal and have it index your newly created stickies backup files with commands like this:
mdimport -d1 stickies_4.txt
The end result is that spotlight searches now show the contents of the stickies you’ve backed up. The caveat is that you have to occasionally run the script to keep them up to date. If anyone figures out a better way to do this, I’m all ears – so please comment below.