脚本将文件移动到Applescript中新创建的文件夹时出现问题

时间:2015-09-23 23:17:52

标签: applescript finder

我遇到的问题是我认为这是一项相当简单的任务,但似乎无法使我的脚本正常工作。我通过论坛找到了很多关于所使用的个人例程的帮助,但它似乎仍然失败。

简而言之,我想要做的是监控一个文件夹,查找要添加的新文件。一旦添加了一批文件(每隔一天左右),它将在另一个目录中创建一个文件夹,新文件夹名称是当前日期,将这些文件移动到新目录,然后执行一个简单的bash脚本使用新目录名称作为参数。

我的脚本编译正常,但是一旦添加了文件,它只会创建新文件夹而不会创建任何其他内容。我感谢任何帮助。

property the_sep : "-"

on adding folder items to my_folder after receiving the_files

tell application "Finder"
    (* First create a new folder with name of folder = current date *)
    set the_path to (folder "qa" of folder "Documents" of folder "ehmlab" of folder "Users" of disk "Macintosh HD")
    set the_name to (item 1 of my myDate())
    set the_name to (the_name & the_sep & item 2 of my myDate())
    set the_name to (the_name & the_sep & item 3 of my myDate())
    make folder at the_path with properties {name:the_name}
    set newDir to the_path & the_name
end tell

(* Next, move the newly added files to the source into the newly created date folder *)
repeat with i from 1 to number of items in the_files
    tell application "Finder"
        try
            set this_file to (item i of the_files)
            move file this_file to folder newDir
        end try
    end tell
end repeat

do shell script "qc.sh " & newDir

end adding folder items to

on myDate()
set myYear to "" & year of (current date)
set myMth to text -2 thru -1 of ("0" & (month of (current date)) * 1)
set myDay to text -2 thru -1 of ("0" & day of (current date))
return {myYear, myMth, myDay}
end myDate

1 个答案:

答案 0 :(得分:0)

失败的原因是不同的路径风格。

AppleScript使用HFS路径(冒号分隔) UNIX使用POSIX路径(斜杠分隔)。

解决方案是将HFS路径字符串强制转换为POSIX路径

do shell script "qc.sh " & quoted form of POSIX path of newDir

这是使用shell的脚本的较短版本,也用于时间戳和创建目录。

on adding folder items to my_folder after receiving the_files

  (* Next, move the newly added files to the source into the newly created date folder,
  "path to documents" is a relative path to the documents folder of the current user *)

  set baseFolder to (path to documents folder as text) & "qa:"
  set timeStamp to do shell script "date +%Y-%m-%d"
  set newDir to baseFolder & timeStamp
  do shell script "mkdir -p " & quoted form of POSIX path of newDir

  (* Next, move the newly added files to the source into the newly created date folder *)

  repeat with aFile in the_files
    tell application "Finder"
      try
        move aFile to folder newDir
      end try
    end tell
  end repeat

  do shell script "qc.sh " & quoted form of POSIX path of newDir

end adding folder items to