如何使用 AppleScript (Big Sur) 将文件从最近修改的文件夹向上移动一级
how to move files from most recently modified folder one level up using AppleScript (Big Sur)
我不是脚本和编程的新手(或专家),但几个小时后,我对如何复制文件感到困惑(在谷歌搜索并尝试了几个小时的各种事情之后)在这种情况下使用 AppleScript。
我想将文件从最近修改的文件夹上移一层。我正在尝试移动文件:
/Users/tsg/my 下载 folder/newest folder/pdfs/target1.pdf
至
/Users/tsg/my 下载 folder/newest folder/target1.pdf
其中“最新文件夹”不是固定名称,但会随着我下载较新的文件夹而改变。
当我运行下面的代码时,
tell application "Finder"
set downloadFolder to POSIX file "/Users/tsg/my downloads folder" as alias
set latestModifiedFolder to (downloadFolder & (name of last item of (sort every folder of downloadFolder by modification date)))
set targetFolder to latestModifiedFolder (* Is this necessary? *)
set pdfFolder to (targetFolder & ":pdfs:")
set pdfFile to (pdfFolder & ("target1.pdf"))
move pdfFile to targetFolder
end tell
我收到错误消息
"Finder got an error: Can’t make {alias "Macintosh HD:Users:tsg:my downloads folder:", "newest folder"} into type folder." number -1700 from {alias "Macintosh HD:Users:tsg:my downloads folder:", "newest folder"} to folder
如果我在“end tell”之前的最后一行添加修饰符 folder: "move pdfFile to folder targetFolder",我得到
"Finder got an error: Can’t make {alias "Macintosh HD:Users:tsg:my downloads folder:", "newest folder"} into type integer." number -1700 from {alias "Macintosh HD:Users:tsg:my downloads folder:", "newest folder"} to integer"
如果我添加修饰符 file “将 file pdfFile 移动到 folder targetFolder”,我得到
"Finder got an error: Can’t make {alias "Macintosh HD:Users:tsg:my downloads folder:", "newest folder", ":pdfs:", "target1.pdf"} into type integer." number -1700 from {alias "Macintosh HD:Users:tsg:my downloads folder:", "newest folder", ":pdfs:", "target1.pdf"} to integer
如果我完全删除“移动”行,我会得到结果:
{alias "Macintosh HD:Users:tsg:my downloads folder:", "newest folder", ":pdfs:", "target1.pdf"}
我实际上想根据搜索词找到文件,声明为 属性 (属性 searchWord : {"target"}),而不是硬编码,但我不能也让它工作。
代码:
set pdfFile to first file of pdfFolder whose name contains searchWord
错误:
"Can’t get file 1 of {alias "Macintosh HD:Users:tsg:my downloads folder:", "newest folder", ":pdfs:"}." number -1728 from file 1 of {alias "Macintosh HD:Users:tsg:my downloads folder:", "newest folder", ":pdfs:"}
可能有不止一个pdf包含“target”这个词,我其实想把所有包含搜索词的文件都上移一层,然后重命名它们pdfs.one.pdf和pdfs.two.pdf.
在此先感谢您的帮助。
出现错误是因为您尝试组合多个不兼容的类。
在处理 Finder 时,Finder 说明符语法
file "foo" of folder "bar" of folder "baz" of startup disk
是避免那些 cannot-do-this 错误的合理方法
tell application "Finder"
set downloadFolder to folder "my downloads folder" of home
set folderName to name of last item of (sort every folder of downloadFolder by modification date)
set targetFolder to folder folderName of downloadFolder
set pdfFolder to folder "pdfs" of targetFolder
set pdfFile to file "target1.pdf" of pdfFolder
move pdfFile to targetFolder
end tell
或者移动文件名中包含“target”的所有文件
tell application "Finder"
set downloadFolder to folder "my downloads folder" of home
set folderName to name of last item of (sort every folder of downloadFolder by modification date)
set targetFolder to folder folderName of downloadFolder
set pdfFolder to folder "pdfs" of targetFolder
set pdfFiles to files of pdfFolder whose name contains "target"
move pdfFiles to targetFolder
end tell
我不是脚本和编程的新手(或专家),但几个小时后,我对如何复制文件感到困惑(在谷歌搜索并尝试了几个小时的各种事情之后)在这种情况下使用 AppleScript。
我想将文件从最近修改的文件夹上移一层。我正在尝试移动文件:
/Users/tsg/my 下载 folder/newest folder/pdfs/target1.pdf
至
/Users/tsg/my 下载 folder/newest folder/target1.pdf
其中“最新文件夹”不是固定名称,但会随着我下载较新的文件夹而改变。
当我运行下面的代码时,
tell application "Finder"
set downloadFolder to POSIX file "/Users/tsg/my downloads folder" as alias
set latestModifiedFolder to (downloadFolder & (name of last item of (sort every folder of downloadFolder by modification date)))
set targetFolder to latestModifiedFolder (* Is this necessary? *)
set pdfFolder to (targetFolder & ":pdfs:")
set pdfFile to (pdfFolder & ("target1.pdf"))
move pdfFile to targetFolder
end tell
我收到错误消息
"Finder got an error: Can’t make {alias "Macintosh HD:Users:tsg:my downloads folder:", "newest folder"} into type folder." number -1700 from {alias "Macintosh HD:Users:tsg:my downloads folder:", "newest folder"} to folder
如果我在“end tell”之前的最后一行添加修饰符 folder: "move pdfFile to folder targetFolder",我得到
"Finder got an error: Can’t make {alias "Macintosh HD:Users:tsg:my downloads folder:", "newest folder"} into type integer." number -1700 from {alias "Macintosh HD:Users:tsg:my downloads folder:", "newest folder"} to integer"
如果我添加修饰符 file “将 file pdfFile 移动到 folder targetFolder”,我得到
"Finder got an error: Can’t make {alias "Macintosh HD:Users:tsg:my downloads folder:", "newest folder", ":pdfs:", "target1.pdf"} into type integer." number -1700 from {alias "Macintosh HD:Users:tsg:my downloads folder:", "newest folder", ":pdfs:", "target1.pdf"} to integer
如果我完全删除“移动”行,我会得到结果:
{alias "Macintosh HD:Users:tsg:my downloads folder:", "newest folder", ":pdfs:", "target1.pdf"}
我实际上想根据搜索词找到文件,声明为 属性 (属性 searchWord : {"target"}),而不是硬编码,但我不能也让它工作。
代码:
set pdfFile to first file of pdfFolder whose name contains searchWord
错误:
"Can’t get file 1 of {alias "Macintosh HD:Users:tsg:my downloads folder:", "newest folder", ":pdfs:"}." number -1728 from file 1 of {alias "Macintosh HD:Users:tsg:my downloads folder:", "newest folder", ":pdfs:"}
可能有不止一个pdf包含“target”这个词,我其实想把所有包含搜索词的文件都上移一层,然后重命名它们pdfs.one.pdf和pdfs.two.pdf.
在此先感谢您的帮助。
出现错误是因为您尝试组合多个不兼容的类。
在处理 Finder 时,Finder 说明符语法
file "foo" of folder "bar" of folder "baz" of startup disk
是避免那些 cannot-do-this 错误的合理方法
tell application "Finder"
set downloadFolder to folder "my downloads folder" of home
set folderName to name of last item of (sort every folder of downloadFolder by modification date)
set targetFolder to folder folderName of downloadFolder
set pdfFolder to folder "pdfs" of targetFolder
set pdfFile to file "target1.pdf" of pdfFolder
move pdfFile to targetFolder
end tell
或者移动文件名中包含“target”的所有文件
tell application "Finder"
set downloadFolder to folder "my downloads folder" of home
set folderName to name of last item of (sort every folder of downloadFolder by modification date)
set targetFolder to folder folderName of downloadFolder
set pdfFolder to folder "pdfs" of targetFolder
set pdfFiles to files of pdfFolder whose name contains "target"
move pdfFiles to targetFolder
end tell