How do I view paths relative to a specific folder?

I have indexed a folder. I would like to view the path of files relative to this folder.

~/Documents/Reports/Accounting/... (rest of file path) ...

becomes equivalent to …

~/... (rest of file path) ...

How can I go about doing this?


JJW

In a script or the UI?

What I hope for first is a UI method. For example, I might look to set a meta-field for the database. The meta field could be a REGEX expression or AppleScript that would strip the front portions of the path to display a relative path.

What I am after is a view for any file showing in the main window when using any criteria.

BTW, I might add a feature request here. The root path to indexed folders is set. The view of path information for any content in that indexed folder might be far easier to comprehend when it would show RELATIVE to the indexed folder rather than being the full path.

EDIT: What would actually be helpful is when we could control the extent of expansion of the PATH information without having to program to extract various levels.


JJW

Hold on a second…
The Path of indexed items isn’t tilde-expanded, but the full path is reported in the Info inspector, e.g.,

image

… and also the item list…

I am after a way to shorten the path in the item list view. In your example, the result might instead be

~/.../Screenshot of Devonthink 3.png

In general, the path starts with the root, a few characters, then some dots, then the final path. Thinking again out loud, it would be more useful on long path text to collapse the first items in the path in order to set highest priority to preserve the last (two) levels in the path.


JJW

It’s an unusual request. Development would have to assess this change.

Here is an AppleScript in the meantime.

(*
extract a truncated relative path for a selection in DT
jjw
2021-08-05
Caveats: The value is stored in the custom key RelativePath.
The custom key in DT can apparently be anything that resolves to this
after doing a REMOVE SPACES and ignore case operation.
Examples: Relative Path, relativepath, relativePATH
*)

use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions

on run {}
	tell application id "DNtp" to set theList to the selection
	if theList is {} then return
	my performSmartRule(theList)
end run

on performSmartRule(theSelections)
	set theResult to display dialog "From what level?" default answer "2" buttons {"Go", "Cancel"} default button "Go"
	if the button returned of theResult is "Cancel" then return
	try
		set theLevel to (the text returned of theResult) as number
	on error
		set theLevel to 2
	end try
	tell application id "DNtp"
		repeat with theItem in theSelections
			set thePath to the path of theItem as text
			set theRelativePath to my extractRelativePathtoLevel(thePath, theLevel)
			set the custom meta data of theItem to {RelativePATH:theRelativePath}
		end repeat
	end tell
end performSmartRule

on extractRelativePathtoLevel(thePath, level)
	set cTID to text item delimiters
	set text item delimiters to "/"
	set theCount to the count of text items of thePath
	if theCount < level then
		set level to the number of text items of thePath
		set thePrefix to ""
	else
		set thePrefix to "..[" & theCount - level - 1 & "]../"
	end if
	set theLevel to level * -1
	set theRPath to (text items theLevel thru -2 of thePath) as text
	set text item delimiters to cTID
	return (thePrefix & theRPath & "/")
end extractRelativePathtoLevel

It’s what some shells do, to shorten the prompt if you’re deep down ten levels of macOS path names …

1 Like

Updated AppleScript now returns relative path for indexed or relative location for imported.

(*
extract a truncated relative path or location for a selection in DT
jjw
2021-09-06 - returns relative path for indexed, relative location for imported
2021-08-05
Caveats: The value is stored in the custom key RelativePath.
The custom key in DT can apparently be anything that resolves to this
after doing a REMOVE SPACES and ignore case operation.
Examples: Relative Path, relativepath, relativePATH
*)

use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions

on run {}
	tell application id "DNtp" to set theList to the selection
	if theList is {} then return
	my performSmartRule(theList)
end run

on performSmartRule(theSelections)
	set theResult to display dialog "From what level?" default answer "2" buttons {"Go", "Cancel"} default button "Go"
	if the button returned of theResult is "Cancel" then return
	try
		set theLevel to (the text returned of theResult) as number
	on error
		set theLevel to 2
	end try
	tell application id "DNtp"
		repeat with theItem in theSelections
			if the indexed of theItem is true then
				set thePath to the path of theItem as text
				set theRelativePath to my extractRelativePathtoLevel(thePath, theLevel)
				set the custom meta data of theItem to {RelativePATH:theRelativePath}
			else
				set theLocation to the location of theItem as text
				set theRelativeLocation to my extractRelativePathtoLevel(theLocation, theLevel)
				set the custom meta data of theItem to {RelativePATH:theRelativeLocation}
			end if
		end repeat
	end tell
end performSmartRule

on extractRelativePathtoLevel(thePath, level)
	set cTID to text item delimiters
	set text item delimiters to "/"
	set theCount to the count of text items of thePath
	if theCount < level then
		set level to the number of text items of thePath
		set thePrefix to ""
	else
		set thePrefix to "..[" & theCount - level - 1 & "]../"
	end if
	set theLevel to level * -1
	set theRPath to (text items theLevel thru -2 of thePath) as text
	set text item delimiters to cTID
	return (thePrefix & theRPath & "/")
end extractRelativePathtoLevel

1 Like