|
# 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 |