Applescript 将文件名附加到文件夹名[不理解]

Applescript 将文件名附加到文件夹名[不理解],applescript,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:sc

我很难理解如何重命名(附加)文件夹中的文件名和文件夹名。 例如,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
谢谢你的帮助

我希望文件夹中的所有文件名都具有文件夹名称 在文件名的开头加上前缀。例如,大约.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

这里有问题吗?我不知道该说什么?标题没有暗示这一点吗?您编写的代码中有什么具体不起作用的地方?重新命名文件名。脚本的最后一部分。我希望文件夹中的所有文件名都在文件名开头加上文件夹名。例如,about.txt将成为folderName-about.txt,等等。您从文件中得到的错误或结果不是您想要的?它正在重命名文件,而不是在“John the Dog”前面加上结构选项“-MAIN”或“-OTHER”。是的,我从您的脚本中获取了该错误或结果,但不确定您的意图。我已经更新了答案,确实有效。非常感谢。唯一让我困惑的是路。我将如何指定HD/分区?而不是桌面。您可以像在脚本中那样设置路径。如果您想知道文件夹的HFS路径,请在AppleScriptedEditor中运行
选择文件夹
,查看结果。如果我将其设置为最初的设置方式:将文件夹设置为文件夹“OS X:Users:David:Desktop:SCRIPT:SCRIPT copy”我收到一个错误:预期的行尾,等等,但找到“”。