Applescript遍历目录

时间:2013-01-03 00:40:04

标签: applescript

我有一个脚本可以添加或更新文件夹中的文件。我想做的是使用applescript将这些文件添加到iTunes。但我似乎无法遍历目录。

tell application "iTunes"
  if playlist myPlaylist exists then delete playlist myPlaylist
  set newPlaylist to (make new user playlist with properties {name:myPlaylist})

  repeat with theFile in myPath as list
    add theFile to playlist myPlaylist
  end repeat
end tell

对于每次重复,theFile的值是myPath的单个字符,而不是myPath中的实际文件。 theFile将是M,a,c,i,n,...而不是file1.mov,file2.mov,......

谢谢。

1 个答案:

答案 0 :(得分:1)

如果myPath是字符串,as list会将其转换为字符数组。

您可以告诉Finder获取别名列表:

tell application "Finder"
    set l to items of (POSIX file "/Users/username/Music/folder" as alias) as alias list
end tell
tell application "iTunes"
    if playlist "test" exists then
        delete tracks of playlist "test"
    else
        set p to make new user playlist with properties {name:"test"}
    end if
    add l to p
end tell

这会将所选文件添加到当前显示的播放列表中:

tell application "Finder"
    set l to selection as alias list
end tell
tell application "iTunes"
    add l to view of browser window 1
end tell