Compilation 编辑applescript时出现问题

Compilation 编辑applescript时出现问题,compilation,applescript,Compilation,Applescript,我刚刚编辑了/Macintosh HD/Library/Scripts/Folder Actions…/文件夹中的文件new item alert.scpt。在编辑了一些权限后,我设法保存了它,但当我单击Run或Compile时,什么也没有发生 这是一个常见的错误,或者如果有人不介意看一下,我的applescript中是否有问题 property Docs : "Macintosh HD:Users:Max:Downloads:Docs" property Music : "Macintosh

我刚刚编辑了/Macintosh HD/Library/Scripts/Folder Actions…/文件夹中的文件
new item alert.scpt
。在编辑了一些权限后,我设法保存了它,但当我单击
Run
Compile
时,什么也没有发生

这是一个常见的错误,或者如果有人不介意看一下,我的applescript中是否有问题

property Docs : "Macintosh HD:Users:Max:Downloads:Docs"
property Music : "Macintosh HD:Users:Max:Downloads:Music"
property Videos : "Macintosh HD:Users:Max:Downloads:Videos"
property Images : "Macintosh HD:Users:Max:Downloads:Images"
property Profiles : "Macintosh HD:Users:Max:Downloads:iPhone:Profiles"
property Zips : "Macintosh HD:Users:Max:Downloads:Zips"
property PSDs : "Macintosh HD:Users:Max:Downloads:PSDs"

on adding folder items to this_folder after receiving added_items
    try
        tell application "Finder"
            --get the name of the folder
            set the folder_name to the name of this_folder
            if (name of eachitem ends with ".png") then
                move eachitem to folder Images
            end if
            if (name of eachitem ends with ".JPEG") then
                move eachitem to folder Images
            end if
            if (name of eachitem ends with ".gif") then
                move eachitem to folder Images
            end if
            if (name of eachitem ends with ".jpg") then
                move eachitem to folder Images
            end if
            if (name of eachitem ends with ".jpeg") then
                move eachitem to folder Images
            end if
            if (name of eachitem ends with ".PNG") then
                move eachitem to folder Images
            end if
            if (name of eachitem ends with ".mov") then
                move eachitem to folder Videos
            end if
            if (name of eachitem ends with ".avi") then
                move eachitem to folder Videos
            end if
            if (name of eachitem ends with ".wma") then
                move eachitem to folder Videos
            end if
            if (name of eachitem ends with ".m4v") then
                move eachitem to folder Videos
            end if
            if (name of eachitem ends with ".mp4") then
                move eachitem to folder Music
            end if
            if (name of eachitem ends with ".mp3") then
                move eachitem to folder Music
            end if
            if (name of eachitem ends with ".wav") then
                move eachitem to folder Music
            end if
            if (name of eachitem ends with ".wma") then
                move eachitem to folder Music
            end if
            if (name of eachitem ends with ".pdf") then
                move eachitem to folder Docs
            end if
            if (name of eachitem ends with ".doc") then
                move eachitem to folder Docs
            end if
            if (name of eachitem ends with ".docx") then
                move eachitem to folder Docs
            end if
            if (name of eachitem ends with ".pages") then
                move eachitem to folder Docs
            end if
            if (name of eachitem ends with ".ppt") then
                move eachitem to folder Docs
            end if
            if (name of eachitem ends with ".pptx") then
                move eachitem to folder Docs
            end if
            if (name of eachitem ends with ".mobileprovision") then
                move eachitem to folder Profiles
            end if
            if (name of eachitem ends with ".zip") then
                move eachitem to folder Zips
            end if
            if (name of eachitem ends with ".psd") then
                move eachitem to folder PSDs
            end if


        end tell

        -- find out how many new items have been placed in the folder
        set the item_count to the number of items in the added_items
        --create the alert string
        set alert_message to ("Folder Actions Alert:" & return & return) as Unicode text
        if the item_count is greater than 1 then
            set alert_message to alert_message & (the item_count as text) & " new items have "
        else
            set alert_message to alert_message & "One new item has "
        end if
        set alert_message to alert_message & "been placed in folder " & «data utxt201C» & the folder_name & «data utxt201D» & "."
        set the alert_message to (the alert_message & return & return & "Would you like to view the added items?")

        display dialog the alert_message buttons {"Yes", "No"} default button 2 with icon 1 giving up after dialog_timeout
        set the user_choice to the button returned of the result

        if user_choice is "Yes" then
            tell application "Finder"
                --go to the desktop 
                activate
                --open the folder
                open this_folder
                --select the items
                reveal the added_items
            end tell
        end if
    end try
