Renaming PDFs

I'm trying to renaming PDF files with it's PDF content but just don't work. That's how i'm trying:

  1. Set Variable “pdfPath” to Text “%TriggerValue%”
  2. execute applescript:
tell application "Keyboard Maestro Engine"
    set filePath to get variable "pdfPath"
    if filePath is "'" then
        return "Error: No se pudo obtener la ruta del archivo"
    end if

    log "Procesando archivo: " & filePath
end tell

tell application "Preview"
    try
        -- Cerrar documentos previos
        close every document saving no

        -- Abrir el archivo PDF
        open POSIX file filePath
        activate
        delay 1

        -- Copiar todo el contenido
        tell application "System Events"
            tell process "Preview"
                keystroke "a" using command down
                delay 0.5
                keystroke "c" using command down
            end tell
        end tell

        delay 0.5
        set PDFContent to the clipboard

        close every document saving no

        -- Buscar el número de identificación 
        set idNumber to do shell script "echo " & quoted form of PDFContent & " | grep -oE '[0-9]{5,12}'"

        if idNumber is not "" then
            -- Obtener directorio y construir nuevo nombre
            set fileDirectory to do shell script "dirname " & quoted form of filePath
            set newName to idNumber & " HC.pdf"

            -- Renombrar el archivo
            try
                do shell script "mv " & quoted form of filePath & " " & quoted form of (fileDirectory & "/" & newName)
                tell application "Keyboard Maestro Engine" to set variable "ScriptResult" to "Archivo renombrado a: " & newName
            on error errMsg
                tell application "Keyboard Maestro Engine" to set variable "ScriptResult" to "Error al renombrar: " & errMsg
            end try
        else
            tell application "Keyboard Maestro Engine" to set variable "ScriptResult" to "No se encontró número de identificación"
        end if

    on error errMsg
        -- Asegurar que se cierren los documentos
        close every document saving no
        tell application "Keyboard Maestro Engine" to set variable "ScriptResult" to "Error: " & errMsg
    end try
end tell

tell application "Preview" to quit
  1. Notification “%ExecutingMacro%”

I'm not a programmer just getting help from claude 3.5. Thanks all of u for the help

I see two other people working on a response, but my response is just to use this action:

Large Language Models:

  1. get into the 90%-100% range with approximate retrievals of plausible looking syntax, but
  2. are hardly better than a toin-coss (tailling off now at around 50%) with code that has the right meaning and function.

( See, for example: Wolfram LLM Benchmarking Project )

Better to spend the same time explaining (to other humans) what it is that you really need.

It sounds like you want to base the .PDF name on the contents of the file.
Can you show us an example of:

  • PDF file contents
  • the file name that you want to derive from such contents

( Presumably you are not thinking of an enormous file name which contains the whole text of the PDF ?)


For the first step – obtaining the raw text content of a PDF, see:

Looks like you're trying to rename the file based on a contained invoice number or similar. @ComplexPoint has done the hard work with their "text extraction" routine -- here's a macro that uses that, letting you pick your file via a dialog and then renaming it:

Rename PDF.kmmacros (22.2 KB)

Image

Hi, thanks for your response, it's exactly what I'm trying to do - find a string of numbers in the PDF file and rename it with that. With Hazel it's very easy but with KM it's a nightmare

El texto:

Atención odontológica

Tipo de plan

POS

Aseguradora

EPS SUR S.A.

Apellidos

LUJ ATEHORTUA

Nombres

CAR DE JES

Edad

61 AÑOS

Dirección residencia

CL 25 83 B-3

Tipo identificación

CC

Nro identificación

71600608

Sexo

MASCULINO

Fecha nacimiento

1961/04/2

Teléfono

5805832

Teléfono

3158588846

The string of numbers that I want to extract is 71600608 and end with a space and HC: “71600608 HC"​​​​​​​​​​​​​​