繁体   English   中英

打开文件后,Apple Script会运行多次

[英]Apple Script runs more than once when file is opened

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

有人可以帮忙吗? 代码如下

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上运行。

这段代码中有一些问题点,任何问题都可能产生奇怪的行为,因此我对其进行了清理,以使其在我的机器上可以正常工作。 首先是新代码,然后是我所做的更改的重点...

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”文件复制了相同的代码,所以我将它们合并
  • 当您的按钮分别为“关闭”,“打印”,“转到”时,“显示对话框”将默认按钮称为“转到订单”。 那就是抛出一个错误(“显示对话框”希望与按钮之一完全匹配)。 我将其替换为索引3。
  • 文件夹操作和Finder有时无法始终很好地配合使用,因此我切换到“系统事件”应用程序以获取对话框和文件打开过程。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM