2

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

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

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

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

    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

4

1 回答 1

1

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

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
于 2012-11-17T00:33:11.950 回答