Rename bunch of files using external script

My DEVONthink db started by importing bunch of files from dirs. They weren’t properly sorted. Recently I developed a script, which can rename file based on it content (LLM+bunch of other stuff to make it right). is there a way to launch it so it could provide better naming for some of the files stored in the database? (I know I can copy files out of db, do the renaming and re-import, but this will cause some dates to be lost). If this is doable in v3 is a good, but if I’ll have to upgrade to v4 because of this, this will be the force which will make me do it.

Perhaps, but who knows without seeing the code?

Depending on the script, it may be technically possible, however version 4 has access to external AI available in-app. It may also be possible to do it with internal tools inside version 4 instead of needing a separate script.

Also, version 3 is in maintenance and will be retired from development and support at some point. Version 4 is the active focus of development. We highly encourage you to upgrade sooner rather than later.

script receives path to file as argument and does the magic and can either rename file in-place, or return new name on stdout.

I know about v3, and know about AI support in V4, but… I couldn’t figure out how to take an action on a file using external script.

You should post your code.

Not sure why that’s needed, but…

#!/usr/bin/env bash

set -euo pipefail

if [[ "$#" -ne 1 ]]; then
  echo "Usage: $0 <file_path>" >&2
  exit 1
fi

FILE_PATH="$1"

# Check file exists
if [[ ! -f "$FILE_PATH" ]]; then
  echo "Error: File not found: $FILE_PATH" >&2
  exit 1
fi

NEW_NAME=$(external_app "$FILE_PATH")

echo "$NEW_NAME"

So people can help you.

2 Likes

Just stick this shell script into a call to do shell script (AppleScript) or doShellScript (JavaScript), taking care to quote correctly. No biggy.

If you really expect something more helpful, you should provide details on the mysterious external_app. Or were you asking how to call a shell script from an Apple/JavaScript script?

3 Likes

Not sure I’m following. I don’t understand how to make an action with selection of bunch of files in file selector.

This surely isn’t your complete code but if you are trying to pass to the shell POSIX paths of items in a DEVONthink database and changing the names, you should NOT do this. You should never modify the internal structure of a database behind DEVONthink’s back.

This…

cd /Users/leonidasx/Databases/DT4\ Databases/Shoebox.dtBase2/Files.noindex/txt/d
mv rss\ sources.txt rss-sources.txt

… leads to…
image

Here is a simple example processing selected records in a database with DEVONthink’s knowledge and assistance and using some shell commands to produce a new name…

tell application id "DNtp"
	if (selected records) is {} then return
	repeat with theRecord in (selected records)
		if (exists path of theRecord) and (path of theRecord is not "") then
			set recPath to path of theRecord
			set recName to do shell script "basename " & (quoted form of recPath as string) & " | tr ' ' '_' "
			set name of theRecord to recName
		end if
	end repeat
end tell
1 Like

I know I shouldn’t rename posix path and that’s what I want to avoid.

Got things working with this, probably, not optimal, but at least it’s starting point.

tell application id "DNtp"
	repeat with theRecord in selected records
		set name of theRecord to my theRename(text of theRecord, POSIX file (path of theRecord) as alias)
	end repeat
end tell

on theRename(txt, thePath)
	tell application "Shortcuts Events"
		set theResult to run shortcut "OCRout" with input thePath
	end tell
	
	-- Check if result is missing or empty
	if theResult is missing value or theResult is "" then
		display alert "Warning" message "Shortcut returned no output" buttons {"OK"}
		return txt -- Fall back to original text
	end if
	
	-- display alert "GPT" message (theResult as string) buttons {"OK", "Cancel"} default button "OK"
	return theResult as string
end theRename

Thanks for the suggestions and pointing into the right direction!

1 Like