Applescript to append a folder name

Hi there,

I’m trying to create an Applescript that is the first step in an Automator workflow.
When I drop a folder on to the Workflow I want a dialog to appear asking what the supplier name is and allowing the user to physically type it in, then add this to the end of the original folder name.

For instance:

Original folder name:

“UK089918”

Dropped on to workflow would result in the dialog prompt:
“What is the supplier name?”
User enters: “CALLAN”

Folder renames to (remaining in the same location it was dragged from):
“UK089918 CALLAN”

Or the above could be attached to a folder action when the original folder is dropped into it the script fires.

I’d like to keep it in Automator as the next step initiates another workflow.

Any help greatly appreciated.

Regards

I never use Automator so here is a folder action script.

on run -- used only for tests
	set p2d to path to desktop as string
	set this_folder to (p2d & "hot:") as alias
	set added_items to {(p2d & "UK089918:") as alias}
	adding folder items to this_folder after receiving added_items
end run

on adding folder items to this_folder after receiving added_items
	set dropped to item 1 of added_items
	set supplierName to text returned of (display dialog "What is the supplier name?" default answer "")
	tell application "Finder"
		set origName to name of dropped
		set newName to origName & space & supplierName
		set name of dropped to newName
	end tell
end adding folder items to

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) lundi 22 juin 2020 18:47:47

Excellent once more Yvan!

One thing I have noticed is the dialog seems to repeat in a loop tho?
E.g.: once UK089918 is dropped on the folder action dialog appears and user enters supplier resulting in the folder name change to UK089918 CALLAN
Then the dialog appears again and the user has to Cancel to get out of the loop.

Expanding on this tho how would I amend the dialog to reference the original folder name as a prompt?
For instance their may be several folders I will be copying across and won’t all have the same supplier name.

E.G.:
UK089918 supplier is CALLAN
IRE56772 supplier is BARONIE
290887 supplier is APA
etc etc

So, for example, the dialog would say “What is the supplier name of UK089918?”
Then the script progresses through all the folders dropped asking the relevant question of each folder and then stops once all folders have been renamed.

Is this possible?

Many thanks

Once again it’s really boring to get questions evolving to require more features.
Is it so difficult to describe the real problem in a single question ?

on run -- used only for tests
	set p2d to path to desktop as string
	set this_folder to (p2d & "hot:") as alias
	set added_items to {(p2d & "UK089918:") as alias, (p2d & "KU089918:") as alias}
	adding folder items to this_folder after receiving added_items
end run

on adding folder items to this_folder after receiving added_items
	repeat with dropped in added_items
		tell application "Finder"
			set origName to name of dropped
		end tell
		set supplierName to text returned of (display dialog "What is the supplier name for " & origName default answer "")
		tell application "Finder"
			set newName to origName & space & supplierName
			set name of dropped to newName
		end tell
	end repeat
end adding folder items to

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) mardi 23 juin 2020 13:27:43

Oops. the run handler used for tests masked a feature of folder actions scripts.
When an item is renamed in the hot folder it’s treated as a newly dropped item.
The well known tip is to move it in a subfolder before renaming it.

on run -- used only for tests
	set p2d to path to desktop as string
	set this_folder to (p2d & "hot:") as alias
	set added_items to {(p2d & "UK089918:") as alias, (p2d & "KU089918:") as alias}
	adding folder items to this_folder after receiving added_items
end run

on adding folder items to this_folder after receiving added_items
	set renamedName to "renamed_demaner"
	set renamedFolder to (this_folder as string) & renamedName
	tell application "Finder"
		if not (exists folder renamedFolder) then
			make new folder at this_folder with properties {name:renamedName}
		end if
	end tell
	repeat with dropped in added_items
		tell application "Finder" to set origName to name of dropped
		if origName is not renamedName then
			set supplierName to text returned of (display dialog "What is the supplier name for " & origName default answer "")
				set newName to origName & space & supplierName
			tell application "Finder"
				set moved to (move dropped to folder renamedFolder) as alias
				set name of moved to newName
			end tell
		end if
	end repeat
end adding folder items to

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) mardi 23 juin 2020 14:16:32

Hi Yvan.

Apologies I was unaware of the impact the script would have once multiple instances were added to the Folder Action in real time, hence why I evolved the request.
I understand that knowing what the overall objective is can be fairly critical to the coding and shall attempt to run thru all scenarios before posting. I was unaware I had done this on previous posts so again, my apologies if so.

Thank you for your additional responses. I will check them out as soon as possible.

Regards

I posted a script using the Finder because I wanted to be sure that the syntax used would be valid for every OS.
Below is a version using System Events. It works under 10.13.6 but I’m not sure that it does under Catalina.
I would be glad to learn how it behaves.

on run -- used only for tests
	set p2d to path to desktop as string
	set this_folder to (p2d & "hot:") as alias
	set added_items to {(p2d & "UK089918:") as alias, (p2d & "KU089918:") as alias}
	adding folder items to this_folder after receiving added_items
end run

on adding folder items to this_folder after receiving added_items
	set renamedName to "renamed_demaner"
	set renamedFolder to (this_folder as string) & renamedName
	tell application "System Events"
		if not (exists folder renamedFolder) then
			make new folder at end of this_folder with properties {name:renamedName}
		end if
	end tell
	repeat with dropped in added_items
		tell application "System Events" to set origName to name of dropped
		if origName is not renamedName then
			set supplierName to text returned of (display dialog "What is the supplier name for " & origName default answer "")
			set newName to origName & space & supplierName
			tell application "System Events"
				set moved to (move dropped to folder renamedFolder)
				set name of moved to newName
			end tell
		end if
	end repeat
end adding folder items to

Here is an alternate code using ASObjC

use AppleScript version "2.5"
use scripting additions
use framework "Foundation"

on run -- used only for tests
	set p2d to path to desktop as string
	set this_folder to (p2d & "hot:") as alias
	set added_items to {(p2d & "UK089918:") as alias, (p2d & "KU089918:") as alias}
	adding folder items to this_folder after receiving added_items
end run

on adding folder items to this_folder after receiving added_items
	set fileManager to current application's NSFileManager's defaultManager()
	set renamedName to "renamed_demaner"
	set POSIXDest to current application's NSString's stringWithString:(POSIX path of ((this_folder as string) & renamedName))
	set destURL to current application's NSURL's fileURLWithPath:POSIXDest
	-- if the subfolder isn't available, create it
	set {theResult, theError} to fileManager's createDirectoryAtURL:destURL withIntermediateDirectories:true attributes:(missing value) |error|:(reference)
	
	repeat with dropped in added_items
		set POSIXDropped to (current application's NSString's stringWithString:(POSIX path of dropped))
		set origName to (POSIXDropped's lastPathComponent()) as string
		if origName is not renamedName then
			set supplierName to text returned of (display dialog "What is the supplier name for " & origName default answer "")
			set newPOSIXPath to (POSIXDest's stringByAppendingPathComponent:(origName & space & supplierName))
			set {theResult, theError} to (fileManager's moveItemAtPath:POSIXDropped toPath:newPOSIXPath |error|:(reference))
		end if
	end repeat
end adding folder items to

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) mercredi 24 juin 2020 16:47:00

Apologies for the late reply Yvan.
I’m on the same OS version as you so am unaware of how this would behave.

Thanks again for your help.

Regards