Automator/Applescript 文件排序和移动

发布于 2024-09-12 12:47:50 字数 251 浏览 6 评论 0原文

我想知道 Automator 中是否有一种方法可以使用 Applescript 代码来获取文件的扩展名,以及它是否等于特定的扩展名(例如 .pdf.rtf ) 移动到该扩展名的特定文件夹(例如 if (extension == pdf) { 移动到文件夹“~/PDF Files” } else if (extension == rtf) { 移动到文件夹“~/Rich Text Files” “ })

I was wondering if there was a way in Automator to use Applescript code to get the extension of a file, and if it equals a specific extension (e.g. .pdf or .rtf) move to a specific folder for that extension (e.g if (extension == pdf) { move to folder "~/PDF Files" } else if (extension == rtf) { move to folder "~/Rich Text Files" })

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

只涨不跌 2024-09-19 12:47:50

这是一个苹果脚本。因为你的要求很简单,所以我就写给你了。请注意我如何使用子例程“getNameAndExtension(F)”获取文件扩展名。通常,您可以从 Finder 获取文件扩展名(称为名称扩展),但我发现 Finder 并不总是可靠,因此我总是使用该子例程。该子程序一直是可靠的。

set homeFolder to path to home folder as text
set rtfFolderName to "Rich Text Files"
set pdfFolderName to "PDF Files"

-- choose the files
set theFiles to choose file with prompt "Choose RTF or PDF files to move into your home folder" with multiple selections allowed

-- make sure the folders exist
tell application "Finder"
    if not (exists folder (homeFolder & rtfFolderName)) then
        make new folder at folder homeFolder with properties {name:rtfFolderName}
    end if
    if not (exists folder (homeFolder & pdfFolderName)) then
        make new folder at folder homeFolder with properties {name:pdfFolderName}
    end if
end tell

-- move the files
repeat with aFile in theFiles
    set fileExtension to item 2 of getNameAndExtension(aFile)
    if fileExtension is "rtf" then
        tell application "Finder"
            move aFile to folder (homeFolder & rtfFolderName)
        end tell
    else if fileExtension is "pdf" then
        tell application "Finder"
            move aFile to folder (homeFolder & pdfFolderName)
        end tell
    end if
end repeat



(*=============== SUBROUTINES ===============*)
on getNameAndExtension(F)
    set F to F as Unicode text
    set {name:Nm, name extension:Ex} to info for file F without size
    if Ex is missing value then set Ex to ""
    if Ex is not "" then
        set Nm to text 1 thru ((count Nm) - (count Ex) - 1) of Nm
    end if
    return {Nm, Ex}
end getNameAndExtension

Here's an applescript. Since your request was simple I just wrote it for you. Note how I get the file extension with the subroutine "getNameAndExtension(F)". Normally you can get the file extension from the Finder (called name extension) but I've found that the Finder is not always reliable so I always use that subroutine. That subroutine has always been reliable.

set homeFolder to path to home folder as text
set rtfFolderName to "Rich Text Files"
set pdfFolderName to "PDF Files"

-- choose the files
set theFiles to choose file with prompt "Choose RTF or PDF files to move into your home folder" with multiple selections allowed

-- make sure the folders exist
tell application "Finder"
    if not (exists folder (homeFolder & rtfFolderName)) then
        make new folder at folder homeFolder with properties {name:rtfFolderName}
    end if
    if not (exists folder (homeFolder & pdfFolderName)) then
        make new folder at folder homeFolder with properties {name:pdfFolderName}
    end if
end tell

-- move the files
repeat with aFile in theFiles
    set fileExtension to item 2 of getNameAndExtension(aFile)
    if fileExtension is "rtf" then
        tell application "Finder"
            move aFile to folder (homeFolder & rtfFolderName)
        end tell
    else if fileExtension is "pdf" then
        tell application "Finder"
            move aFile to folder (homeFolder & pdfFolderName)
        end tell
    end if
end repeat



(*=============== SUBROUTINES ===============*)
on getNameAndExtension(F)
    set F to F as Unicode text
    set {name:Nm, name extension:Ex} to info for file F without size
    if Ex is missing value then set Ex to ""
    if Ex is not "" then
        set Nm to text 1 thru ((count Nm) - (count Ex) - 1) of Nm
    end if
    return {Nm, Ex}
end getNameAndExtension
简单 2024-09-19 12:47:50

我现在不在苹果公司,所以我无法使用 Automator 来解决这个问题。但是,如果您可以通过将查找器切换到列表视图,按类型排序,然后选择相同类型的文件块并将它们拖到正确的文件夹中来做到这一点。

I'm not at my Apple right now, so I can't play around with Automator and figure it out. But if you could do this by switching finder to list view, sort by type, and then select blocks of files of the same type and drag them into the right folder.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文