【问题标题】:Searching for file in folder and placing it in another folder在文件夹中搜索文件并将其放置在另一个文件夹中
【发布时间】:2013-10-13 18:00:11
【问题描述】:

两天前我学到了一点关于applescripting的知识——这是我第一次“编程”,所以你可能会看到一些有趣的东西:

    tell application "Finder"
--I don't really know why I use as string but otherwise it won't work
set strFilepath to selection as string
set mainFolder to strFilepath
if (exists strFilepath & "ro") is false then
    make new folder at mainFolder with properties {name:"ro"}
    make new folder at mainFolder & "ro" with properties {name:"carta"}
    make new folder at mainFolder & "ro" with properties {name:"A4"}
end if
if (exists strFilepath & "ingl") is false then
    make new folder at mainFolder with properties {name:"ingl"}
    make new folder at mainFolder & "ingl" with properties {name:"carta"}
    make new folder at mainFolder & "ingl" with properties {name:"A4"}
end if
if (exists strFilepath & "es") is false then
    make new folder at mainFolder with properties {name:"es"}
    make new folder at mainFolder & "es" with properties {name:"carta"}
    make new folder at mainFolder & "es" with properties {name:"A4"}
end if
if (exists strFilepath & "it") is false then
    make new folder at mainFolder with properties {name:"it"}
    make new folder at mainFolder & "it" with properties {name:"carta"}
    make new folder at mainFolder & "it" with properties {name:"A4"}
end if
if (exists strFilepath & "fr") is false then
    make new folder at mainFolder with properties {name:"fr"}
    make new folder at mainFolder & "fr" with properties {name:"carta"}
    make new folder at mainFolder & "fr" with properties {name:"A4"}
end if
if (exists strFilepath & "port") is false then
    make new folder at mainFolder with properties {name:"port"}
    make new folder at mainFolder & "port" with properties {name:"carta"}
    make new folder at mainFolder & "port" with properties {name:"A4"}
end if
--it does what I want up until here
set allFiles to entire contents of mainFolder
--here I get an error nummber -1728, I guess because of that string... 
try
    --I guess this code also doesn't work… I have PDF files that end like xxx_it_carta.pdf, xxx_it_A4.pdf, xxx_en_carta.pdf, etc
    search of (name of every item of allFiles) for "_it_carta"
    if file exists then move to mainFolder & "it" & "carta"
end try
search of (name of every item of allFiles) for "_it_A4"
try
    if file exists then move to mainFolder & "it" & "A4"
end try
--and so on
    end tell

请帮我安排这段代码,这样我就可以让它工作,我会更多地了解applescript。

谢谢!

【问题讨论】:

    标签: string sorting search applescript


    【解决方案1】:

    你可以试试这样的:

        set folderNameList to {"ro", "ingl", "es", "it", "fr", "port"}
    
    tell application "Finder"
        set strFilepath to (get selection) as text
        repeat with folderName in folderNameList
            if not (exists folder (strFilepath & folderName)) then make new folder at strFilepath with properties {name:folderName}
        end repeat
    
        set allFiles to entire contents of folder strFilepath
        -- not clear on your move command
        move (files of (entire contents of folder strFilepath) whose name ends with "_it_carta") to folder (strFilepath & "it")
    end tell
    

    编辑

    set folderNameList to {"ro", "ingl", "es", "it", "fr", "port"}
    
    tell application "Finder"
        set strFilepath to (get selection) as text
        repeat with folderName in folderNameList
            set newFolder to POSIX path of (strFilepath & folderName) & "/" 
            do shell script "mkdir -p " & (quoted form of newFolder) & "{\"A4\",\"carta\"}"
        end repeat
    
        set allFiles to entire contents of folder strFilepath
        -- not clear on your move command
        move (files of (entire contents of folder strFilepath) whose name ends with "_it_carta") to folder (strFilepath & "it")
    end tell
    

    【讨论】:

    • 酷!这看起来好多了(而且更短了)......谢谢!关于搜索部分的任何想法?
    • 最后一行搜索以“_it_carta”结尾的文件并将它们移动到“it”文件夹
    • 太棒了!它有效...您能否调整代码,使列表中的每个文件夹也有两个子文件夹{“A4”,“carta”}。我自己试过了,只在最后一个文件夹(端口)中它起作用了——其余的都是空的......非常感谢!
    猜你喜欢
    • 2012-06-19
    • 1970-01-01
    • 1970-01-01
    • 2021-02-21
    • 2014-05-20
    • 1970-01-01
    • 2021-05-01
    • 2020-02-10
    • 2021-04-07
    相关资源
    最近更新 更多