Can I simplify my way of renaming a document?

Get help. Get answers. Let others lend you a hand.

Moderator: Mr_Noodle

Can I simplify my way of renaming a document? Wed Jul 23, 2025 12:41 am • by halloleo
I have some (PDF) documents which I, when they come into a "FileMe" folder

  1. first want to rename and
  2. then move into the approriapte folder.

Hazel is good at the moving part (2), and for the renaming I have a hand-written tool which takes the path to a document and returns the new name for the document.But the renaming itself I am doing in Hazel.

So Hazel calls the tool and then renames the file. Sounds simple, but it looks pretty cumbersome the way I have implemented it: I involves a detour via AppleSCript and an explicit path to the called tool -- very brittle.

Here a screenshot of the rules:

Image

And here the embedded AppleScript:
Code: Select all
set filePath to POSIX path of theFile
set shellCmd to "$HOME/opt/link/bin/name-for-notice" & " " & quoted form of filePath

set shellOut to do shell script shellCmd
if shellOut is not equal to "" then
   set theRes to {hazelPassesScript:true, hazelOutputAttributes:{shellOut}}
else
   set theRes to {hazelPassesScript:false}
end if


Can I simplify this somehow? Particularly, can I get rid of the Applescript (which is only glue_) and tell Hazel directly to call the tool?

Or should my tool do the renaming? But how can I move the file afterwards?
halloleo
 
Posts: 66
Joined: Thu Apr 27, 2017 10:10 pm

Can you describe what actual renaming you need/what your script does?
Mr_Noodle
Site Admin
 
Posts: 11998
Joined: Sun Sep 03, 2006 1:30 am
Location: New York City

Mr_Noodle wrote:Can you describe what actual renaming you need/what your script does?


Sure. The tool does roughly the following:

  1. Read the text layer of the PDF (via `pdfftotext`)
  2. Get two dates in the text with some regexes
  3. Converts these dates to a "shorted" date range string (Example: 13/06/2024 and 17/07/2024 becomes 2024-06-13to07-17)
  4. fills this range string into a filename tempalte
  5. and returns this filename string.

I would rather keep the name generation logic in the tool. (I actually have already a couple of these tools for different types input files.) I just feel the stuff in Hazel around the tool call to return the filename string to hazel is rather complicated...
halloleo
 
Posts: 66
Joined: Thu Apr 27, 2017 10:10 pm

All that should be doable in Hazel but if you insist on keeping the commandline tool, there's no other way to do it at the moment. Apple/JavaScript is the only way to return any attributes back to Hazel.
Mr_Noodle
Site Admin
 
Posts: 11998
Joined: Sun Sep 03, 2006 1:30 am
Location: New York City

Mr_Noodle wrote:All that should be doable in Hazel but if you insist on keeping the commandline tool, there's no other way to do it at the moment. Apple/JavaScript is the only way to return any attributes back to Hazel.


Thank you!

I might consider doing the move first and then letting the tool do calculate the name and do the renaming - this way I don't have to return anything back to Hazel.
halloleo
 
Posts: 66
Joined: Thu Apr 27, 2017 10:10 pm


Return to Support