English 中文(简体)
Automator/Applescript File Sorting and Moving
原标题:

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" })

最佳回答

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
问题回答

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.





相关问题
Creating Help Files - Applescript Studio

I would like to know how to generate help files for an applescript studio application I am currently creating. I have tried many different options of creating help, and googled it for quite some time ...

封顶下载和开放式链接

I ve在Angap邮报中设定了一条规则,以操作“Download & Openlink”pple。 我希望这一文字在邮件电文中下载该词,在下载该词之后,该词应开放。

AppleScript to open named terminal window

I have two windows/tabs set up to run in Terminal.app, "syd" and "mel". i.e. in Shell | New Window, "syd" and "mel" are listed. How can I open these terminal configurations with AppleScript?

Hello World Error

I m learning AppleScript and my first program is a Hello World(of course!): display dialog "Hello World" But when I try to run this I got the error: The result of a numeric operation was too ...

User properties/ privileges in AppleScript

I want to write an applescript program that first checks to see if the user has Admin privileges, and if it doesn t then requesting a re-log-in or something. Eventually the script is going to need to ...

Applescript to archive email in Mail.app

I need to write an Applescript for Mail.app that will take all messages in my Inbox and Sent Messages that are older than a certain # of days and move them into respective folders "On My Mac", or ...

热门标签