Rename folder from PDF placed into the same location?

I have a task on a daily basis here at my workplace where a PDF file is copied into a folder named “Outsource” and this folder is then renamed to include the filename of the PDF after the current folder name.

For example:

“Outsource” (original folder)
“job 126163 PO00010621.pdf” (pdf copied into the Outsource folder)

I then manually rename the folder “Outsource Job 126163 - PO00010621”

Is there a script that could automatically rename the folder based on the PDF that has been copied?

Thank you and Merry Christmas,

Regards, Shaun (sticks1977)

Model: Mac Pro
AppleScript: 2.11
Browser: Safari 537.36
Operating System: macOS 10.14

It is simple:

  1. Make your folder HOT folder.
  2. Attach to it the following script:

on adding folder items to aFolder after receiving theItems
	tell application "Finder"
		set PDFname to name of (item 1 of theItems)
		if PDFname ends with ".pdf" then
			set PDFbasename to text 1 thru ((get offset of ".pdf" in PDFname) - 1) of PDFname
			set name of aFolder to (name of aFolder) & " " & PDFbasename
		end if
	end tell
end adding folder items to

@KniazidisR

CAUTION, if my memory is right, after dropping a pdf in the hot folder, the folder name will be changed so it will no longer be active as ‘hot’ folder.

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) mercredi 18 décembre 2019 10:17:23

Yes, Yvan, you are right. Here I fixed my script.

  1. Save my script with name “FolderNameChange.scpt” at special location of your Mac: ~ Library/Workflows/Applications/Folder Actions

  2. Make your folder HOT folder (Right-click on folder → ServicesFolder actions Setup…)

  3. Attach to it this script, “FolderNameChange.scpt” (you will found it in the popup window)


on adding folder items to aFolder after receiving theItems
	tell application "System Events"
		
		set PDFname to name of (item 1 of theItems)
		
		if PDFname ends with ".pdf" then
			
			set aFolderPath to path of (container of aFolder)
			set oldName to name of aFolder
			set PDFbasename to text 1 thru ((get offset of ".pdf" in PDFname) - 1) of PDFname
			set folderActionName to "Outsource " & PDFbasename
			set name of aFolder to folderActionName
			
			make new folder action at end of folder actions with properties {enabled:true, name:folderActionName, path:(aFolderPath & folderActionName)}
			repeat until folder action folderActionName exists
				delay 0.1
			end repeat
			tell folder action folderActionName to make new script at end of scripts with properties {name:"FolderNameChange.scpt"}
			delete folder action oldName
			
		end if
		
	end tell
end adding folder items to

UPDATE: Now the script is debugged and tested. On my Mojave system works fine