# set record for window / Random article

**URL:** <https://discourse.devontechnologies.com/t/set-record-for-window-random-article/8256>\
**Category:** Automation\
**Created:** [September 20, 2009, 1:34pm UTC](https://discourse.devontechnologies.com/t/set-record-for-window-random-article/8256 "2009-09-20T13:34:51Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![Maak](https://discourse.devontechnologies.com/user_avatar/discourse.devontechnologies.com/maak/32/61_2.png) [@Maak](https://discourse.devontechnologies.com/u/Maak)\
**Post date:** [September 20, 2009, 1:34pm UTC](https://discourse.devontechnologies.com/t/set-record-for-window-random-article/8256/1 "2009-09-20T13:34:51Z")

</div>

with a growing database, I often find myself browsing and clicking through just to get new inspiration. While the see also function is already excellent, what I am looking for is a similar experience to a box of index cards, where I can just pick a card and be surprised - or the “random article” link in wikipedia.

do you think it might pe possible to write a script that could switch the view to a random file in the database?

---

<div class="post-metadata">

**Author:** ![marjar](https://discourse.devontechnologies.com/user_avatar/discourse.devontechnologies.com/marjar/32/6106_2.png) [@marjar](https://discourse.devontechnologies.com/u/marjar)\
**Post date:** [September 21, 2009, 1:30am UTC](https://discourse.devontechnologies.com/t/set-record-for-window-random-article/8256/2 "2009-09-21T01:30:52Z")

</div>

Hope this helps:

```auto
tell application "DEVONthink Pro"
	set db to database 1
	repeat
		try
			set dum to count parents of the db
			set num to random number from 1 to dum
			set mum to parent num of the db
			set chum to count mum's children
			set num to random number from 1 to chum
			set kiddo to child num of mum
			if kiddo's type is not group then exit repeat
		end try
	end repeat
	set the record of window 1 to kiddo
end tell

```

---

<div class="post-metadata">

**Author:** ![Maak](https://discourse.devontechnologies.com/user_avatar/discourse.devontechnologies.com/maak/32/61_2.png) [@Maak](https://discourse.devontechnologies.com/u/Maak)\
**Post date:** [September 21, 2009, 7:40am UTC](https://discourse.devontechnologies.com/t/set-record-for-window-random-article/8256/3 "2009-09-21T07:40:30Z")

</div>

great! however, I still get an error message for this line:

set the record of window 1 to kiddo

---

<div class="post-metadata">

**Author:** ![marjar](https://discourse.devontechnologies.com/user_avatar/discourse.devontechnologies.com/marjar/32/6106_2.png) [@marjar](https://discourse.devontechnologies.com/u/marjar)\
**Post date:** [September 21, 2009, 9:09am UTC](https://discourse.devontechnologies.com/t/set-record-for-window-random-article/8256/4 "2009-09-21T09:09:13Z")

</div>

Yeah, I thought I needed a bit more error checking. Try this:

```auto
tell application "DEVONthink Pro"
	set db to database 1
	repeat
		try
			set dum to count parents of the db
			set num to random number from 1 to dum
			set mum to parent num of the db
			set chum to count mum's children
			set num to random number from 1 to chum
			set kiddo to child num of mum
			if kiddo's type is not group then exit repeat
		end try
	end repeat
	
	if not (exists (the 1st document window whose visible is true)) then
		open window for record kiddo
	else
		set myWindow to the 1st document window whose visible is true
		set myWindow's record to kiddo
	end if
end tell

```

Hopefully that’ll do the trick.

---

<div class="post-metadata">

**Author:** ![Maak](https://discourse.devontechnologies.com/user_avatar/discourse.devontechnologies.com/maak/32/61_2.png) [@Maak](https://discourse.devontechnologies.com/u/Maak)\
**Post date:** [September 21, 2009, 9:49am UTC](https://discourse.devontechnologies.com/t/set-record-for-window-random-article/8256/5 "2009-09-21T09:49:20Z")

</div>

that did it - wow! It opens a random document in a new window, in my case, an old tax report which did not belong there and after another run, something more interesting, as in: [“And now for something completely different”](http://www.youtube.com/view_play_list?p=FDF01C3BAFF2FFA2&search_query=completely+different).

One day I’ll find out how to tweak the script in a way that the documents open in the current viewer window instead a new document - but again, I am happy that it works - you [fixed](http://www.youtube.com/watch?v=Tq_xTeWiv6I&feature=PlayList&p=FDF01C3BAFF2FFA2&index=0) it! thanks!

---

<div class="post-metadata">

**Author:** ![Maak](https://discourse.devontechnologies.com/user_avatar/discourse.devontechnologies.com/maak/32/61_2.png) [@Maak](https://discourse.devontechnologies.com/u/Maak)\
**Post date:** [February 18, 2010, 10:02pm UTC](https://discourse.devontechnologies.com/t/set-record-for-window-random-article/8256/6 "2010-02-18T22:02:25Z")

</div>

sorry to bother you again, but since I discovered the [Oblique Strategies](http://tools.blackhat-seo.com/strategies/) - Deck ([wikipedia](http://en.wikipedia.org/wiki/Oblique_Strategies)), I try to change the script above, but I can’t get it to work.

What I am trying to do, is change the random script above to consider only records from the selected group and display it in the window that is already active instead of opening a new one.

While the above script works great in opening any record from the database in the new window, a script for the random display of folder contents would be great for creative strategies. For example, writing a short story and having the computer pick a scenario…

But I got stuck. Dear script wizards, please help! 😒

---

<div class="post-metadata">

**Author:** ![marjar](https://discourse.devontechnologies.com/user_avatar/discourse.devontechnologies.com/marjar/32/6106_2.png) [@marjar](https://discourse.devontechnologies.com/u/marjar)\
**Post date:** [February 25, 2010, 9:12am UTC](https://discourse.devontechnologies.com/t/set-record-for-window-random-article/8256/7 "2010-02-25T09:12:30Z")

</div>

I’m currently recovering from a virus ( a head cold in summer!), but once I’ve finished my recovery, I’ll get onto it - it’s no trouble, I like a challenge.

marcus

---

<div class="post-metadata">

**Author:** ![cyol](https://discourse.devontechnologies.com/letter_avatar_proxy/v4/letter/c/278dde/32.png) [@cyol](https://discourse.devontechnologies.com/u/cyol)\
**Post date:** [February 25, 2010, 5:01pm UTC](https://discourse.devontechnologies.com/t/set-record-for-window-random-article/8256/8 "2010-02-25T17:01:44Z")

</div>

What a splendid idea!

When I hit my F7 key, I’ve often thought to myself that I’ll probably never see the text I’m saving again.

This idea and implementation will give me [a pleasant surprise](http://www.backwoodshome.com/articles/burris60.html) whenever I need the inspiration.

Cool beans!

---

<div class="post-metadata">

**Author:** ![Maak](https://discourse.devontechnologies.com/user_avatar/discourse.devontechnologies.com/maak/32/61_2.png) [@Maak](https://discourse.devontechnologies.com/u/Maak)\
**Post date:** [March 6, 2010, 10:30pm UTC](https://discourse.devontechnologies.com/t/set-record-for-window-random-article/8256/9 "2010-03-06T22:30:15Z")

</div>

> [@marjar](#):
>
> I’m currently recovering from a virus ( a head cold in summer!), but once I’ve finished my recovery, I’ll get onto it - it’s no trouble, I like a challenge.
> 
> marcus

good luck with your virus, and thanks for the help!

Meanwhile, I tried to change the script myself. I got only half of the way, but here it is:

```auto
tell application id "com.devon-technologies.thinkpro2"
	set cgr to the current group
	set chnum to count cgr's children
	set randnum to random number from 1 to chnum
	set RandomRecord to child randnum of cgr
	open window for record RandomRecord
	
	--how can I set the selection of the viewer window?
	--here is what I tried so far
	--set the root of viewer window 1 to RandomRecord
	--set selection of viewer window 1 to record RandomRecord	
end tell

```

This script opens a random record in the current group in a new window.

For some reason, however, I can’t get it to open the random record in the active viewer window. Is it possible to set the selection of the viewer window at all, and if so, how?

Thanks!

---

<div class="post-metadata">

**Author:** ![sjk](https://discourse.devontechnologies.com/user_avatar/discourse.devontechnologies.com/sjk/32/32_2.png) [@sjk](https://discourse.devontechnologies.com/u/sjk)\
**Post date:** [March 7, 2010, 1:05am UTC](https://discourse.devontechnologies.com/t/set-record-for-window-random-article/8256/10 "2010-03-07T01:05:25Z")

</div>

> [@Maak](#):
>
> good luck with your virus

My first reaction to reading that before seeing marcus’ original text was “WTF, an OS X virus?!?”. 😃

---

<div class="post-metadata">

**Author:** ![Maak](https://discourse.devontechnologies.com/user_avatar/discourse.devontechnologies.com/maak/32/61_2.png) [@Maak](https://discourse.devontechnologies.com/u/Maak)\
**Post date:** [March 7, 2010, 7:55am UTC](https://discourse.devontechnologies.com/t/set-record-for-window-random-article/8256/11 "2010-03-07T07:55:30Z")

</div>

ok, another one. Just discovered that you can use “some” for the randomness , which shortens my first applescript experiment quite a bit. I have no idea what I am doing here, never coded a line in my life, however: As with DTPO 2.0.1, the active record of a viewer window can’t be set, or at least, not by me… 😢

```auto
tell application id "com.devon-technologies.thinkpro2"
	try
		set RandomRecord to some child of the current group
		if RandomRecord's type is not group then exit repeat
	end try
	
	--if not (exists (the 1st document window whose visible is true)) then
	open window for record RandomRecord
	--else
	--set myWindow to the 1st document window whose visible is true
	--set myWindow's record to RandomRecord
	--end if
end tell

```

Heck, what are the magic words to have the random record appear in the active viewer window? I don’t know. Dear different time-zone-people: How do you do (“it”)? ❓❓❓

---

<div class="post-metadata">

**Author:** ![marjar](https://discourse.devontechnologies.com/user_avatar/discourse.devontechnologies.com/marjar/32/6106_2.png) [@marjar](https://discourse.devontechnologies.com/u/marjar)\
**Post date:** [March 7, 2010, 10:04am UTC](https://discourse.devontechnologies.com/t/set-record-for-window-random-article/8256/12 "2010-03-07T10:04:23Z")

</div>

In my experience, using “some” gets inferior results to generating a random number from the count of the result; you get the same number far too often.

As far as the viewer window problem goes, a viewer window is something like a Finder window - you can’t display documents in it. On the other hand, if your random record is a group, DTPO will display it in a document window, even though you can’t do anything with it, like double-clicking on icons etc. I wrote something that deals with that problem:

```auto
tell application id "com.devon-technologies.thinkpro2"
	set cgr to the current group
	set chnum to count cgr's children
	set randnum to random number from 1 to chnum
	set RandomRecord to child randnum of cgr
	
	set itsKind to kind of RandomRecord
	if itsKind contains "Group" then
		set the root of viewer window 1 to RandomRecord
	else
		if exists (the 1st document window whose visible is true) then
			set docWin to the 1st document window whose visible is true
			set docWin's record to RandomRecord
		else
			open window for record RandomRecord
		end if
	end if
end tell

```

Hope that helps.

---

<div class="post-metadata">

**Author:** ![Maak](https://discourse.devontechnologies.com/user_avatar/discourse.devontechnologies.com/maak/32/61_2.png) [@Maak](https://discourse.devontechnologies.com/u/Maak)\
**Post date:** [March 7, 2010, 11:23am UTC](https://discourse.devontechnologies.com/t/set-record-for-window-random-article/8256/13 "2010-03-07T11:23:32Z")

</div>

thanks, this works better, though it still opens a new window instead displaying the random record in an existing window (in 2-pane-view)

Nevertheless, what the script does great is open a random article from the current group in a new window.

Now I am a bit confused with the terminology : Is it even possible to set the record of a DT window in 2- or 3-pane view? @Christian?

\*Maybe this scripting question is of general interest, so I changed this post’s title.

---

<div class="post-metadata">

**Author:** ![system](https://devontech-discourse.s3.dualstack.us-east-1.amazonaws.com/uploads/original/2X/4/4e6ff70c96be2eb307a274d890bba8d71ba671b3.png) [@system](https://discourse.devontechnologies.com/u/system)\
**Post date:** [January 21, 2022, 11:06am UTC](https://discourse.devontechnologies.com/t/set-record-for-window-random-article/8256/14 "2022-01-21T11:06:02Z")

</div>

Closed on January 21, 2022, 11:06am UTC


