Random Note Review

Hey, I am kind of new to Scripting and was wondering if anyone could tell me whether the following is possible or not and to guide me on how to do it:

I want to create a script that opens a random file (usually a personal note, plain or rich text) from a certain Database or Group. I guess the random part is where I am lost in trying to write the script.

The rationale behind this is clearly summarized here: https://praxis.fortelabs.co/randomnote-web/

Thank you!

4 Likes

Here’s a simple example:


tell application id "DNtp"
	set theContents to (contents of current database)
	set theContent to some item of theContents
	open window for record theContent
end tell

Wow! So simple and yet so effective. Thank you!

Took me a while to figure out how the “contents” of a group were called, but for those who like me struggle with Script, this one will pick a random note from the currently selected group:


tell application id "DNtp"
	set theContents to (children of current group)
	set theContent to some item of theContents
	open window for record theContent
end tell

3 Likes

Bear in mind children used this way will include groups.

Thanks for the warning. Any way to exclude groups?

Thx!

1 Like

Use…

set currGroup to current group
set theContents to (children of currGroup whose (type is not group))
1 Like

Why would you want to open random note? Curious

1 Like

Bluefrog — The script is written to pull a note randomly from a certain group. How would you set it to first select a random group … and then pull a random note from that randomly selected group?

Thanks.

Here is a basic teaching edition approach…

property ignoredGroups : {"Tags", "Inbox", "Trash"}

tell application id "DNtp"
	set theDatabase to current database
	set groupList to (parents of theDatabase whose (location does not contain "/Tags")) -- Filtering out the Tags groups as they are parents as well.
	set randomItem to some item of groupList -- This is asking for a random item in the list
	if (name of randomItem) is not in ignoredGroups then -- Ignoring anything specified above

		set fileList to (children of randomItem whose (type is not group) or (type is not smart group))
		-- Groups and smart groups can be children so filter them out
		if (count fileList) > 0 then -- If there's more than one file in the random group
			set randomFile to some item of fileList -- Grab a random file
			open window for record randomFile -- Open it in a document window
		else -- Otherwise alert the user a random empty group has been chosen
			display alert "No files in: " & (name of randomItem)
		end if

	end if
end tell
5 Likes

Thanks, Jim!

This is nearly exactly what I need. However, whenever I run the script, “randomItem” is always a randomly selected Group but not a randomly selected note from within that randomly selected group. Since receiving your tip I’ve been trying to tweak it to exclude Groups from the selection process so that it only grabs a document or file (like you did with Trash and Inbox), but with no luck. Thoughts?

1 Like

I modified the script above.
Note: With a very large database of groups, this will be less performant.

1 Like

Well, mmoren10 provided a link up there.

But the general idea is if you have something like an external brain, similar to maybe a personal wiki, and in my case called a Zettelkasten (translated from German as note box or box of notes), you sometimes save information that you either forget or that isn’t really ready to write down etc.

Usually all info in my Zettelkasten is linked to other info bits. But sometimes, when I’m writing down info that doesn’t relate to all existing info, a note goes unlinked to other notes. Unlinked notes can be forgotten and never be used again (which either means you never needed that knowledge again or you have it twice in your system and the links go to the twin).

Also, I never copy much text from what I read but formulate my insights in a new way, only seldomly using direct quotations. So improving my own writing is always in demand.

And thirdly, this is about serendipity. I collect notes whenever I read and so far my Zettelkasten consists of about 200 notes with summarisations of books or articles and about 500 notes of personally formulated insights from all my work. Of course I cannot remember them all. Random looks at some of them might trigger a new idea for a current project etc?

So these are the three reasons for a daily random note reading: checking the linking, optimizing my writing and invoking serendipity!

I have two groups with Zettels in them (for the time being), one for source notes and the other for my idea/thinking notes. So my script looks like this:

tell application id "DNtp"
    set theGroupZ to (get record with uuid "put id here")
    set theGroupQ to (get record with uuid "put id here")
    set theContentq to (children of theGroupQ)
    set theContentz to (children of theGroupZ)

    set zufall to (random number from 0 to 1)
    if zufall > 0 then
        set theContent to some item of theContentz
    else
        set theContent to some item of theContentq
    end if

    open window for record theContent
end tell
1 Like

I made a tweak to this to recursively call itself again if it finds an empty group, since I generally want an item, not an error in the log.

use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions

property ignoredGroups : {"Tags", "Inbox", "Trash"}

tell application id "DNtp"
	-- functions in applescript aka subroutines:
	-- http://www.macosxautomation.com/applescript/sbrt/
	my get_item()
end tell

on get_item()
	tell application id "DNtp"
		set theDatabase to current database
		set groupList to (parents of theDatabase whose (location does not contain "/Tags")) -- Filtering out the Tags groups as they are parents as well.
		set randomItem to some item of groupList -- This is asking for a random item in the list
		if (name of randomItem) is not in ignoredGroups then -- Ignoring anything specified above
			set fileList to (children of randomItem whose (type is not group) or (type is not smart group))
			-- Groups and smart groups can be children so filter them out
			if (count fileList) > 0 then -- If there's more than one file in the random group
				set randomFile to some item of fileList -- Grab a random file
				open window for record randomFile -- Open it in a document window
			else -- Otherwise recursively reselect a group since a random empty group has been chosen
				my get_item()
			end if
		end if
	end tell
end get_item

Welcome @naupaka

A nice variation indeed. :slight_smile:

1 Like