简体   繁体   中英

Apple Script runs more than once when file is opened

I have a piece of code which is used as a folder action and constantly watches the folder. When a new file is placed in the folder, the script runs and outputs a dialog box. The user can either open or print the file from the dialog box. However, when I select the open button, the code then produces the dialog box again when the a new file hasn't been opened. This only occurs when the file is opened and not when it is printed.

Can anyone help? Code is below

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

Edit: Mojave is running on the iMac in question.

There were a few problematic points in this code, any of which could produce odd behavior, so I cleaned it up so that it works correctly on my machine. New code first, then bullet points on what I changed...

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
  • "after receiving" always gives a list of aliases, even if it's a single item, so we should loop through the list rather than trying to convert it directly to string.
  • You duplicated the same code for 'HA' and 'OG' files, so I merged them
  • Your Display Dialog cited the default button as "Go to order" when your buttons were "Dismiss", "Print", "Go to". That was throwing an error (Display Dialog wants an exact match to one of the buttons). I replaced that with the index 3.
  • Folder Actions and the Finder do not always play well together, so I switched to the System Events app for the dialogs and the file open procedure.

This should work properly now...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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