Reputation: 315
I have to duplicate multiple files using an AppleScript. What this script should do is first, ask the user to select the folder which contains the files that have to be duplicated. Second, show a list of all of the files that there're in the folder that user have selected. In this step, the user can select multiple files. And the last step is duplicate the files. Here's t'he script I'm using:
--Get the folder
set theFolder to (choose folder with prompt "Select the folder that contains the files to copy. In the next step you'll be able to select the files to copy.") as text
--Get the path to de destination folder of the files
set destination_folder to (path to home folder) as text
--Generate the list of files inside theFolder
tell application "Finder"
set theItems to items of folder theFolder
set theNames to {}
repeat with anItem in theItems
set end of theNames to name of anItem
end repeat
end tell
-Let user select the files of the list
choose from list theNames with prompt "Select the files" OK button name "OK" cancel button name "Cancel" with multiple selections allowed
tell result
if it is false then error number -128 -- cancel
set theChoices to it
end tell
if (count of theChoices) is greater than or equal to 1 then
repeat with aChoice in theChoices
set thisItem to theFolder & aChoice
-- do something with thisItem
duplicate thisItem to destination_folder
end repeat
end if
The problem is that when the srcipt has to run the line "copy thisItem to destination_folder" it crashes. Here's the oputput that generates AppleScript Editor when I try to run:
tell application "AppleScript Editor"
choose from list {"Obres.xlsx", "Programa_sardanes", "Sardanes.xlsx"} with prompt "Escull els arxius o l'arxiu que vols afegir" OK button name "Acceptar" cancel button name "Cancelar" with multiple selections allowed
--> {"Sardanes.xlsx"}
-- 'core'\'clon'{ 'insh':'utxt'("Macintosh HD:Users:Joan:"), '----':'utxt'("Macintosh HD:Users:Joan:MEGA:Sardanes.xlsx"), &'subj':null(), &'csig':65536 }
--> error number -1700 from "Macintosh HD:Users:Joan:MEGA:Sardanes.xlsx" to reference
Result:
error "Can't generate \"Macintosh HD:Users:Joan:MEGA:Sardanes.xlsx\" on the type reference." number -1700 from "Macintosh HD:Users:Joan:MEGA:Sardanes.xlsx" to reference
I have been trying several hours to solve this problem but I don't know where is the error on the script. I hope someone could help me. And if someone knows a much more simple way to do this would be helpful also. Thank you!
Upvotes: 0
Views: 1496
Reputation: 1767
I made some small changes to your script and it should work now...
Basically, you were missing a couple small portions. The "duplicate" command is a function of the "Finder", so I added a "Tell application "Finder"" to the duplicate portion. You were also storing your path to your file and folder as text, I modified them to be referenced as "alias".
on run
--Get the folder
set theFolder to (choose folder with prompt "Select the folder that contains the files to copy. In the next step you'll be able to select the files to copy.") as text
--Get the path to de destination folder of the files
set destination_folder to (path to home folder)
--Generate the list of files inside theFolder
tell application "Finder"
set theItems to items of folder theFolder
set theNames to {}
repeat with anItem in theItems
set end of theNames to name of anItem
end repeat
end tell
--Let user select the files of the list
choose from list theNames with prompt "Select the files" OK button name "OK" cancel button name "Cancel" with multiple selections allowed
tell result
if it is false then error number -128 -- cancel
set theChoices to it
end tell
if (count of theChoices) is greater than or equal to 1 then
repeat with aChoice in theChoices
set thisItem to (theFolder & aChoice) as alias
-- do something with thisItem
tell application "Finder" to duplicate thisItem to destination_folder
end repeat
end if
end run
Upvotes: 1