See list of all file types within a database?

Is it possible to see a list of all the file types/extensions that exist within a database? I’m making smart groups by file types, but I don’t know what file types there are in it. I don’t want to trawl through it all manually of course.

Well, you could certainly start with File > Database Properties….

Stephen

Expand all the groups and tags in your database, click and shift-click to select all, and use Tools->Create Metadata Overview.

Open the new Metadata Overview.tsv file in Apple numbers. Go to Categories in the Inspector and add a new category based on Kind#String. Collapse all the categories to see a list of the represented types.

There are probably easier ways, but I’m childishly amused by the Categories feature. Any day I can turn on Categories is a good day.

I probably don’t get out enough.

4 Likes

Add the condition Extension is to a smart group, the popup includes all extensions found in the opened databases.

Where is the list of known extensions stored?

It’s retrieved from the databases.

Ahh, so dynamically created, not stored in a plist somewhere.

Thanks all. Seeing that there is a list of all extensions, do I have to make all these smart groups manually or can I tell Devonthink to create smart groups for each extension in the database?

Why would you want to create smart groups for all the extensions?

It would be easier to create the smart groups as you need them.

1 Like

In addition: there may be several extensions referring to the same for of file (jpeg/jpg, for example). What do you want to achieve with these smart groups?

Because I want to organise my files by extension, via smart groups. Can Devonthink not do this?

You could of course set up the desired (smart) groups on your own. But what’s the advantage or usage scenario compared to e.g. the History smart group sorted by kind?

2 Likes

Yes, but to set up the smart groups on my own, I need to know all the file types that exist in the database - hence my initial question.

Are you indexing or importing files?

The file types (!) are readily available in the smart group dialog. But that is not the same as the extension! Eg, jpeg, jpg and png extensions will all be considered of type image.

This script creates Smart Groups for all extensions of the current database.

-- Create Smart Groups for all extensions

use AppleScript version "2.4"
use framework "Foundation"
use scripting additions

property useUpperCase : true -- if you'd like to use lowercase names change this to false. make sure to delete existing Smart Groups and empty the trash before you change this property

tell application id "DNtp"
	try
		set theDatabase to current database
		set theDatabase_Extensions to my getExtensions(theDatabase)
		set theDestinationGroup to root of theDatabase
		
		repeat with thisExtension in theDatabase_Extensions
			set thisSmartGroup_Predicates to "extension==" & thisExtension
			set theSmartGroups to (smart groups of theDatabase whose search predicates = thisSmartGroup_Predicates)
			if theSmartGroups = {} then
				set thisSmartGroup to create record with {type:smart group, search predicates:thisSmartGroup_Predicates, name:thisExtension, exclude from search:true} in theDestinationGroup
			else
				replicate record (item 1 of theSmartGroups) to theDestinationGroup
			end if
		end repeat
		
	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


on getExtensions(theDatabase)
	try
		tell application id "DNtp" to set theRecords_Paths to path of contents of theDatabase
		set theRecords_Extensions to (current application's NSMutableArray's arrayWithArray:theRecords_Paths)'s valueForKey:"pathExtension"
		if useUpperCase then
			set theRecords_Extensions_upperCase to theRecords_Extensions's valueForKey:"uppercaseString"
		else
			set theRecords_Extensions_upperCase to theRecords_Extensions's valueForKey:"lowercaseString"
		end if
		set theRecords_Extensions_upperCase_deduplicated to (current application's NSOrderedSet's orderedSetWithArray:theRecords_Extensions_upperCase)'s array()
		set theRecords_Extensions_upperCase_deduplicated_filtered to (theRecords_Extensions_upperCase_deduplicated's filteredArrayUsingPredicate:(current application's NSPredicate's predicateWithFormat:"self != ''"))
		return theRecords_Extensions_upperCase_deduplicated_filtered as list
		
	on error error_message number error_number
		activate
		if the error_number is not -128 then display alert "Error: Handler \"getExtensions\"" message error_message as warning
		error number -128
	end try
end getExtensions
1 Like

@BLUEFROG Importing - I decided to go down that route instead of indexing.

@chrillek - a good point, my terminology left room for ambiguity. But I suppose my question would apply to both - both file extensions and file types.

Here is a much simpler (and hopefully easy to learn from) script that works with extensions of imported files.

-- Extensions Smart Rules
-- Created by BLUEFROG/Jim Neumann on Sun 08 21 2022
-- Copyright 2022 DEVONtechnologies, LLC. All rights reserved.

(*Creates one smart rule per known file extension. Works with imported files only.
From https://discourse.devontechnologies.com/t/see-list-of-all-file-types-within-a-database/72265 *)

tell application id "DNtp"
	set currentDB to (current database)
	tell currentDB
		set dbPath to path
		set extensionsList to my getFolders(dbPath)
		if extensionsList ≠ {} then
			set smartRuleDest to create location "/Extensions Smart Rules"
			repeat with theExtension in extensionsList
				create record with {name:theExtension & " files", type:smart group, search predicates:"extension==" & theExtension} in smartRuleDest
			end repeat
		end if
	end tell
end tell

on getFolders(dbPath)
	set fileLocs to ((dbPath as POSIX file) & "Files.noindex" as string)
	tell application "Finder"
		name of (folders of alias fileLocs)
	end tell
end getFolders
1 Like