Rename File to Grandparent Folder's Name then Append Predefined Text

Hi,

I’m trying to create an Automator service that uses AppleScript to do the following:

Get the name of the grandparent folder
Rename the file to its grandparent folder’s name
Append predefined text to the end of the new name

Example:

I know how to make an Automator service, but I’m having trouble making the AppleScript. Let me know what you think! I’ll appreciate any help or links.


use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions
set theFile to choose file

tell application "System Events"
	set theFileExtension to name extension of theFile
	set name of theFile to (name of container of container of theFile) & "." & theFileExtension
end tell

Awesome! That worked. I added a part to append the word “_APPEND”.


use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions
set theFile to choose file

tell application "System Events"
	set theFileExtension to name extension of theFile
	set name of theFile to (name of container of container of theFile) & "_APPEND" & "." & theFileExtension
end tell

But, I can’t get Automator to send the selected file to the script.

  1. Open Automator
  2. In the window that pops up, highlight Quick Action and then hit Choose.
  3. Now in the Library section on the left, click on Utilities and then find Set Value of Variable. Drag it to the main window on the right.
  4. In the Variable dropdown, choose New variable… and call it original_Files
  5. In the Workflow receives current choose files or folders
  6. Set the in dropdown to Finder.app
  7. Still in the Utilities section of the Library on the left, find Run AppleScript. Drag it to the main window under our last step.

Opens shell-script:

[b]on run {input, parameters}

(* Your script goes here *)

return input

end run[/b]

  1. Replace (* Your script goes here *) with our script lines to get this:

on run {input, parameters}
	set theFile to item 1 of input
	tell application "System Events"
		set theFileExtension to name extension of theFile
		set name of theFile to (name of container of container of theFile) & "_APPEND" & "." & theFileExtension
	end tell
	return input
end run
  1. Go to File > Save and give your new quick action a name. I’ll call mine append_GrandFolderName.

You have just created a Service (quick action). This means that if you right-click anybody file in Finder, you can see service append_GrandFolderName at bottom of pop up menu and use it by clicking.

Thanks for the update, KniazidisR. I followed your instructions a few times, but I keep getting an error, I included screenshots below.

Is the variable missing from the script? If the Variable is “original_name”, should the script include “set theFile to original_name”? Or is that, like, not a thing. :confused:

Thanks again!

Your input is not a file but a list of files (even if there is only one file in the list).

Hi, I was at work. Shane is right and most likely we need to change code line set theFile to input to that:

set theFile to item 1 of input

OK, I already checked - now everything works correctly.

Nailed it! Thanks, KniazidisR. And thanks for the support, Stanley.