在Applescript中打开包含变量的文件夹

时间:2012-11-16 23:48:24

标签: applescript

我正在尝试创建一个脚本,根据我每天从网站下载的文件为我运行一个函数。

基本上,我的网站根据日期(yyyy,mm,dd)创建一个文件夹。我希望Applecript首先打开该文件夹,然后在里面的每个文件夹/文件上运行操作。

麻烦的是,当我尝试使用变量将脚本指向该文件夹时,它会给出错误:无法将路径名转换为常量。

也许真正的问题是我对此有多新!这是我尝试过的:

    tell application "Finder"
    set {year:y, month:m, day:d} to (current date)
    set theTime to y & m & d
    set pathToTarget to path to theTime
    open folder pathToTarget of folder "F-grams" of folder "files" of folder "default" of folder "sites" of folder "foldagram" of folder "htdocs" of folder "MAMP" of folder "Applications" of startup disk
end tell

谁想帮助新手上路? -JB

1 个答案:

答案 0 :(得分:1)

每次将新项目添加到目标文件夹时,都会触发此文件夹操作。第二个重复块将允许您为每个添加的文件夹中的每个项目发送命令。您可以使用anItem变量引用这些项中的每一项。将脚本保存在库中的“文件夹操作脚本”文件夹中。请务必将文件夹操作附加到目标文件夹。

on adding folder items to theFolder after receiving theFolders
    try
        repeat with aFolder in theFolders
            tell application "Finder" to set myItems to every item of aFolder
            repeat with anItem in myItems
                -- Insert your code here
            end repeat
        end repeat
    on error errMsg number errNum
        tell me
            activate
            display alert errMsg & return & return & "Error number" & errNum buttons "Cancel"
        end tell
    end try
end adding folder items to

但是,如果您想按日期搜索文件夹,则应按照以下方式设置日期字符串的格式:

set delimiter to ", "
set aDate to (current date)
set aYear to (year of aDate) as string
set aMonth to (month of aDate as integer) as string
if length of aMonth is 1 then set aMonth to "0" & aMonth
set aDay to (day of aDate as integer) as string
if length of aDay is 1 then set aDay to "0" & aDay
set theTime to aYear & delimiter & aMonth & delimiter & aDay as string