Skip to content

Instantly share code, notes, and snippets.

@nishad
Forked from dvcrn/README.md
Created March 13, 2025 11:49
Show Gist options
  • Save nishad/f952620bf8c3e236a6190485d55a65c3 to your computer and use it in GitHub Desktop.
Save nishad/f952620bf8c3e236a6190485d55a65c3 to your computer and use it in GitHub Desktop.

Revisions

  1. @dvcrn dvcrn revised this gist Sep 14, 2023. 1 changed file with 8 additions and 0 deletions.
    8 changes: 8 additions & 0 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,8 @@
    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.
  2. @dvcrn dvcrn created this gist Sep 14, 2023.
    67 changes: 67 additions & 0 deletions smartrule.applescript
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,67 @@
    # 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