Select random item from a list of records with AppleScript

I’d like to flag a random item on a daily base by a reminder script:

on performReminder(theRecord)
try
	tell application id "DNtp"
		set theItem to some item of theRecord
		set state of theItem to true
	end tell
on error errMsg
	
	display alert (localized string "Error when flagging notes") message errMsg
	
end try
end performReminder

However, I get an error:

Most likely because “theRecord” is not of the correct type. I’m not the most proficient AppleScripter to say the least :wink: Any suggestions?

1 Like

Some item needs a list, e.g. the children of a group.

The first line is only for testing. comment it out by prefixing with # (or delete it).


tell application id "DNtp" to my performReminder(item 1 of (selection as list)) -- this is only for testing. comment it out by prefixing with # (or delete it)

-- Reminder script: Set flagged state of random child

on performReminder(theGroup)
	tell application id "DNtp"
		try
			set theChildren to children of theGroup whose type is not group
			set theRandomChild to some item of theChildren
			set state of theRandomChild to true
			
		on error error_message number error_number
			if the error_number is not -128 then display alert "DEVONthink" message error_message as warning
			return
		end try
	end tell
end performReminder

The traditional method is using double hyphens, -- This is a comment.

A random item where? In a group? In the entire database?

I use -- for my comments and # to comment out/deactivate a line. Using different ones for each makes it much easier to see what is what.

Another advantage is that this way activating all deactivated lines in a selection can be scripted without uncommenting my own comments which would prevent a script from compiling :slight_smile:

Thanks a lot. Works for me. :+1:

1 Like

is there a way this script can just select an item rather than actually open it? or just open in the main window rather than opening a new window? tagging @pete31 since he has come through for me twice recently in very clutch ways :slight_smile:

This only opens a new window if necessary. :slight_smile:

-- Reminder script: Set selection of window 1 to of random child

on performReminder(theGroup)
	tell application id "DNtp"
		try
			set theChildren to search "kind:any" in theGroup
			if theChildren = {} then return
			set theRandomChild to some item of theChildren
			
			if (exists viewer window 1) then
				set selection of viewer window 1 to {theRandomChild}
			else
				set newWindow to open window for record root of database of theRandomChild
				set selection of newWindow to {theRandomChild}
			end if
			
		on error error_message number error_number
			if the error_number is not -128 then display alert "DEVONthink" message error_message as warning
			return
		end try
	end tell
end performReminder