applescript:如何复制真实文件而不是别名,但是要递归

时间:2019-03-27 11:22:44

标签: macos applescript alias

我正在使用以下脚本从USB-Stick上的硬盘驱动器复制某些文件夹内容。由于文件夹内容可能会更改,因此我使用别名。

该脚本曾经可以完美地运行,但是由于我必须对文件夹结构进行更改,所以现在有时我会使用别名的别名(脚本的变通办法,将不再赘述)。

问题在于该脚本似乎只转换了别名文件夹的第一级,但是如果文件夹的别名中有文件的别名,它将复制文件别名。

我想知道是否有可能让脚本递归遍历文件夹的每个级别(即每个文件)并改为复制原始文件。

谢谢!

彼得

ps:源文件夹的示例性文件夹结构:

文件夹1的别名

          alias of folder 1-1

                   alias of file a, alias of file b

文件夹2的别名

         alias of file c

--> first a dialogue

display notification "hello, get folder ready"
delay 5 --> allow time for the notification to trigger

set theDialogText to "choose source"
display dialog theDialogText



--> Result: {button returned:"OK"}


--> now computing

set the backupSource to (choose folder) as alias

set theDialogText to "choose destination"


display dialog theDialogText

--> Result: {button returned:"OK"}


set the backupDestination to (choose folder) as alias

display notification "maybe this takes a while..."

delay 6 --> allow time for the notification to trigger

--> now copy

tell application "Finder"

    activate
    with timeout of 600 seconds --> equeals 10 Minutes

        set everyAlias to every item of folder backupSource
        repeat with thisAlias in everyAlias

            set originalFile to (original item of thisAlias)
            duplicate originalFile to backupDestination

        end repeat
    end timeout
end tell
``````````

I feel that the the line "repeat thisAlias in everyAlias" doesn´t do its job and goes only one level down, ie it converts the alias on the first level and not all other aliases within this alias-folder

2 个答案:

答案 0 :(得分:0)

我没有快速解决的方法,而是有关编程的建议:是时候添加调试了。在这种情况下,我怀疑您的脚本未在要使用的文件上运行。那是做什么呢?

例如,在开头附近,
set debugOn to true
或更好
property debugOn : true

然后在您的duplicate命令之前

if debugOn then display dialog ¬
     "Copying thisAlias: " & return & (thisAlias as text) & return & ¬
     "of originalFile: " & return & originalFile buttons "OK" default button 1

...等等。您可以通过对话框甚至不同的行为方式在整个代码中留下这些if debugOn then…检查,并在工作后将其更改为property debugOn : false。在需要编辑脚本的不可避免的日子里,拥有所有这些功能也很棒,但是它已经老化了,您已经忘记了所有细微之处。

答案 1 :(得分:0)

下面的AppleScript代码是否可以帮助您完成所需的工作?

tell application "Finder"
    set sourceFolder to choose folder with prompt "CHOOSE SOURCE"
    set destinationFolder to choose folder with prompt "CHOOSE DESTINATION"
    set nestedFolders to a reference to (entire contents of sourceFolder)
    set aliasFiles to a reference to every alias file of nestedFolders
    set aliasFiles to contents of aliasFiles
    repeat with i from 1 to count of aliasFiles
        set thisItem to item i of aliasFiles
        if not (exists of original item of thisItem) then
            delete thisItem
        end if
    end repeat
    set aliasFiles to a reference to every alias file of nestedFolders
    set originalFiles to (original item of aliasFiles) as alias list
    duplicate originalFiles to destinationFolder
end tell