按创建日期过滤返回与POSIX路径不兼容的Finder对象参考:如何转换它?

时间:2015-03-05 18:54:54

标签: applescript osx-mavericks

我是这个论坛的新手,但对AppleScript来说更新。我非常接近得到我需要做的事情,但我在中间碰到了一个障碍。

我得到的错误与上面完全相同,这就是我在这里的原因。

set text item delimiters to "."

tell application "Finder"

    set theFilestoChoose to every item of (choose file with prompt "Please select the file(s) you would like to move and rename" with multiple selections allowed) as list

    display dialog "Would you like to move these files to an existing folder and then rename them, or create a new folder and then rename them?" buttons {"Move to an existing folder and rename", "Create a new folder and rename"}

    if result = {button returned:"Move to an existing folder and rename"} then

        set firstnewname to "Aauuttoommaattoorr"
        repeat with index from 1 to the count of theFilestoChoose
            set theFilesChosenbeingrenamedfirsttime to item index of theFilestoChoose
            set filenamecount to text items of (get name of theFilesChosenbeingrenamedfirsttime)
            if number of filenamecount is 1 then
                set fileextension to ""
            else
                set fileextension to "." & item -1 of filenamecount
            end if
            set the name of theFilesChosenbeingrenamedfirsttime to firstnewname & index & fileextension as string
        end repeat

        log theFilesChosenbeingrenamedfirsttime

        set choosingtheplacetomove to choose folder with prompt "Select the folder to move to"

        set thechosenfoldersname to name of folder choosingtheplacetomove -- sets the folder name as text
        set AppleScript's text item delimiters to {"-"}
        set Numberofthemonthatthebeginningofthefoldername to text item 1 of thechosenfoldersname as string -- for later to append the number back on without having to ask again! :)        
        set shortenedname to text item 2 of thechosenfoldersname as string
        set the name of choosingtheplacetomove to shortenedname as string
        set thefolderstemporarynameaslocation to choosingtheplacetomove as string


        move theFilestoChoose to folder thefolderstemporarynameaslocation
        log theFilestoChoose
        set allfilesindestinationfolder to every file in choosingtheplacetomove as alias list -- 

        set aInitials to the text returned of (display dialog "Whose camera were this/these pictures taken on?" default answer "")

        set filteredList to my filterList(allfilesindestinationfolder, aInitials) as list
        log filteredList

        -- everything above this is correct so far and works perfect




        --TROUBLE SECTION BELOW

        set theSortedfilterList to (sort filteredList by creation date) -- something happens here with the theSortedfilterList that makes it unintelligible to convert to POSIX later. but I need the files in the order that this line puts them.

        log theSortedfilterList

        set timetorenamelasttime to theSortedfilterList

        set newbasename to shortenedname

        repeat with index from 1 to the count of timetorenamelasttime
            set theonefile to item index of timetorenamelasttime
            set theonefilenamecount to text items of (get name of theonetwothreefile)
            if number of theonefilenamecount is 1 then
                set fileextensionone to ""
            else
                set fileextensionone to "." & item -1 of theonefilenamecount
            end if

            tell application "System Events" to set CreaDate to creation date of file theonefile
            set CreaDate2 to CreaDate as text -- need to trim down to the first 10 characters and eliminate the "-"
            set AppleScript's text item delimiters to {""}
            set shorteneddatename to text items 1 thru 10 of CreaDate2 as string

            set the name of theonefile to shorteneddatename & {"-"} & newbasename & {"-"} & aInitials & {"-"} & index & fileextensionone as string
        end repeat




        --Trouble section above :)      



        set the name of choosingtheplacetomove to Numberofthemonthatthebeginningofthefoldername & "-" & shortenedname as string -- returns the month prefix to the foldername

    else if result = {button returned:"Create a new folder and rename"} then

        set repeatConfirmation to true --Boolean to decided if script should be repeated; default is to repeat

        repeat while (repeatConfirmation = true) --Repeat if Any Tests Are Failed
            set thefirstquestion to choose from list {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"} with title "Event Month Selection" with prompt "Select the month in which the event started. Select only one month:"
            set theMonthAnswer to result
            if theMonthAnswer = false then --"For historical reasons, choose from list is the only dialog command that returns a result (false) instead of signaling an error when the user presses the “Cancel” button."
                set repeatConfirmation to false
                set exitingeventmonth to display dialog ("You are exiting selecting the event month phase of the program. You will need to manually fix this decision") buttons {"OK"} with title "Exiting Event Month Selection"
                if button returned of exitingeventmonth = "OK" then
                    set theMonthAnswer to ""
                end if
            else
                set confirmationanswer to display dialog "You selected " & theMonthAnswer & ", is this correct? " buttons {"Yes, that's correct.", "No, the picture(s) are from a different month."} with title "Confirm Event Month Selection"
                if button returned of confirmationanswer = "Yes, that's correct." then
                    set repeatConfirmation to false
                else
                    display dialog ("That's okay, you can select another again!") buttons {"OK"} with title "Return to Event Month Selection"
                end if

            end if
        end repeat

        log theMonthAnswer

        if theMonthAnswer = "" then
            set monthtonumber to "You have cancelled this action."
            log monthtonumber
        else if theMonthAnswer = {"January"} then
            set monthtonumber to "01"
            log monthtonumber
        else if theMonthAnswer = {"February"} then
            set monthtonumber to "02"
            log monthtonumber
        else if theMonthAnswer = {"March"} then
            set monthtonumber to "03"
            log monthtonumber
        else if theMonthAnswer = {"April"} then
            set monthtonumber to "04"
            log monthtonumber
        else if theMonthAnswer = {"May"} then
            set monthtonumber to "05"
            log monthtonumber
        else if theMonthAnswer = {"June"} then
            set monthtonumber to "06"
            log monthtonumber
        else if theMonthAnswer = {"July"} then
            set monthtonumber to "07"
            log monthtonumber
        else if theMonthAnswer = {"August"} then
            set monthtonumber to "08"
            log monthtonumber
        else if theMonthAnswer = {"September"} then
            set monthtonumber to "09"
            log monthtonumber
        else if theMonthAnswer = {"October"} then
            set monthtonumber to "10"
            log monthtonumber
        else if theMonthAnswer = {"November"} then
            set monthtonumber to "11"
            log monthtonumber
        else if theMonthAnswer = {"December"} then
            set monthtonumber to "12"
            log monthtonumber
        end if

        set theNameofFoldertoMake to text returned of (display dialog "Please enter the name of the new folder you are creating:" default answer "" with title "New Folder Name")

        set LocationOfNewFolder to choose folder with prompt "Choose the location of the new folder you are creating:"
        set theNewNameofFoldertoMake to monthtonumber & "-" & theNameofFoldertoMake
        set newfolderaction to make new folder at LocationOfNewFolder with properties {name:theNewNameofFoldertoMake}
        move theFilestoChoose to newfolderaction

    end if
end tell

--function

on filterList(allfilesindestinationfolder, aInitials)
    set patterns to {aInitials as string, "Aauuttoommaattoorr"}
    set output to {}

    repeat with aFile in the allfilesindestinationfolder
        repeat with aPattern in patterns
            set filepath to aFile as string
            if filepath contains aPattern then
                set end of the output to aFile
                exit repeat
            end if
        end repeat
    end repeat

    return output
end filterList

编辑:

好的,所以你想要调试和错误消息。谢谢你的指导。以下是我从以下几行得到的内容:

将theSortedfilterList设置为(按创建日期排序filteredList)

记录theSortedfilterList

日志返回:

文件夹文件夹的文件夹Aauuttoommaattoorr3.AVI文件夹桌面文件夹桌面文件夹用户名启动盘用户,文件夹文件夹文件夹Aauuttoommaattoorr2.MOV文件夹桌面文件夹桌面文件夹用户名文件夹用户启动盘,文件用户文件夹的文件Aauuttoommaattoorr1.mov文件夹的SnowDay文件夹的桌面文件夹的用户名启动盘的用户

将pxFile设置为((theSortedfilterList)的POSIX路径为别名)

“无法制作«class docf»\”Acuttoommaattoorr3.AVI \“of class cfol»\”SnowDay \“of class cfol»\”Desktop \“of class cfol»\”Username \“ «class cfol»\“用户\”的«类sdsk»应用程序\“Finder \”,«class docf»\“Aauuttoommaattoorr2.MOV \”«class cfol»\“SnowDay \”of class cfol»\ «class cfol»的“桌面”\“用户名”«class cfol»\“用户”的«class sdsk»应用程序\“Finder \”,«class docf»\“Aauuttoommaattoorr1.mov \”of« «ccl»»«class cfol»的“SnowDay”«class cfol»««cype»的“用户名”\«应用程序的«class sdsk»的“用户”\“Finder \”进入类型别名。“编号-1700来自{«class docf»“Aauuttoommaattoorr3.AVI”的«class cfol»“SnowDay”的«class cfol»“桌面”的«class cfol»“用户名”的«class cfol»“用户”的«class sdsk «,«class docf»«Aocuttoommaattoorr2.MOV“«class cfol»”SnowDay“«class cfol»”桌面“«class cfol»”用户名“«class cfol»”用户“«class sdsk»,« class docf»«class cfol»的“Aauuttoommaattoorr1.mov”«class cfol»“classcfol”的“桌面”«class cfol»的用户名««class sdsk»}的用户名} alias

这就是为什么我用Finder对象引用标记它不会返回POSIX路径,因为我尝试了使用POSIX路径行的建议。我认为看到它上面和周围的线条有助于设定背景。

所以我需要这些文件,但我需要按创建日期排序,以便按顺序进行下一个重命名步骤。

1 个答案:

答案 0 :(得分:0)

您可以强制将查找程序文件说明符添加到别名中,该别名具有名为POSIX path的属性

有点简短的例子,在想象的告诉应用程序“Finder”块中。

 set pxFile to POSIX path of ((file "HFSPath") as alias)

修改 您还可以使过滤器返回alias listas alias list)。 您可以浏览别名列表,并从每个列表中获取posix路径。