Help with workflow/script to rename file by folder name?

I originally posted this in the Automator forums but, based on my understanding, this last piece is really an applescript issue since I’ll have to add that to the end of the workflow. Sorry for the duplicate post but I believe the other one can be deleted.

Hi all, I was hoping someone can help me to put together the last piece of this puzzle. I’ve put together an automator workflow that allows me to resize/scale cbr (comic book) files from their original size down to whatever percentage i choose. I have to unzip the file then highlight all the pictures and use the workflow to resize them…at which point the workflow rezips the file and changes the extension to cbr with the newly scaled images. The last thing I’m trying to do is rename that file (it always comes out as archive.cbr) into the name of the folder that it is located in. I haven’t been able to figure this out as of yet.

Update: I’ve been messing with it again today and cannot nail this down. The closest that I got was added a replace text workflow but that prompts me to manually type the output files name out. I’m trying to have the final outputted cbr file automatically renamed to match the exact folder name (just the name not the entire path). I tried messing with different scripts and nothing seems to work.

To be clear, once the workflow runs, it rezips the files and changes them from archive.zip to archive.cbr. All I am looking to do is change the name of archive.cbr to “whatever the name of the folder is”.cbr

And while we’re on the subject, is there a way to skip the part where I unzip and highlight all the files manuallly before running the workflow? Can that be added tot eh workflow so that it’ll do everything in one step?

Hello.

I have made the meat of a small AppleScript action for you, it worked at my place, with "get marked Finder-objects in front, but you may want to try this on some copies so you assert that it works as it should.

You’ll use this AppleScript action, instead of your rename file extension, as it tries to to both.


on run {input, parameters}
	set c to item 1 of input as alias
	tell application "Finder"
		set b to a reference to file c
		set name of b to name of folder of b
		set name extension of b to "cbr"
	end tell
	return input
end run

Thank you for your help. I am not home right now to test this out but will try as soon as I get home. So I should add this script to the end of the workflow above correct? Hopefully this works.

On a similar note, do you know how the best way to go about editing this workflow to include the act of unzipping? Currently I manually unzip the file, then go into the folder, highlight the files and run the workflow (i have it set as a service). Would this work better if it was a folder action? Is that possible?

Thank you again for all your help. The script you gave me, if it works, will save me a massive amount of time. It’s greatly appreciated.

Hello.

Hopefull it works, if it doesn’t then insert log statement’s like “log b” and “log c”, then open console app, be sure that the item “all messages” or similiar is selected, then enter “Automator” as the search word.

When the “get marked” items executed, it was just one item I had marked, and that item got passed over as a hfs path, the way I parsed the “input parameter”.

It will be exiting, and no I have no idea about how to include the act of unzipping. Honestly, I use Automator as little as I can, but the thought reaches me that you can "get marked items, the finder actions, then have an unzip action as the next action, before you continue with the compression? I wouldn’t do it myself, I’d rather uncompress manually, unless there are 100’s of zip files to be processed.

I just wanted to let you know that your script worked perfectly. The folder is renamed exactly as I wanted it too and worked seamlessly with my workflow. Thank you so much for your help.

Glad I could help. :slight_smile: