[英]Applescript to move files to Newly created directory - but what if it exists?
我已經把這個 applescript 放在一起,當一個文件被添加到一個動作文件夾時,它會根據文件名的第一部分創建一個新目錄,在里面創建一個名為“images”的目錄,然后將該文件移動到“images” '里面的文件夾。 這一切都很好,直到。 .dun-dun-dun.. 目錄已經存在., (ohno_). 所以我嘗試添加一些 If 語句,它仍然運行腳本的 rest 很好(它添加了一些其他文件並將完成的文件夾集移動到我硬盤上的另一個文件夾)。 但我無法獲取名稱的第一部分相同的文件來移動或復制,我在這里閱讀了很多關於如果文件存在的帖子,發現很少有關於文件夾是否存在的帖子。 並試圖弄清楚。 但不能得到這個 GO:。 這是我目前所處的位置:
on run {input, parameters}
tell application "Finder"
set file_image to name of file input
end tell
tell application "Finder"
set fileName to name of file input
set AppleScript's text item delimiters to "."
if number of text items of fileName > 1 then
set fileName to text items 1 thru -2 of fileName as text
end if
set AppleScript's text item delimiters to "-"
if number of text items of fileName > 1 then
set fileName to text items 1 thru -2 of fileName as text
end if
fileName
end tell
tell application "Finder"
activate
set target of Finder window 1 to folder "MAKER" of folder "Desktop" of folder "nathan" of folder "Users" of startup disk
set thePath to folder fileName of folder "MAKER" of folder "Desktop" of folder "nathan" of folder "Users" of startup disk
if exists folder thePath then
set source_folder to folder "new-actiontester" of folder "Desktop" of folder "nathan" of folder "Users" of startup disk
set source_files to every file in source_folder
set target_folder to folder "images" of folder fileName of folder "MAKER" of folder "Desktop" of folder "nathan" of folder "Users" of startup disk
repeat with i from 1 to number of items in source_files
set source_file to (item i of source_files)
move source_file to (target_folder) -- use "copy source_file to folder (target_folder as alias)" to copy the files
end repeat
end if
make new folder at folder "MAKER" of folder "Desktop" of folder "nathan" of folder "Users" of startup disk with properties {name:fileName}
set target of Finder window 1 to folder fileName of folder "MAKER" of folder "Desktop" of folder "nathan" of folder "Users" of startup disk
make new folder at folder fileName of folder "MAKER" of folder "Desktop" of folder "nathan" of folder "Users" of startup disk with properties {name:"images"}
set source_folder to folder "new-actiontester" of folder "Desktop" of folder "nathan" of folder "Users" of startup disk
set source_folder_list to folder "img-list" of folder "Desktop" of folder "nathan" of folder "Users" of startup disk
set source_files to every file in source_folder
set source_files_list to every file in source_folder_list
set target_folder to folder "images" of folder fileName of folder "MAKER" of folder "Desktop" of folder "nathan" of folder "Users" of startup disk
set target_Go to folder "GO" of folder "Desktop" of folder "nathan" of folder "Users" of startup disk
set folder_Number to folder fileName of folder "MAKER" of folder "Desktop" of folder "nathan" of folder "Users" of startup disk
set target_folder_list to folder fileName of folder "MAKER" of folder "Desktop" of folder "nathan" of folder "Users" of startup disk
repeat with i from 1 to number of items in source_files
set source_file to (item i of source_files)
move source_file to (target_folder) -- use "copy source_file to folder (target_folder as alias)" to copy the files
end repeat
repeat with i from 1 to number of items in source_files_list
set source_file_list to (item i of source_files_list)
copy source_file_list to folder (target_folder_list as alias) -- use "copy source_file to folder (target_folder_list as alias)" to copy the files
end repeat
copy folder_Number to folder (target_Go as alias) -- use "copy source_file to folder (target_folder_list as alias)" to copy the files
end tell
我發現其他人的文件夾操作是一個需要排除故障的恐怖表演,因此對於我自己的測試,我只需將輸入設置為別名(並刪除運行的書擋)。 大概這不會是一個問題,但由於我不知道它是否像這樣運行,所以我讓我找到它時繼續運行。
本質上,我將您的每個make new folder
命令都放在了它自己的 try 塊中。 希望這能解決您的問題。
on run {input, parameters}
tell application "Finder"
set fileName to name of file input
set AppleScript's text item delimiters to "."
if number of text items of fileName > 1 then
set fileName to text items 1 thru -2 of fileName as text
end if
set AppleScript's text item delimiters to "-"
if number of text items of fileName > 1 then
set fileName to text items 1 thru -2 of fileName as text
end if
set AppleScript's text item delimiters to ""
end tell
tell application "Finder"
set mokr to (path to desktop) & "MAKER:" as text
set target of Finder window 1 to mokr
try
make new folder at mokr with properties {name:fileName} with name
end try
set thePath to mokr & fileName as alias
try
make new folder at thePath with properties {name:"images"} with name
end try
if exists folder thePath then
set source_folder to folder "new-actiontester" of desktop
set source_files to every file in source_folder
set target_folder to folder "images" of thePath
repeat with source_file in source_files
duplicate source_file to target_folder
end repeat
end if
end tell
end run
次要編輯:在頂部,我只是添加了對分隔符的重置,因為我使用了要求它們為默認值的as text
。 將它們重置為某些東西(默認值或以前的值)被認為是一種很好的做法,因此您可能會考慮這樣做。
在 applescript 中復制文件的命令實際上是duplicate
的。
我使用了一個變量來簡化你的路徑,但它也應該適用於你的風格。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.