Renaming a file with parent directory - If there is one.

Hi Everyone - This seems like a great forum and resource. I am a newbie at Applescripting, but can see how powerful it is. I am hoping to get a little help with creating a script of my own. Hopefully I can be of help once I learn more!

I’d like to rename a file to the name of its nearest parent directory. So if I have:

-Incoming
–Folder1
—File.m4v

I’d like the name to be Folder1.jpg.

I’ve seen some good scripts that deal with ideas like this, but nothing that nails it on the head. The one thing I’d like to add as an if/then/else statement because some of the files do not have a parent folder and thus their parent folder is “Incoming” as seen above. And I’d rather not get a lot of files named “Incoming”.

So IF the parent folder of file.jpg is NOT “Incoming”, THEN rename it to its parent folder. ELSE ignore.

I hope that makes sense. Thanks again!

Hi,

try this


set theFile to choose file
tell application "Finder"
	set parentFolderName to name of container of theFile
	if parentFolderName is not "Incoming" then
		set fileExtension to name extension of theFile
		set name of theFile to parentFolderName & "." & fileExtension
	end if
end tell

Thanks for the reply Stefan! I am trying to run this script inside of a program called Hazel. When I run the script I get the following error. I don’t know if I should go to the Hazel forums for this or if there is something you could help with. Thanks again for any help!

hazelworker[42081] OSAScript error: {
NSLocalizedDescription = “No user interaction allowed.”;
NSLocalizedFailureReason = “No user interaction allowed.”;
OSAScriptErrorAppAddressKey = “<NSAppleEventDescriptor: [0x0,2 "hazelworker"]>”;
OSAScriptErrorAppNameKey = “Hazel.prefPane”;
OSAScriptErrorBriefMessageKey = “No user interaction allowed.”;
OSAScriptErrorMessageKey = “No user interaction allowed.”;
OSAScriptErrorNumberKey = “-1713”;
OSAScriptErrorRangeKey = “NSRange: {0, 0}”;
}

No user interaction allowed means exactly what it says.

The first line of the script asks the user to choose a file.
In which way do you pass the information about the file?

That actually did the trick! I removed the line:

set theFile to choose file

And now it works just fine. Thank you Stefan!