Changing the name of a file and saving

hi
i am using this script to copy a file from one location to external drive

try
	tell application "Finder" to set disks_ to name of disks whose startup is false
	set chosen_ to choose from list disks_ with prompt "Choose a volume to copy to."
	if chosen_ is false then return -- user canceled, quit script 
	set originalFile to "Macintosh HD:Users:Shared:01.pdf"
	set Newname to "New_name"
	do shell script "ditto " & quoted form of (POSIX path of originalFile) & "  " & (POSIX path of chosen_)
on error
	display dialog "No disk is mounted" buttons ("Ok") default button 1
end try

this is great for me and you guys have help me to build this script
thanks
Now I want to add some more feature to the same script and ope i will not be disapointed.
here is what i want to do
after i choose the volume i want script to display new dialog box where i can change the file name “01.pdf” to 'whatever i want" and then it save the script with new name.
please help me
thanks

After your set original file, add something like:


display dialog "What should the new file be called?" default answer "New_Name"
set Newname to the result as string

thanks for your response
digpen
i added the code to the script like this

tell application "Finder" to set disks_ to name of disks whose startup is false
set chosen_ to choose from list disks_ with prompt "Choose a volume to copy to."
if chosen_ is false then return -- user canceled, quit script 
set originalFile to "Macintosh HD:Users:Shared:01.pdf"
display dialog "What should the new file be called?" default answer "New_Name"
set Newname to the result as string
set Newname to "New_name"
do shell script "ditto " & quoted form of (POSIX path of originalFile) & "  " & (POSIX path of chosen_)
end tell

but doesn’t work - ERROR -Can’t make {text returned:“New_Name”, button returned:“OK”} into a string.

please give some suggestion

try


set Newname to the text returned of result

or similar, try


set Newname to the text returned of display dialog "What should the new file be called?" default answer "New_Name"

and then get rid of the following line later on in your code.

set Newname to “New_Name”

Kevin

thanks ktam
I tried the first code it copied the file with same name as “01.pdf”
and the second code desn’t compiled.
here is what i tried


tell application "Finder" to set disks_ to name of disks whose startup is false
set chosen_ to choose from list disks_ with prompt "Choose a volume to copy to."
if chosen_ is false then return -- user canceled, quit script 
set originalFile to "Macintosh HD:Users:Shared:01.pdf"
display dialog "What should the new file be called?" default answer "New_Name"
set Newname to the text returned of result
do shell script "ditto " & quoted form of (POSIX path of originalFile) & "  " & (POSIX path of chosen_)

and


tell application "Finder" to set disks_ to name of disks whose startup is false
set chosen_ to choose from list disks_ with prompt "Choose a volume to copy to."
if chosen_ is false then return -- user canceled, quit script 
set originalFile to "Macintosh HD:Users:Shared:01.pdf"
set Newname to the text returned of display dialog "What should the new file be called?" default answer "New_Name" 
do shell script "ditto " & quoted form of (POSIX path of originalFile) & "  " & (POSIX path of chosen_)

please give me some suggestions
i need to do this and it is very important for me
thanks

You should use this… Basically the end of your code (the do shell script) didn’t make use of the variable that was defined. I forgot to tell you that the result will include both the text and the button, so you need to define which result you wanted to make decisions on.:

tell application "Finder" to set disks_ to name of disks whose startup is false
set chosen_ to choose from list disks_ with prompt "Choose a volume to copy to."
if chosen_ is false then return -- user canceled, quit script 
set originalFile to "Macintosh HD:Users:Shared:01.pdf"
display dialog "What should the new file be called?" default answer "New_Name"
set newName to the text returned of result
do shell script "ditto " & quoted form of (POSIX path of originalFile) & "? " & (POSIX path of chosen_) & "/" & newName

that is GREAT Digpen
i am looking for this answer for about six days and you solve the mystry.
during those days i was thinking if it is possible to add auto number feature in the script
like if the “01.pdf” already exist in the selected external drive then the script automatically add a suffix "011.pdf’ and if "011.pdf"exist than it add one more number and make it “012.pdf” or "0111.pdf
Actually the whole purpose is that of not replacing the file at same location.
because the users are going to copy lots of files from various location to selected external drive with same name.
if you have any suggestion please send
thankyou very much

You can try something along the lines of:


if not (exists file Newname of folder chosen_ ) then
try
do shell script "ditto " & quoted form of (POSIX path of originalFile) & "  " & (POSIX path of chosen_) & "/" &Newname
end try
else
-->  You'll have to determine how you want to add values to make the name unique

end if

You might be able to append the Finders File Number to the Newname. Best thing to do is check on if the files exist or use a LOOP in the else clause to keep incrementing until the not exists = true (as show above)

it sounds complex to me
i am very new to applescript.
give me some suggestion about how can I stop user to replace files, suppose if they type same name for different file.
thanks for your help

I was thinking that you could use something like this:

tell application "Finder" to set disks_ to name of disks whose startup is false
set chosen_ to choose from list disks_ with prompt "Choose a volume to copy to."
if chosen_ is false then return -- user canceled, quit script 
set originalFile to "Macintosh HD:Users:Shared:01.pdf"

repeat
	
	display dialog "What should the new file be called?" default answer "New_Name"
	set newName to the text returned of result
	
	if exists (file newName of chosen_) then
		display dialog "A file with that name already exists, please try again."
	else
		exit repeat
	end if
end repeat

try
	do shell script "ditto " & quoted form of (POSIX path of originalFile) & "? " & (POSIX path of chosen_) & "/" & newName
end try

thanks digpen
acccording to your script i see that its exactly what i want.
but there is something wrong that make the script not workable
i tried to fixed that but i failed
can you please help me again.
its give me error after i name the file for the first time.

please give some suggestions

thanks once again for all you help

please help me to solve this issue
this script works fine and digpen helped me to write this.(thanks Digpen)
this script on run ask user to select the external drive and copy “01.pdf” to selected drive with their own newName. (works fine)

tell application "Finder" to set disks_ to name of disks whose startup is false 
set chosen_ to choose from list disks_ with prompt "Choose a volume to copy to." 
if chosen_ is false then return -- user canceled, quit script  
set originalFile to "Macintosh HD:Users:Shared:01.pdf" 
display dialog "What should the new file be called?" default answer "New_Name" 
set Newname to the text returned of result 
do shell script "ditto " & quoted form of (POSIX path of originalFile) & "� " & (POSIX path of chosen_) & "/" & Newname

but the only problem is that if “newName” already exist in the selected drive then it over write it and i don’t want this.
i want - if the "newName exist in the selected drive then new dialog box shows up that says-“file exist please type different name” and then the file is saved with different name.
here is what Digpen suggested me but i could not make it work

tell application "Finder" to set disks_ to name of disks whose startup is false 
set chosen_ to choose from list disks_ with prompt "Choose a volume to copy to." 
if chosen_ is false then return -- user canceled, quit script  
set originalFile to "Macintosh HD:Users:Shared:01.pdf" 

repeat
	
	display dialog "What should the new file be called?" default answer "New_Name"
	set newName to the text returned of result
	
	if exists (file newName of chosen_) then
		display dialog "A file with that name already exists, please try again."
	else
		exit repeat
	end if
end repeat

try
	do shell script "ditto " & quoted form of (POSIX path of originalFile) & "? " & (POSIX path of chosen_) & "/" & newName
end try

please help
thanks