AppleScript 照片文件夹引用在子例程中不起作用 - 为什么?



我是AppleScript的初学者。我想做的是让苹果的照片应用在现有文件夹中创建一个新文件夹。最终,我希望递归地克隆文件夹结构,所以我需要能够将父文件夹传递给子例程,并让子例程创建文件夹。

如果我在相同的上下文中都这样做,我可以在父文件夹中创建子文件夹,但如果我在子例程中做完全相同的事情,传递对父文件夹的引用,它会失败,并出现以下错误:

error "Can’t make folder id "1632F3F7-D00F-42DB-B9B3-F781D5DDAFD8/L0/020" of application "Photos" into the expected type." number -1700 from folder id "1632F3F7-D00F-42DB-B9B3-F781D5DDAFD8/L0/020"

我认为这与theParentFolder通过引用而不是通过值传递给子例程的事实有关,但我尝试了一些不成功的尝试来解决这个问题并失败了。

on createFolderInSubRoutine(parentFolder)
if not (folder named "bar" exists) then make new folder named "bar" at parentFolder -- fails
end createFolderInSubRoutine
tell application "Photos"
set theParentFolder to folder "My Parent Folder"
if not (folder named "foo" exists) then make new folder named "foo" at theParentFolder -- works
my createFolderInSubRoutine(theParentFolder)
return
end tell

找到一个可行的解决方案:

on createFolderInSubRoutine(parentFolder)
tell parentFolder
if not (folder named "bar" exists) then make new folder named "bar" at parentFolder
end tell
end createFolderInSubRoutine
tell application "Photos"
set theParentFolder to folder "My Parent Folder"
my createFolderInSubRoutine(theParentFolder)
return
end tell

我不完全理解为什么可以工作,但我很高兴知道它可以工作。