用于从应用程序导出图像"Photos"苹果脚本



我在照片应用程序中有相册和嵌套的相册和文件夹。我希望我的苹果脚本导出图像,保持我在应用程序中拥有的相册或文件夹结构。

我尝试了在线可用的脚本:

tell application "Finder"
set location_1 to (choose folder with prompt "Choose a folder to export into") as text
end tell

tell application "Photos"
set x to name of every folder
choose from list x with prompt "Choose a project to export"
set theP to item 1 of result
tell application "Finder" to make new folder at alias location_1 with properties {name:theP}
tell folder theP
set initflist to every folder
set initalist to every album
if initflist is equal to {} then

log "process albums"
processAlbums(initalist, location_1 & theP) of me
else
if initalist is not equal to {} then
log "process albums"
processAlbums(initalist, location_1 & theP) of me
end if
log "process sub folders "
processSfolders(initflist, (location_1 & theP)) of me
end if
end tell
end tell

on processAlbums(alist, apath)
tell application "Photos"
repeat with a in alist
tell a
set theimages to get media items of album a
set thename to name of a
tell application "Finder"
if not (exists folder thename in alias apath) then
make new folder at alias apath with properties {name:thename}
end if
set destination to apath & ":" & thename & ":"
end tell
with timeout of 6000 seconds
tell a
set settings to "JPEG - Original Size"
export theimages to alias destination
end tell
end timeout
end tell
end repeat
end tell
end processAlbums

on processSfolders(flist, fpath)
tell application "Photos"
repeat with a in flist
try
set thename to name of a
tell application "Finder"
if not (exists folder thename in alias fpath) then
make new folder at alias fpath with properties {name:thename}
end if
end tell
tell a
set sAlist to every album
set sflist to every folder
if sflist is equal to {} then
processAlbums(sAlist, fpath & ":" & thename) of me
else
if sAlist is not equal to {} then
processAlbums(sAlist, fpath & ":" & thename) of me
end if
processSfolders(sflist, fpath & ":" & thename) of me
end if
end tell
on error errMsg
log "error"
end try
end repeat

end tell
end processSfolders

问题是它只获取子相册的名称,而不是顶级相册的名称。我必须维护相册或文件夹的整个结构。

我不知道AppleScript,我尝试调整这个,但到目前为止没有运气。 请问我可以得到一个方向吗?

您可以获取文件夹的名称以及文件夹中包含的相册,这将使您能够创建顶级目录和子目录。影集只能包含媒体项,而不能包含影集。顶级相册被分类为文件夹或容器。在 Applescript 照片词典中,它给出了这些项目的定义。

tell application "Finder"
set location_1 to (choose folder with prompt "Choose a folder to export into") as text
end tell
tell application "Photos"
activate
set fds to folders
repeat with fd in fds
set fdName to name of fd
set abNames to every album of fd
if parent of fd is missing value then
my createFolders(fdName, abNames, location_1, fd)
end if
end repeat
end tell
on createFolders(fName, aAlbums, fPath, fd)
tell application "Finder"
if not (exists folder fName in alias fPath) then
make new folder with properties {name:fName} at fPath
end if
repeat with a in aAlbums
set aName to name of a
set aPath to ((fPath as alias) as text) & fName
if not (exists folder aName in alias aPath) then
make new folder with properties {name:aName} at aPath
end if
set exPath to ((aPath as alias) as text) & aName
my exportImages(a, exPath)
end repeat
end tell
tell application "Photos"
set rcFolders to every folder of fd
repeat with rcFd in rcFolders
set rcAlbums to every album of rcFd
set rcName to name of rcFd
set rcPath to ((fPath as alias) as text) & fName
my createFolders(rcName, rcAlbums, rcPath, rcFd)
end repeat
end tell
end createFolders
on exportImages(photoAlbum, destination)
tell application "Photos"
set theimages to get media items of photoAlbum
with timeout of 6000 seconds
tell photoAlbum
set settings to "JPEG - Original Size"
export theimages to alias destination
end tell
end timeout
end tell
end exportImages

编辑 - 错误处理

要处理错误,请找到导致错误的命令并将其包装在 try 块中。解决方案可能是退出应用程序,以便该过程结束,并可能添加短暂的延迟,然后继续使用脚本。

try
export theimages to alias destination
on error
-- statements to execute in case of error
error "The exporting of images failed to complete"
quit
end try

来自开发人员参考 - 当命令无法在分配的时间内完成时(无论是默认值为两分钟,还是由 with timeout 语句设置的时间(,AppleScript 将停止运行脚本并返回错误"事件超时"。AppleScript 不会取消操作,它只是停止执行脚本。如果希望脚本继续,可以将语句包装在 try 语句中。但是,脚本是否可以在超时后发送命令以取消有问题的冗长操作取决于执行该命令的应用程序。有关 try 语句的更多信息。

最新更新