使用AppleScript复制多个文件

时间:2014-08-07 16:37:01

标签: applescript

我必须使用AppleScript复制多个文件。该脚本应该首先做的是,要求用户选择包含必须复制的文件的文件夹。其次,显示用户已选择的文件夹中的所有文件的列表。在此步骤中,用户可以选择多个文件。最后一步是复制文件。以下是我使用的脚本:

--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

问题在于,当srcipt必须运行该行时,将thisItem复制到destination_folder"它崩溃了。这是我尝试运行时生成AppleScript编辑器的oputput:

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

我一直在尝试几个小时来解决这个问题,但我不知道脚本上的错误在哪里。我希望有人可以帮助我。如果有人知道一个更简单的方法,这也会有所帮助。谢谢!

1 个答案:

答案 0 :(得分:1)

我对你的脚本进行了一些小改动,现在应该可以正常工作了......

基本上,你错过了一小部分。 "重复"命令是" Finder"的功能,所以我添加了一个" Tell应用程序" Finder""复制部分。您还将文件和文件夹的路径存储为文本,我将其修改为"别名"。

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