end adding folder items to

文件夹操作脚本不应直接运行。它通过文件夹的上下文菜单与任意数量的文件夹相关联

然后,每当该文件夹的内容发生更改时,就会触发脚本,并运行将文件夹项添加到处理程序的

因此,您只需右键单击要附加脚本的文件夹,然后选择
Set folder action
菜单项


另外,您不应该覆盖系统提供的文件。。。您可以将自定义版本保存在自己的主目录中,并将其附加到任何文件夹。

文件夹操作处理程序将以静默方式失败,因此如果出现错误,脚本将中止。在发布的脚本中,您根本没有查看添加的项目,而是使用了一个未定义的变量-eachitem。还要注意的是,如果使用一个try语句而不使用on error部分,则只会吃掉所有的错误,这也无助于调试。我知道你使用的是苹果的一个示例脚本,但那东西已经很旧了

我发现,使用文件夹操作时,最好的方法是将脚本的主要部分放在文件夹操作处理程序之外,这样它也可以正常运行(脚本不需要专门用作文件夹操作)-这有助于调试,还可以为您提供小程序或小程序。对于测试,您还可以注释掉文件夹操作处理程序声明,并添加语句以获取文件夹和文件

我希望您不介意,但我冒昧地重新安排了您的脚本,以便它也可以从脚本编辑器运行:

property Docs : "Macintosh HD:Users:Max:Downloads:Docs"
property Music : "Macintosh HD:Users:Max:Downloads:Music"
property Videos : "Macintosh HD:Users:Max:Downloads:Videos"
property Images : "Macintosh HD:Users:Max:Downloads:Images"
property Profiles : "Macintosh HD:Users:Max:Downloads:iPhone:Profiles"
property Zips : "Macintosh HD:Users:Max:Downloads:Zips"
property PSDs : "Macintosh HD:Users:Max:Downloads:PSDs"

property dialogTimeout : 10


on run -- script run as an application or from the Script Editor
    set someFiles to (choose file with multiple selections allowed) -- manually get some files
    tell application "Finder" to set containingFolder to (container of first item of someFiles) as alias
    doStuff(containingFolder, someFiles)
end run


on adding folder items to this_folder after receiving added_items -- folder action handler
    doStuff(this_folder, added_items)
end adding folder items to


on doStuff(theFolder, theStuff) -- do stuff with file items in a folder
    set numberOfItems to (count theStuff)
    tell application "Finder"
        set folderName to name of theFolder
        repeat with anItem in theStuff
            if name extension of anItem is in {"png", "jpg", "jpeg", "gif"} then
                move anItem to folder Images
            else if name extension of anItem is in {"mov", "avi", "wma", "m4v"} then
                move anItem to folder Videos
            else if name extension of anItem is in {"mp4", "mp3", "wav", "wma"} then
                move anItem to folder Music
            else if name extension of anItem is in {"pdf", "doc", "docx", "pages", "ppt", "pptx"} then
                move anItem to folder Docs
            else if name extension of anItem is in {"mobileprovision"} then
                move anItem to folder Profiles
            else if name extension of anItem is in {"zip"} then
                move anItem to folder Zips
            else if name extension of anItem is in {"psd"} then
                move anItem to folder PSDs
            else
                -- no match, so leave it
            end if
        end repeat
    end tell

    -- create the alert string
    set alertText to ("Folder Actions Alert:" & return & return) as Unicode text
    if numberOfItems is greater than 1 then
        set alertText to alertText & numberOfItems & " new items have "
        set plural to "s"
    else
        set alertText to alertText & "One new item has "
        set plural to ""
    end if
    set alertText to alertText & "been added to the folder " & «data utxt201C» & folderName & «data utxt201D» & "."
    set alertText to alertText & return & return & "Would you like to view the added item" & plural & "?"

    display dialog alertText buttons {"Yes", "No"} default button 2 with icon 1 giving up after dialogTimeout

    if (button returned of the result) is "Yes" then tell application "Finder"
        activate
        -- open theFolder
        reveal theStuff
    end tell
end doStuff

我只是这样做了,并用中的代码创建了一个新脚本,但每当我向文件夹中添加一个项目时,都不会发生任何事情。知道我做错了什么吗?我刚刚收到一个警报,询问我是否要查看此文件。这正是您的脚本在“--create the alert string”行之后所做的。因此,您的代码至少是由fonder操作启动的,但是您的代码没有执行您想要执行的操作。我想这应该是关于堆栈溢出的另一个问题,与您刚才提出的问题无关。。。