首页 > 解决方案 > 打开文件时,Apple Script 会多次运行

问题描述

我有一段代码用作文件夹操作并不断监视文件夹。将新文件放入文件夹时,脚本运行并输出一个对话框。用户可以从对话框中打开或打印文件。但是,当我选择打开按钮时,代码会在新文件尚未打开时再次生成对话框。这仅在文件打开时发生,而不是在打印时发生。

任何人都可以帮忙吗?代码如下

on adding folder items to theAttachedFolder after receiving theNewItems
    set filepath to theNewItems as string
    if filepath contains "HA" then
        set theDialogText to "HA is in file name"
        do shell script "afplay '/System/Library/Sounds/Submarine.aiff'"
        display dialog theDialogText buttons {"Dismiss", "Print", "Go to "} default button "Go to order" with icon note

    if result = {button returned:"Go to"} then
        tell application "Finder"
            open file filepath
        end tell
    else if result = {button returned:"Print"} then
        tell application "Shelf Label Printer"
            activate
            print filepath
            quit
        end tell
        display dialog "Printed" with icon note
    end if
if filepath contains "OG" then
    set theDialogText to "OG is in file name"
    do shell script "afplay '/System/Library/Sounds/Submarine.aiff'"
    display dialog theDialogText buttons {"Dismiss", "Print", "Go to"} default button "Go to order" with icon note

    if result = {button returned:"Go to"} then
        tell application "Finder"
            open file filepath
        end tell
    else if result = {button returned:"Print"} then
        tell application "Shelf Label Printer"
            activate
            print filepath
            quit
        end tell
        display dialog "Printed" with icon note

编辑: Mojave 正在有问题的 iMac 上运行。

标签: macosdirectoryapplescript

解决方案


这段代码有几个问题点,任何一个都可能产生奇怪的行为,所以我清理了它,让它在我的机器上正常工作。首先是新代码,然后是我更改的要点...

on adding folder items to theAttachedFolder after receiving theNewItems
    repeat with thisItem in theNewItems
        set filepath to POSIX path of thisItem as string
        if filepath contains "HA" or filepath contains "OG" then
            set theDialogText to "HA or OG is in file name"
            do shell script "afplay '/System/Library/Sounds/Submarine.aiff'"
            tell application "System Events"
                display dialog theDialogText buttons {"Dismiss", "Print", "Go to"} default button 3 with icon note
            end tell
            if result = {button returned:"Go to"} then
                tell application "System Events"
                    open file filepath
                end tell
            else if result = {button returned:"Print"} then
                tell application "Shelf Label Printer"
                    activate
                    print filepath
                    quit
                end tell
                display dialog "Printed" with icon note
            end if
        end if
    end repeat
end adding folder items to
  • “接收后”总是给出一个别名列表,即使它是单个项目,所以我们应该循环遍历列表而不是尝试将其直接转换为字符串。
  • 您为“HA”和“OG”文件复制了相同的代码,所以我将它们合并
  • 当您的按钮是“Dismiss”、“Print”、“Go to”时,您的显示对话框将默认按钮称为“Go to order”。这引发了一个错误(显示对话框希望与其中一个按钮完全匹配)。我用索引 3 替换了它。
  • Folder Actions 和 Finder 并不总是能很好地配合使用,所以我切换到 System Events 应用程序的对话框和文件打开过程。

这现在应该可以正常工作...


推荐阅读