Applescript 将文件移动到新创建的目录 - 但如果它存在怎么办?

Applescript to move files to Newly created directory - but what if it exists?

我把这个applescript放在一起,当一个文件被添加到一个动作文件夹时,它会根据文件名的第一部分创建一个新目录,在里面创建一个名为'images'的目录,然后将该文件移动到里面的 'images' 文件夹中。这一切都很好,直到。 .dun-dun-dun..目录已经存在!! (不好了_) 。所以我尝试添加一些 If 语句,它仍然是 运行 脚本的其余部分(它添加了一些其他文件并将完成的文件夹集移动到我硬盘上的另一个文件夹),但我无法获得具有相同名称第一部分的文件以移动或复制。我在这里阅读了很多关于如果文件存在的帖子,如果文件夹存在则发现很少,并试图弄清楚,但无法得到这个走.. 这是我目前所在的位置:

on run {input, parameters}
    tell application "Finder"
        set file_image to name of file input
    end tell
    tell application "Finder"
        set fileName to name of file input
        set AppleScript's text item delimiters to "."
        if number of text items of fileName > 1 then
            set fileName to text items 1 thru -2 of fileName as text
        end if
        set AppleScript's text item delimiters to "-"
        if number of text items of fileName > 1 then
            set fileName to text items 1 thru -2 of fileName as text
        end if
        fileName
    end tell
    
    tell application "Finder"
        activate
        set target of Finder window 1 to folder "MAKER" of folder "Desktop" of folder "nathan" of folder "Users" of startup disk
        set thePath to folder fileName of folder "MAKER" of folder "Desktop" of folder "nathan" of folder "Users" of startup disk
        if exists folder thePath then
            set source_folder to folder "new-actiontester" of folder "Desktop" of folder "nathan" of folder "Users" of startup disk
            set source_files to every file in source_folder
            set target_folder to folder "images" of folder fileName of folder "MAKER" of folder "Desktop" of folder "nathan" of folder "Users" of startup disk
            repeat with i from 1 to number of items in source_files
                set source_file to (item i of source_files)
                move source_file to (target_folder) -- use "copy source_file to folder (target_folder as alias)" to copy the files
            end repeat
        end if
        make new folder at folder "MAKER" of folder "Desktop" of folder "nathan" of folder "Users" of startup disk with properties {name:fileName}
        set target of Finder window 1 to folder fileName of folder "MAKER" of folder "Desktop" of folder "nathan" of folder "Users" of startup disk
        make new folder at folder fileName of folder "MAKER" of folder "Desktop" of folder "nathan" of folder "Users" of startup disk with properties {name:"images"}
        set source_folder to folder "new-actiontester" of folder "Desktop" of folder "nathan" of folder "Users" of startup disk
        set source_folder_list to folder "img-list" of folder "Desktop" of folder "nathan" of folder "Users" of startup disk
        set source_files to every file in source_folder
        set source_files_list to every file in source_folder_list
        set target_folder to folder "images" of folder fileName of folder "MAKER" of folder "Desktop" of folder "nathan" of folder "Users" of startup disk
        set target_Go to folder "GO" of folder "Desktop" of folder "nathan" of folder "Users" of startup disk
        set folder_Number to folder fileName of folder "MAKER" of folder "Desktop" of folder "nathan" of folder "Users" of startup disk
        set target_folder_list to folder fileName of folder "MAKER" of folder "Desktop" of folder "nathan" of folder "Users" of startup disk
        repeat with i from 1 to number of items in source_files
            set source_file to (item i of source_files)
            move source_file to (target_folder) -- use "copy source_file to folder (target_folder as alias)" to copy the files
        end repeat
        repeat with i from 1 to number of items in source_files_list
            set source_file_list to (item i of source_files_list)
            copy source_file_list to folder (target_folder_list as alias) -- use "copy source_file to folder (target_folder_list as alias)" to copy the files
        end repeat
        copy folder_Number to folder (target_Go as alias) -- use "copy source_file to folder (target_folder_list as alias)" to copy the files
    end tell

我发现其他人的文件夹操作很恐怖,所以为了我自己的测试,我只是将 input 设置为别名(并删除了 运行 书挡) .大概这不会成为问题,但由于我不知道它是否 运行 是这样的,所以我将 保留在 运行 上,因为我发现了它。

基本上,我将您的每个 make new folder 命令都放在了自己的 try 块中。希望这会解决您的问题。

on run {input, parameters}
tell application "Finder"
    set fileName to name of file input
    set AppleScript's text item delimiters to "."
    if number of text items of fileName > 1 then
        set fileName to text items 1 thru -2 of fileName as text
    end if
    set AppleScript's text item delimiters to "-"
    if number of text items of fileName > 1 then
        set fileName to text items 1 thru -2 of fileName as text
    end if
    set AppleScript's text item delimiters to ""
end tell
tell application "Finder"
    set mokr to (path to desktop) & "MAKER:" as text
    set target of Finder window 1 to mokr
    
    try
        make new folder at mokr with properties {name:fileName} with name
    end try
    
    set thePath to mokr & fileName as alias
    try
        make new folder at thePath with properties {name:"images"} with name
    end try
    
    if exists folder thePath then
        set source_folder to folder "new-actiontester" of desktop
        set source_files to every file in source_folder
        set target_folder to folder "images" of thePath
        
    repeat with source_file in source_files
        duplicate source_file to target_folder
    end repeat

    end if
end tell
end run

少量编辑: 在顶部部分,我只是在分隔符中添加了一个重置​​,因为我使用了 as text 要求它们是默认值。将它们重置为某些内容(默认值或以前的值)被认为是一种很好的做法,因此您可以考虑这样做。

applescript中复制文件的命令实际上是duplicate.

我使用了一个变量来简化您的路径,但它也应该适合您的风格。