You can do this with an AppleScript. Use the Script Editor in the Utilities folder. Here is a script I wrote for you just now. It works on one folder at a time-- not the top level folder, but the one in your example that is 3587915.
When you run this script it asks you to choose a folder. In this example you would open 3587915 and then click "Choose." Very important: don't choose your 3589919. We are only working on one folder at a time here. If this works for you we can expand to looping through the folders in the top-level folder.
tell application "Finder"
set the_folder to choose folder
set the_folder_name to name of the_folder
set the_subfolders to every folder in the_folder
repeat with I from 1 to count of the_subfolders
set name of every file of item I of the_subfolders to the_folder_name & "_" & I & ".pdf"
move every file of item I of the_subfolders to the_folder
end repeat
--
-- optionally delete the subfolders
delete every folder in the_folder -- put two dashes in front of this line to comment it out
end tell
Here are screenshots showing you how it works. You should practice on a set of folders that don't matter at first. You can see my starting set-up in the first picture and the result in the second. The captions on the pictures are not showing up (for me) so here is what they should be:
Picture 1: Automator Service, with slightly modified AppleScript.
Picture 2: folder_1, folder_2, folder_3, folder_4, and folder_5 each have a single pdf in them. Only folder_1's contents are shown here.
Picture 3: This is the result. The subfolders are gone. The files are named after the parent folder.
You can save the script as an app. Or, you can run the script within a step in Automator since Automator can run an AppleScript. That's in the Utilities section of Automator, "Run AppleScript." I would include a picture but I am new here and am only allowed two pictures until I am upgraded.
If you want to do it with a service, you use almost the same script, but you have to pass the control-clicked folder. Rather than using the AppleScript step "Choose Folder" you tell your Automator Service to "receive selected folder in the Finder" and you set "the_folder" to item 1 of the input. See the first picture. Works like a champ here!
UPDATE: I misread your question and originally gave an answer that started at "character 1." If I understand correctly you want to trim off the first two characters, and the last two. Therefore, you want to start at "character 3." I have made the correction here.
Do it this way:
set the_short_name to characters 3 thru -2 of the_original_name as string
That's all there is to it.
Since you're working with Hazel you can get the name of the file this way:
set the_original_name to name of theFile
You may want to pull off more than just two characters since the last four characters are probably ".png" so take off 6 characters, and then add ".png" to the short name.
One more thing: Hazel may think that the renamed file is a "new" file, and consequently run the AppleScript on the renamed file over and over until the name is so short it can't get shorter. The way I'd handle that is I'd move the file out of the watched folder first, and rename it after that. In my case I have a folder called "Renamed Screenshots" on my desktop-- your path is going to be different. Use your own username (not "youruser" as I've put in the line below).
move theFile to (POSIX file "/Users/youruser/Desktop/Renamed Screenshots")
set the name of theFile to the_short_name & ".png"
That should do it.
Best Answer
The following AppleScript should do the job. It removes only an underscore followed by eight digits, e.g.
_2020012
, at the end of a filename without changing the extension. I don't know how familiar you are with using AppleScripts but you should be okay if you carefully follow the directions below. Sorry that I can't make its creation simpler but, once created, it is simple to use.To place this code on your Mac, select File->New->Service inside Automator and then double click on "Run AppleScript" among the "Library" options. Select the "files or folders" option in the "Service receives selected" menu at the top of the newly opened window. Paste the code inside the window and save it through File->Save inside Automator. Make sure you select all of the code above before copying it from here as a few lines of the code run quite a bit to the right outside the highlighted window here.
You can give this AppleScript a name such as "Remove_Number" while saving it inside Automator. The first attached picture shows how things should look like in Automator after the code is pasted and saved. You can quit Automator after the code is saved (but it does not actually matter).
When completed, you should be able to change the names of any number of files you might select through Finder. After selecting the files to be changed in Finder, right click on the selected files and you should see an option with the name you have given the AppleScript towards the bottom of the right-click menu. (The 2nd attached picture shows how things look like in Finder at this stage.) Any selected file with an underscore followed by eight digits at the end of its name should have that underscore and the digits removed immediately after you select the AppleScript from the right-click menu.