How to Trim N Characters from End? (SOLVED)

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

Moderator: Mr_Noodle

EDIT: I solved this—screenshots below roughly depict how I used Custom Text Attributes per the (somewhat vague but generally helpful) response from Mr. Noodle.

Image
Image
Image
Image
Image
Image

Original Message: I'm downloading stock assets from Envato then using Hazel to match .zip files from that URL, then using Hazel actions to unarchive and rename them, replacing characters like "-" and "_" with spaces. Easy enough so far using the match patterns with the "Rename" action. However, the filenames also include dates at the end of the filename (I'm guessing upload dates), and they're typically always a different and generally unpredictable date.

Examples of unedited filenames include:
  • list-titles-for-apple-motion-and-fcpx-2023-12-29-01-30-03-utc.zip
  • id-card-mockup-2024-05-02-20-23-18-utc

What is predictable (as far as I've encountered, at least), is that the string is always the same number of characters (23 or 24, depending on whether you include the preceding hyphen or space). However, I couldn't find a function in Hazel from trimming characters in either direction, nor could I find a way (short of scripting) to match via Regex (e.g. ^\d{4}-\d{2}-\d{2}-\d{2}-\d{2}-\d{2}-\w{3}$). Even with ChatGPT and Google, I'm also not quite skilled enough to figure out how to put together a script or Apple Shortcut to pass this task to (from Hazel, anyway—I do have renaming apps configured, but that defeats the point of trying to automate via Hazel).

Am I missing something? Is there a way within Hazel that I could accomplish this without further scripting? Or what would the script be? I couldn't figure out how to put together an Applescript that interacted with Hazel the correct way (at least with the in-app method).
Last edited by mrbrownjeremy on Mon Apr 14, 2025 8:13 pm, edited 1 time in total.
mrbrownjeremy
 
Posts: 2
Joined: Mon Apr 14, 2025 7:43 am

Re: How to Trim N Characters from End? Mon Apr 14, 2025 9:30 am • by Mr_Noodle
You can use Hazel's match patterns. Have a custom text attribute match "anything" and after that, the pattern to match the characters that you want to get rid of. By doing that, you have a custom attribute with the text you want to keep. You can then use that in a Rename action.
Mr_Noodle
Site Admin
 
Posts: 11865
Joined: Sun Sep 03, 2006 1:30 am
Location: New York City

Re: How to Trim N Characters from End? Mon Apr 14, 2025 4:27 pm • by mrbrownjeremy
EDIT: Nevermind, I got it. The process to get here was painful (and disappointing, considering this is a paid app meant to save time), but at the very least knowing it was possible kept me going. I've attached a screenshot below for anyone who might need a proper visualization of the setup, and can hopefully save them the hours I lost in my journey to save time today.

Image
Image
Image
Image
Image
Image

Original Message: Thank you for the response, but I still don't understand. Can you send an example (or link to an example) of something similar? I've spent a few hours today trying to understand how to wrangle this and am feeling very defeated. I included screenshots below to show where I'm at with my configuration.


Image
Image
Image
mrbrownjeremy
 
Posts: 2
Joined: Mon Apr 14, 2025 7:43 am


Return to Support