AppleScript: Get record name without suffix

I’ve set DEVONthink to display suffixes.

When I use set theName to name of theRecord in a new database I get the the name without suffix.

Doing this in different old databases I get the name with suffix,

Never found that confusing as I display suffixes and thought this was normal behaviour, but it now seems to be a bug

In version 2 there was an import preference to name files with/without extension, in version 3 there’s now a display preference that can be changed anytime. The old files were probably imported with the extension and AppleScript always delivers the name independent of the preferences.

Does that mean something like this doesn’t make sense and I have to
check the string if it contains a suffix?

set displaySuffix to do shell script "defaults read com.devon-technologies.think3 DisplaySuffix"

if displaySuffix = 1 then set theName to Basename(theName)

on Basename(theName)
	set revName to reverse of characters of theName as string
	set revNameWithoutExtension to characters ((character offset of "." in revName) + 1) thru -1 in revName as string
	set theBasename to reverse of characters of revNameWithoutExtension as string
end Basename

If so, any hint how to check for a suffix?

AppleScript doesn’t depend on preferences, therefore the most reliable solution is probably to get the extension of the path and then to check whether the name has the same extension.

Thanks!

Here’s a handler to get the name without suffix

set theName to my recordName(name of theRecord, filename of theRecord)

on recordName(theName, theFileName)
	set revName to reverse of (characters of theName) as string
	set suffixName to reverse of (characters 1 thru ((character offset of "." in revName) - 1) in revName) as string
	set revFileName to reverse of (characters of theFileName) as string
	set suffixFileName to reverse of (characters 1 thru ((character offset of "." in revFileName) - 1) in revFileName) as string
	if suffixName = suffixFileName then set theName to reverse of (characters ((character offset of "." in revName) + 1) thru -1 in revName) as string
	return theName
end recordName

Note: You’re processing the record’s name and filename before determining if they need to be processed in the first place. While modern computers are fast enough to not be bogged down by this, at scale you are wasting CPU cycles processing things that don’t need to be processed.

Here’s a teaching edition version that first determines if the name needs to be processed. It also processes the name is a different fashion, using AppleScript’s text item delimiters in a way not as often seen.

tell application id "DNtp"
	set sel to item 1 of (selection as list)
	if (name of sel) = (filename of sel) then -- If the name and filename are equal, the name has an extension
		my stripextension(name of sel as string) -- Pass the record's name to the handler
	end if
end tell

on stripextension(recordName)
	set ot to AppleScript's text item delimiters -- Cache the default text delimiter
	
	set reversedName to (reverse of characters of recordName) as string -- Reverse the characters of the name and put it into a new string
	
	set AppleScript's text item delimiters to "." -- Split the reversed name on "."		
	set nameComponents to (text items of reversedName) -- Get the components
	set culledName to (items 2 thru -1 of nameComponents) as string -- Get the components, ignoring the first which is the extension, as a new string joined with the text delimiter if present
	set AppleScript's text item delimiters to ot -- reset the text delimiter
	
	set newName to (reverse of characters of culledName) as string -- Reverse the reversed name again
end stripextension

In general you’re right about only processing if necessary (I often forget that) but in this case it wouldn’t catch duplicates, so adding number of duplicates would be necessary. This usage of text item delimiters is nice, thanks!

No problem

Doesn’t that set the revNameWithoutExtension to the part of the file name before the first dot? So if the file name would be “first part.second part.pdf”, the revNameWithoutExtension becomes “first part”?

I’m referring to Pete’s version of the script. Bluefrog’s does fix this issue by reversing the name and then looking for the dot. Instructive to see what one can do. Although it would be a lot easier if Applescript’s vocabulary was richer.

Ah, I forgot to change filename to theName. Fixed it, now it’s clear that I’m also reversing the string before looking for the dot.