prepend or append path of selected file(s) to file(s) and move

I am a complete newbie when it comes to programming and scripting on Mac OS X but I have had some experience doing simple programs in the past in Unix.

Anyway, I would like to select files on a Mac that are in a folder and copy them to a new folder and rename them to include the path of the original folder.
So for example.
baseFolder:originals is the folder path
The originals folder will contain one to several files. e.g. filename1.pdf, filename2.pdf, etc
I want to select one or more of the files and send them to new folder like → WorkFolder:
with a new name that includes path. I would like to have delimiter between folders that is pc compatible.
So in the example filename1.pdf would be moved to WorkFolder and renamed to baseFolder-originals-filename1.pdf
Thanks in advance if you can help.
Regards,
Bruce

Hi,

try this


set destinationFolder to choose folder
tell application "Finder" to set sel to selection
set TID to text item delimiters
repeat with oneItem in sel
	set text item delimiters to ":"
	set pathItems to text items of (oneItem as text)
	set text item delimiters to "-"
	set newName to pathItems as text
	set theSource to quoted form of POSIX path of (oneItem as text)
	set theDestination to quoted form of ((POSIX path of destinationFolder) & newName)
	do shell script "/bin/cp -pr " & theSource & space & theDestination
end repeat
set text item delimiters to TID

HI Stefan,
You are awesome… I figured out how to add to automator so that I could drag and drop the files on to the script.
I would like to hardwire the destination folder into the script instead of allowing the user to choose a folder.
I suppose that should be as simple as setting your first line from ‘set destinationFolder to choose folder’
to 'set destinationFolder to . Are there any rules for setting a path? I suppose if I look through the numerous results on this forum I could find this but if you have time. :wink:
path I want is Mac HD:Users:leroy:Documents
Bruce

HI Stefan,
I figured it out… This apple scripting is pretty cool and powerful. Thanks for getting me on the path… no pun intended… lol
Bruce

It’s important to have a colon at the end of a string path in case of a folder,
because we need it to concatenate the destination path

set destinationPath to "Mac HD:Users:leroy:Documents:"

You could also “hard-code” the documents folder with

path to documents folder

Note: path to documents folder returns an alias, not a string path,
but coercing to POSIX path works with both string path or alias

HI Stefan,
Thanks… very cool. I owe a beer or two to you. Let me know how i can get it to you… serious.
One last question. I can run the application using Automator ( I have Mac OS 10.6.2) but Automator does not work on another Mac we have which is version 10.4.11.
Is there a way I can turn this applescript into a pure applescript that acts like a droplet? Similar to the droplets you create in Photoshop. That way I would be sure it runs on most if not all 10.x macs.
Thanks,
Bruce

this should work in Tiger and higher, save it as application bundle


on run
	open (choose file with multiple selections allowed)
end run

on open theseItems
	set destinationFolder to path to documents folder
	set TID to text item delimiters
	repeat with oneItem in theseItems
		set text item delimiters to ":"
		set pathItems to text items of (oneItem as string)
		set text item delimiters to "-"
		set newName to pathItems as string
		set theSource to quoted form of POSIX path of oneItem
		set theDestination to quoted form of ((POSIX path of destinationFolder) & newName)
		do shell script "/bin/cp -pr " & theSource & space & theDestination
	end repeat
	set text item delimiters to TID
end open