Applescript:从文件名中获取周数

Applescript: Getting week number from file name

我的智能手机上有很多照片。它们的文件名格式都是 yyyy-mm-dd hh:mm:ss.jpg

我需要根据周数将它们分类到文件夹中。

使用 AppleScript 将文件移动到文件夹中没有问题 :) 为给定日期定义周数也没有问题。我在语法甚至数据类型方面(我认为)有问题,但我不明白问题出在哪里。

这是我正在使用的。

  • 我有一个为给定日期定义周数的功能
  • 我扫描我的文件夹并将所有名称都放入列表中
  • 我解析名称以获取 yyyy、mm 和 dd 作为变量
  • 然后我尝试使用这些变量调用我的函数 - 这就是问题开始的地方。
  • 请看一下我的代码示例:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    property DBFolder :"Macintosh HD:Users:lyubovberezina:Dropbox:DB"
    property CUFolder :"Macintosh HD:Users:lyubovberezina:Dropbox:Camera Uploads"

    on weekNumber(_inputDate)

    script Week_Number_Extras

        on dateOfFirstWeekOfYear(_year)
            -- Get Monday of first week number
            set _date to current date
            set year of _date to _year
            set month of _date to 1
            set day of _date to 1
            set time of _date to 0

            -- Get the first Thursday of this year
            repeat until weekday of _date is Thursday
                set _date to _date + (24 * 60 * 60)
            end repeat

            -- Return the Monday before
            set _date to _date - (3 * 24 * 60 * 60)

            return _date
        end dateOfFirstWeekOfYear

    end script

    -- Make a copy of the passed date object to avoid changes to the original
    copy _inputDate to _targetDate

    -- Reset the time and go back to Monday
    set time of _targetDate to 0
    repeat until weekday of _targetDate is Monday
        set _targetDate to _targetDate - (24 * 60 * 60)
    end repeat

    -- Get the date of the first week for the current year and the next one
    tell Week_Number_Extras
        set _matchDate to dateOfFirstWeekOfYear(year of _targetDate)
        set _nextYearsFirstWeekDate to dateOfFirstWeekOfYear((year of _targetDate) + 1)
    end tell

    -- Exit early, if the current week is the first one of next year
    if _targetDate = _nextYearsFirstWeekDate then return 1

    -- Count up until the target date is reached
    set _weekNumber to 1
    repeat until _targetDate = _matchDate
        set _matchDate to _matchDate + (7 * 24 * 60 * 60)
        set _weekNumber to _weekNumber + 1
    end repeat

    return _weekNumber
    end weekNumber

    tell application"Finder"
    set this_folder to folder CUFolder
    set this_list to every file of this_folder
    repeat with i in this_list
        set fileName to name of i
        set fileYear to characters 1 thru 4 of fileName as string
        set fileMonth to characters 6 thru 7 of fileName as string
        set fileDay to characters 9 thru 10 of fileName as string
    end repeat
    end tell

    set theNewDate to date (fileMonth &"/" & fileDay &"/" & fileYear)
    weekNumber(theNewDate)

    在这种情况下是行

    1
    set theNewDate1 to date (fileMonth &"/" & fileDay &"/" & fileYear)

    工作正常(我的意思是,没有错误),但由于它在循环之外,它只给出循环中最后一个图像的结果。
    但是,如果我将它移到循环中,则会出现错误:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    property DBFolder :"Macintosh HD:Users:lyubovberezina:Dropbox:DB"
    property CUFolder :"Macintosh HD:Users:lyubovberezina:Dropbox:Camera Uploads"

    on weekNumber(_inputDate)

    script Week_Number_Extras

        on dateOfFirstWeekOfYear(_year)
            -- Get Monday of first week number
            set _date to current date
            set year of _date to _year
            set month of _date to 1
            set day of _date to 1
            set time of _date to 0

            -- Get the first Thursday of this year
            repeat until weekday of _date is Thursday
                set _date to _date + (24 * 60 * 60)
            end repeat

            -- Return the Monday before
            set _date to _date - (3 * 24 * 60 * 60)

            return _date
        end dateOfFirstWeekOfYear

    end script

    -- Make a copy of the passed date object to avoid changes to the original
    copy _inputDate to _targetDate

    -- Reset the time and go back to Monday
    set time of _targetDate to 0
    repeat until weekday of _targetDate is Monday
        set _targetDate to _targetDate - (24 * 60 * 60)
    end repeat

    -- Get the date of the first week for the current year and the next one
    tell Week_Number_Extras
        set _matchDate to dateOfFirstWeekOfYear(year of _targetDate)
        set _nextYearsFirstWeekDate to dateOfFirstWeekOfYear((year of _targetDate) + 1)
    end tell

    -- Exit early, if the current week is the first one of next year
    if _targetDate = _nextYearsFirstWeekDate then return 1

    -- Count up until the target date is reached
    set _weekNumber to 1
    repeat until _targetDate = _matchDate
        set _matchDate to _matchDate + (7 * 24 * 60 * 60)
        set _weekNumber to _weekNumber + 1
    end repeat

    return _weekNumber
    end weekNumber

    tell application"Finder"
    set this_folder to folder CUFolder
    set this_list to every file of this_folder
    repeat with i in this_list
        set fileName to name of i
        set fileYear to characters 1 thru 4 of fileName as string
        set fileMonth to characters 6 thru 7 of fileName as string
        set fileDay to characters 9 thru 10 of fileName as string
        set theNewDate to date (fileMonth &"/" & fileDay &"/" & fileYear)
        weekNumber(theNewDate)
    end repeat
    end tell

    获取第一个文件后报错:

    1
    2
    3
    4
    5
    6
    get name of document file"2013-01-02 02.43.21.jpg" of folder"Camera Uploads" of folder"Dropbox" of folder"lyubovberezina" of folder"Users" of startup disk
        -->"2013-01-02 02.43.21.jpg"
    get date"01/02/2013"
        --> error number -1728 from date"01/02/2013"
    Result:
    error"Finder got an error: Can’t get date "01/02/2013"." number -1728 from date"Wednesday, January 2, 2013 12:00:00 AM"

    我几乎是 Applescript 的新手,所以我无法理解为什么代码在循环外工作而不在循环内工作。对于此事,我将不胜感激。

    非常感谢!


    至少在周数部分使用 shell 脚本可能更容易:

    do shell script"date -jf %F 2013-07-19 +%V"

    cd ~/Dropbox/Camera\\ Uploads; for f in *; do d=../DB/$(date -jf %F ${f:0:10} +%Y-%V"$f"); mkdir -p $d; mv"$f" $d; done


    你应该坚持你原来的格式:

    1
    2
    3
    4
    set theNewDate to current date
    set theNewDate's year to fileYear
    set theNewDate's month to fileMonth
    set theNewDate's day to fileDay

    还有:

    将 _date 设置为 _date (24 * 60 * 60)
    是相同的
    将 _date 设置为 _date 天

    将 _date 设置为 _date - (3 * 24 * 60 * 60)
    是相同的
    将 _date 设置为 _date - (3 * days)

    将 _matchDate 设置为 _matchDate (7 * 24 * 60 * 60)
    是相同的
    将 _matchDate 设置为 _matchDate 周