【发布时间】:2014-01-08 01:26:02
【问题描述】:
我正在编写我的第一个 AppleScript,但在最后一个命令中遇到了错误。基本上,脚本应该遍历给定文件夹(“/Volumes/Public2/Pictures/LINP/temp/”)中的所有 jpg,并根据文件名中的日期将它们全部移动到子文件夹中:
例如。 000DC5D1A54E(天井)_1_20130604084734_139988.jpg
其中 20130604 是日期 (2013-06-04)
我的脚本部分获取每个文件名并循环它们,创建必要的子文件夹效果很好。完全没有问题。当我尝试实际的“移动”时,我得到了系统事件错误。因此,首先,这是完整的脚本:
set workFolder to "/Volumes/Public2/Pictures/LINP/temp/"
log workFolder
set workFolder to (POSIX file workFolder) as alias
log workFolder
tell application "System Events"
--get files to work on
set filesToProcess to files of workFolder
--log filesToProcess
repeat with thisFile in filesToProcess
log thisFile
set {fileName, fileExt} to {name, name extension} of thisFile
log fileName
log fileExt
if (fileExt is in "(*jpg*)" and ((length of fileName) is greater than 10)) then
--get name of file without extension
set rootName to text 1 thru -((length of fileExt) + 2) of fileName
log rootName
--break apart the file name to get to the imortant stuff
set AppleScript's text item delimiters to "_"
--store the camera name
set cameraName to text item 1 of rootName
log cameraName
--store the full date
set photoDate to text item 3 of rootName
log photoDate
--cut out the time information
set photoDate to text 1 thru 8 of photoDate
log photoDate
--break out the year
set photoYear to text 1 thru 4 of photoDate
log photoYear
--break out the month
set photoMonth to text 5 thru 6 of photoDate
log photoMonth
--break out the day
set photoDay to text 7 thru 8 of photoDate
log photoDay
--combine parts into a new name of the form "YYYY-MM-DD"
set photoFolder to photoYear & "-" & photoMonth & "-" & photoDay
log photoFolder
--make sure a correctly named folder exists
set targetFolder to my checkForFolder({parentFolder:(workFolder as text), folderName:photoFolder}) as text
log "targetFolder: " & targetFolder
log thisFile
log "targetFolder Class: " & class of targetFolder
log "thisFile Class: " & class of thisFile
set finalTarget1 to (POSIX file targetFolder) as alias as string
log "finalTarget1: " & finalTarget1
log "finalTarget1 Class: " & class of finalTarget1
--Here's where the error pops up. I've tried both HFS and POSIX formats for the target folder, but the issue seems to be with the file
--Yes, the files definitely exist (I've confirmed this). I show exactly what the values are and error messages I'm getting at the end of this post
move thisFile to the folder finalTarget1
move thisFile to the folder targetFolder
end if
end repeat
end tell
to checkForFolder({parentFolder:fParent, folderName:fName})
--find or create a folder
tell application "System Events"
--set fName to POSIX file of fName as alias
--set fParent to POSIX file of fParent as alias
if not (exists POSIX path of (folder fName of folder fParent)) then
set output to POSIX path of (make new folder at end of folder fParent with properties {name:fName})
else
set output to (POSIX path of (folder fName of folder fParent))
end if
end tell
--returns a POSIX path
return output
end checkForFolder
在对 Apple 论坛、Stack Overflow 和 Google 预言机进行了数小时的研究后,我意识到上下文很重要,因此我试图提供尽可能多的信息。在这里,我打印(记录)了 thisFile、targetFolder 和 finalTarget1 的内容和 Class 类型:
(目标文件夹:/Volumes/Public2/Pictures/LINP/temp/2013-06-04)
(targetFolder 类:文本)
这个文件
(*file Public2:Pictures:LINP:temp:000DC5D1A54E(Patio)_1_20130604084734_139988.jpg*)
(thisFile 类:文件)
(finalTarget1: Public2:Pictures:LINP:temp:2013-06-04:)
(finalTarget1 类:文本)
以下是两次移动尝试中的每一个都显示的错误:
将此文件移动到文件夹 finalTarget1
= 将文件“Public2:Pictures:LINP:temp:000DC5D1A54E(Patio)_1_20130604084734_139988.jpg”移动到文件夹“Public2:Pictures:LINP:temp:2013-06-04:”
错误“系统事件出错:无法获取文件\”Public2:Pictures:LINP:temp:000DC5D1A54E(Patio)_1_20130604084734_139988.jpg\”。”编号-1728来自文件“Public2:Pictures:LINP:temp :000DC5D1A54E(天井)_1_20130604084734_139988.jpg"
将此文件移动到文件夹 targetFolder
= 将文件“Public2:Pictures:LINP:temp:000DC5D1A54E(Patio)_1_20130604084734_139988.jpg”移动到文件夹“/Volumes/Public2/Pictures/LINP/temp/2013-06-04”
错误“系统事件出错:无法获取文件\”Public2:Pictures:LINP:temp:000DC5D1A54E(Patio)_1_20130604084734_139988.jpg\”。”编号-1728来自文件“Public2:Pictures:LINP:temp :000DC5D1A54E(天井)_1_20130604084734_139988.jpg"
最后,是的,Public2 已安装,我知道它是可访问的,因为我正在取回完整的文件夹内容并适当地创建每个子文件夹。只是最后一次“移动”操作失败了,我敢肯定这是我无知的产物。
谁能帮助确定原因以及我该如何解决?
【问题讨论】:
标签: file applescript move