Best way to test something is a document, in AppleScript?

A recent AppleScript script by @pete31 included a test for groups, like this:

set thisRecord_Type to (type of thisRecord) as string
if thisRecord_Type is in {"group", "«constant ****DTgr»"} then
    ...
end if

I’m writing some other code where it would be useful to test that something is not a document. Some cursory testing of the values returned by type of X shows that for documents, it returns things like, “markdown”, “txt”, “PDF document”, “RTF”, etc. – basically, the specific type. What’s the best way to test against any document type? It’s quite impractical to write a test against all the possible specific types, even if I could produce a complete list.

I guess you could test whether it’s a group or a smart group

set thisRecord_Type to (type of thisRecord) as string
if thisRecord_Type is in {"group", "«constant ****DTgr»", "smart group", "«constant ****DTsg»"} then
				
end if

1 Like

What’s the purpose of the script actually? Anyway, this should be sufficient in the latest releases (checked this both in the Script Editor.app and the Scripts menu):


tell application id "DNtp"
	repeat with thisRecord in selected records
		if (type of thisRecord as string) is in {"group", "smart group", "feed"} then
			display dialog ((name of thisRecord) as string) & " is a node (" & (type of thisRecord as string) & ")"
		else
			display dialog ((name of thisRecord) as string) & " is a document (" & (type of thisRecord as string) & ")"
		end if
	end repeat
end tell
1 Like

Somehow it seemed like there might be more types than groups, smart groups, and feed. For example, are tags really indistinguishable from groups at the AppleScript level?

Initially, my question was just so that I could further expand the icon-copying script from another posting, but that led to the question in my mind of how to test more directly that something is a document.

Anyway, thanks everyone!

Yes, that is correct.

type (bookmark/‌feed/‌formatted note/‌group/‌html/‌markdown/‌PDF document/‌picture/‌plist/‌quicktime/‌rtf/‌rtfd/‌script/‌sheet/‌smart group/‌txt/‌unknown/‌webarchive/‌xml, r/o) : The type of a record. Note: In compiled menu/toolbar scripts you might have to use a string representation of the type for comparisons.

@cgrunenberg: Since there is a tags group class, should there be a separate type as well?

1 Like

Where is that list of types from? It looks like a line from an AppleScript dictionary description, but I can’t find that in what Script Debugger shows.

I don’t use Script Debugger. I use (and have always used) Apple’s built-in Script Editor.

Shift-Command-O > DEVONthink 3 in Script Editor.

My mistake. Script Debugger’s entry looks like this:

I overlooked the “type4”, which I mistakenly assumed would be like one of the other “type” entries, but in fact, clicking “type4” reveals a long list of types (matching what you see in Script Editor):

No problem. That could be helpful info for others new to Script Debugger. :slight_smile:

There’s a tag type property.