Filename to include text based upon a keyword in the file

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

Moderator: Mr_Noodle

I am new to Hazel and am attempting my first “scripts”. I may be getting off on the wrong foot…

Consider a file ‘foo.pdf’.

Within file “foo.pdf” there are certain text strings:
    “heading 1 text_1”
    “heading 2 text_2”

I wish to perform the following actions:

Code: Select all
retrieve text_1 & text_2

if text_1 = “alpha”
   filename := “omega gamma text_2.pdf”
else if text_1 =“beta”
   filename := “omega delta text_2.pdf”
else
   the usual exceptions
end if

rename “foo.pdf” to filename
move file to folder folder_1


Examples:

Code: Select all
File contains “heading 1 alpha heading 2 what_ever”.
File renamed to “omega gamma what_ever.pdf”

File contains “heading 1 beta heading 2 what_ever”.
File renamed to “omega delta what_ever.pdf”


The best I’ve been able to solve this is as follows:

Code: Select all
Rule 1
   If all of the following conditions are met
      Full Name is foo.pdf
      Contents contain match “heading 1 “<retrieve 1>
      Contents contain match “heading 2 “<retrieve 2>
   Do the following…
      Rename with pattern: “omega <retrieve 1> <retrieve 2>.pdf”

Rule 2
   If all of the following conditions are met
      Name contains “alpha”
   Do the following…
      Rename with pattern > Replace “alpha” with “gamma”

Rule 3
   If all of the following conditions are met
      Name contains “beta”
   Do the following…
      Rename with pattern > Replace “beta” with “delta”

Rule 4
   If all of the following conditions are met
      Name contains “omega”
   Do the following…
      Move file to folder _1


My Hazel solution seems very… obtuse… wordy…

Is there a better way?

What am I not understanding concerning “The Hazel Way”?

Should I just drop into Apple Shortcuts or a Shell script?

Is there an accepted convention of translating the Hazel GUI code into text for discussion?

Thank you in advance!

XD-2
"Mission Control, this is X-ray-Delta-Two, two-one-zero-three transmission concluded."
XD-2
 
Posts: 2
Joined: Tue Jul 14, 2026 2:10 pm

Try a custom table attribute. You can set up one column with "text_1" which you match against and then rename using "text_2". The manual should have more details.
Mr_Noodle
Site Admin
 
Posts: 12237
Joined: Sun Sep 03, 2006 1:30 am
Location: New York City

Mr_Noodle wrote:Try a custom table attribute. You can set up one column with "text_1" which you match against and then rename using "text_2". The manual should have more details.


Got it! Worked great!

Thank you!

Now on to the next "learning moment"! :)
XD-2
 
Posts: 2
Joined: Tue Jul 14, 2026 2:10 pm


Return to Support