簡體   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