2013-07-18 19 views
0

我的智能手機中有很多照片。所有這些文件名格式是yyyy-mm-dd hh:mm:ss.jpgAppleScript:從文件名中獲取星期編號

我需要根據星期數將它們排序到文件夾中。

我用AppleScript將文件移動到文件夾沒有問題:)我也沒有定義給定日期的星期編號的問題。我有一個問題(我認爲)與語法甚至數據類型,但我不明白問題所在。

這是我正在使用的。

  1. 我有一個定義給定日期
  2. 我通過我的文件夾進行掃描,並得到所有的名字進入名單
  3. 我解析名稱獲得YYYY,MM和DD一個星期數的函數變量
  4. 然後我嘗試用這些變量調用我的函數 - 這裏是問題的起點。

請在我的代碼這個例子來看看:

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) 

在這種情況下,行

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

好的工作(我的意思是,沒有錯誤),但因爲它是外該循環僅爲循環中的最後一個圖像提供結果。 但如果我將它移動到環路,則它給出了一個錯誤:

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 

的錯誤給出的第一個文件之後鬥爭採取:

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的幾乎完全新手,所以我不明白爲什麼代碼在循環外工作,而不是在循環內工作。我將不勝感激這個問題的任何幫助。

非常感謝!

回答

0

你應該堅持原有的格式:

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 + days

set _date to _date - (3 * 24 * 60 * 60)
與 相同set _date to _date - (3 *天)

集_matchDate到_matchDate +(7 * 24 * 60 * 60)
相同
集_matchDate到_matchDate +周

1

這可能是更容易使用至少一個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