我正在處理我的第一個AppleScript,並在最後一條命令中遇到錯誤。基本上,腳本應該循環訪問給定文件夾中的所有jpg(「/ Volumes/Public2/Pictures/LINP/temp /」),並根據文件名中的日期將它們全部移動到子文件夾中:AppleScript移動文件失敗。我做錯了什麼?
Ex。Ex。 000DC5D1A54E(天井)_1_20130604084734_139988.jpg
其中20130604是日期(2013年6月4日)
我的腳本是獲取每個文件名,並遍歷它們,創建必要的子文件夾中的一部分效果很好。沒有問題。這是當我嘗試實際的「移動」,我得到一個系統事件錯誤。因此,要啓動,以下是完整的腳本:
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
後鑽研蘋果論壇,堆棧溢出和諮詢谷歌甲骨文小時,我知道這方面的問題,所以我想作爲提供儘可能多的信息。這裏,我印刷(記錄)的內容,並類類型thisFile,targetFolder和finalTarget1的:
(targetFolder:/卷/ Public2 /圖片/ LINP /溫度/ 2013年6月4日)
(targetFolder類:文字)
thisFile
(*文件Public2:圖片:LINP:溫度:000DC5D1A54E(天井)_1_20130604084734_139988.jpg *)
(thisFile類:文件)
(finalTarget1:Public2:圖片:LINP:溫度:2013年6月4日:)
(finalTarget1類:文字)
而且這裏是兩個移動嘗試中的每一個顯示的錯誤:
將此文件移動到文件夾finalTarget1
=移動文件「Public2:圖片:LINP:temp:000DC5D1A54E(Patio)_1_20130604084734_139988.jpg」to folder「Public2:圖片:LINP:temp:2013-06-04:」
錯誤「系統事件出錯:無法獲取文件\「Public2:圖片:LINP:temp:000DC5D1A54E(Patio)_1_20130604084734_139988.jpg \」。 「數-1728從文件 」Public2:圖片:LINP:溫度:000DC5D1A54E(天井)_1_20130604084734_139988.jpg「
移動thisFile到文件夾targetFolder
=移動文件」 Public2:圖片:LINP:溫度:000DC5D1A54E (天井)_1_20130604084734_139988.jpg」文件夾 「/卷/ Public2 /圖片/ LINP /溫度/ 2013年6月4日」
錯誤 「系統事件得到了一個錯誤:無法獲取文件\」 Public2:圖片: LINP:溫度:000DC5D1A54E(天井)_1_20130604084734_139988.jpg \」。 「number -1728 from file」Public2:圖片:LINP:temp:000DC5D1A54E(Patio)_1_20130604084734_139988。jpg「
最後,是的,Public2被掛載,我知道它是可訪問的,因爲我得到完整的文件夾內容,並適當地創建每個子文件夾,這只是最後一個」移動「操作失敗我敢肯定,這是我的無知的產物。
誰能幫助查明原因,並如何解決呢?
CRGreen:謝謝你的建議。不幸的是,當我將該行更改爲「將此文件移動到finalTarget1」時,出現以下錯誤: 將文件「Public2:Pictures:LINP:temp:000DC5D1A54E(Patio)_1_20130604084734_139988.jpg」移動到「Public2:Pictures:LINP:temp: 2013-06-04:「 - > error number -1700 from」Public2:圖片:LINP:temp:2013-06-04:「到位置說明符 結果: 錯誤」系統事件出錯:無法制作\「Public2:圖片:LINP:temp:2013-06-04:\」轉換爲類型位置說明符。「從「Public2:圖片:LINP:temp:2013-06-04:」到位置說明符 – Techlifter
的編號-1700至於這兩個動作,我嘗試了兩個稍微不同的動作,看看是否可以工作。我在我的文章中提供了每次嘗試的輸出,希望得到的錯誤消息有助於確定問題。 – Techlifter
Hi @ user3171341。我沒有看到那個錯誤。當我將第一行修改爲「將thisFile移動到finalTarget1」的腳本運行時,它可以正常工作。你使用哪個版本的操作系統?保持第二行註釋掉,但看看這是否適用於第一個移動行:'將thisFile移動到(我的finalTarget1作爲別名)'如果這個工作,我會編輯我的答案。 – CRGreen