Skip to content

Instantly share code, notes, and snippets.

@dvcrn
Last active October 8, 2024 23:39
Show Gist options
  • Save dvcrn/62056fa25811ef638691ab7131346084 to your computer and use it in GitHub Desktop.
Save dvcrn/62056fa25811ef638691ab7131346084 to your computer and use it in GitHub Desktop.
DEVONthink 3 automatic filenames with ChatGPT

To install, make sure you install the openai CLI:

  1. pip3 install openai
  2. Run which openai to get the full path of where the executable is
  3. Copy the path and replace /opt/homebrew/bin/openai in the script with your path if it's different
  4. Update set OPENAI_API_KEY to "xxx" to use your OpenAI key

To use GPT3.5-turbo instead of gpt-4, change /opt/homebrew/bin/openai api chat_completions.create -m gpt-4 to -m gpt-3.5-turbo or other model with larger context window.

# function to generate a random string
on randomString(length)
set theCharacters to "abcdefghijklmnopqrstuvwxyz0123456789"
set theResult to ""
repeat length times
set theResult to theResult & character (random number from 1 to length of theCharacters) of theCharacters
end repeat
return theResult
end randomString
# store filecontent into a temporary txt file and return the path to it
on storeFileContent(filecontent)
set uniqueIdentifier to my randomString(20)
set posixtmpfile to POSIX path of (path to temporary items folder) & uniqueIdentifier & ".txt"
try
set fhandle to open for access posixtmpfile with write permission
write filecontent to fhandle as «class utf8»
close access fhandle
return posixtmpfile
on error
try
close access posixtmpfile
end try
end try
end storeFileContent
on processRecord(theRecord)
tell application id "DNtp"
if type of theRecord as text is "group" or (word count of theRecord) is 0 then return
set c to plain text of theRecord
# cut c to be max 8000 chars long, if it's longer than 8000. otherwise take the entire content
if length of c > 8000 then
set c to text 1 thru 8000 of c
end if
set posixtmpfile to my storeFileContent(c)
log "temporary filepath: " & posixtmpfile
set OPENAI_API_KEY to "xxx"
set theCommand to "OPENAI_API_KEY='" & OPENAI_API_KEY & "' /opt/homebrew/bin/openai api chat_completions.create -m gpt-4 -g system \"You are a program designed to generate filenames that could match the given text. Output exactly 1 filename that could fit the content and nothing else. Don't output a file extension, separate words by space. No preamble, no extra output, only output the filename. Keep it concise.\" -g user \"$(cat " & posixtmpfile & ")\""
log "executing: " & theCommand
try
set theResult to do shell script theCommand
log "command result: " & theResult
set name of theRecord to theResult
on error errorMessage number errorNumber
log errorMessage
display dialog "Error: " & errorMessage & " (" & errorNumber & ")"
end try
end tell
end processRecord
on performSmartRule(theRecords)
tell application id "DNtp"
repeat with theRecord in theRecords
my processRecord(theRecord)
end repeat
end tell
end performSmartRule
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment