简体   繁体   English

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

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

I have put together this applescript that when a file is added to an action folder, it makes a new directory based on the first portion of the file name, makes a directory called 'images' inside, and then Moves that file to the 'images' folder inside.我已经把这个 applescript 放在一起,当一个文件被添加到一个动作文件夹时,它会根据文件名的第一部分创建一个新目录,在里面创建一个名为“images”的目录,然后将该文件移动到“images” '里面的文件夹。 This all works GREAT, UNTIL.这一切都很好,直到。 .dun-dun-dun.. The directory exists already., (ohno_). .dun-dun-dun.. 目录已经存在., (ohno_). So I tried adding some If statements and it still is running the rest of the script fine (which adds in some other files and moves the completed folder set to another folder on my hard drive).所以我尝试添加一些 If 语句,它仍然运行脚本的 rest 很好(它添加了一些其他文件并将完成的文件夹集移动到我硬盘上的另一个文件夹)。 but I can not get the file with the same first portion of the name to Move or copy over,, I'm reading a lot of posts here on If file exists, finding few on if Folder exists.但我无法获取名称的第一部分相同的文件来移动或复制,我在这里阅读了很多关于如果文件存在的帖子,发现很少有关于文件夹是否存在的帖子。 and trying to figure it out.并试图弄清楚。 but can not get this to GO:.但不能得到这个 GO:。 This is where I am at so far:这是我目前所处的位置:

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

I find other people's folder actions a horror show to troubleshoot so for my own testing, I simply set input to an alias (and removed the run bookends).我发现其他人的文件夹操作是一个需要排除故障的恐怖表演,因此对于我自己的测试,我只需将输入设置为别名(并删除运行的书挡)。 Presumably it won't be an issue here but as I have no idea whether it runs like this, I leave the on run as I found it.大概这不会是一个问题,但由于我不知道它是否像这样运行,所以我让我找到它时继续运行

Essentially, I put your each of the make new folder commands inside its own try block.本质上,我将您的每个make new folder命令都放在了它自己的 try 块中。 Hopefully that will resolve your issue.希望这能解决您的问题。

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

Minor edits: In the top section, I simply added a reset to the delimiters as I used an as text which required they be defaults.次要编辑:在顶部,我只是添加了对分隔符的重置,因为我使用了要求它们为默认值的as text It is a considered a good practice to reset them to something (defaults or previous) so you might consider that.将它们重置为某些东西(默认值或以前的值)被认为是一种很好的做法,因此您可能会考虑这样做。

The command to copy a file in applescript is actually duplicate .在 applescript 中复制文件的命令实际上是duplicate的。

I used a variable to streamline your paths but it should work with your style as well.我使用了一个变量来简化你的路径,但它也应该适用于你的风格。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM