使用文件夹名称附加文件名[不明白]

时间:2014-07-01 15:08:48

标签: applescript

我很难理解如何使用文件夹名称重命名(追加)文件夹内的文件名。 例如,about.txt,picture.jpg等成为folderName-about.txt,folderName-picture等 我是AppleScript的完整菜鸟,这是我的第一个剧本!

on run {input, parameters}

tell application "Finder"
set theFolder to folder "OS X:Users:David:Desktop:SCRIPT:script-copy"
set targetFolder to folder "OS X:Users:David:Desktop:SCRIPT:script-copy-finished"

display dialog "Which structure do you wish to duplicate?" buttons {"Structure-MAIN", "Structure-OTHER"}
set chosenStructure to button returned of result

if contents of chosenStructure is equal to "Structure-MAIN" then
    set chosenStructure to "OS X:Users:David:Desktop:SCRIPT:script-copy:-MAIN"
else
    set chosenStructure to "OS X:Users:David:Desktop:SCRIPT:script-copy:-OTHER"
end if

display dialog "Specify a new folder name:" default answer "John The Dog"
set newName to (text returned of result)
set createNewStructure to make new folder at targetFolder with properties {name:newName}
duplicate every file of entire contents of folder chosenStructure to createNewStructure






set the_folder to name of folder chosenStructure
repeat with this_file in (get files of entire contents of folder chosenStructure)

    set the_start to offset of "_" in ((name of this_file) as string)
    set the_stop to count (name of this_file as string)
    set name of this_file to (the_folder & (items the_start thru the_stop of (name of this_file as string)))
end repeat

end tell


return input
end run

感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

  

我希望文件夹中的所有文件名都有文件夹名称   前缀在文件名的开头。例如,about.txt成为   folderName-about.txt等。

试试这个:

tell application "Finder"
    ...
    set the_folder to name of createNewStructure
    repeat with this_file in (get files of entire contents of createNewStructure)
        set name of this_file to the_folder & "-" & (name of this_file)
    end repeat
end tell

整个剧本:

set theFolder to ((path to desktop) as text) & "SCRIPT:script-copy"
set targetFolder to ((path to desktop) as text) & "SCRIPT:script-copy-finished"

display dialog "Which structure do you wish to duplicate?" buttons {"-MAIN", "-OTHER"}
set chosenStructure to ((path to desktop) as text) & "SCRIPT:script-copy:" & button returned of result

display dialog "Specify a new folder name:" default answer "John The Dog"
set newName to (text returned of result)

tell application "Finder"
    set createNewStructure to make new folder at targetFolder with properties {name:newName}
    duplicate every file of entire contents of folder chosenStructure to createNewStructure

    set the_folder to name of createNewStructure
    repeat with this_file in (get files of entire contents of createNewStructure)
        set name of this_file to the_folder & "-" & (name of this_file)
    end repeat
end tell