2011-07-27 53 views
0

我剛剛在/ Macintosh HD/Library/Scripts/Folder Actions ... /文件夾中編輯了文件new item alert.scpt。我管理的一些編輯權限後,將其保存,但是當我點擊RunCompile沒有任何反應。已編輯的蘋果文件的問題

這是一個常見的錯誤,或者如果有人不介意看看我的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 

回答

0

文件夾操作腳本不應該直接運行。它通過這些文件夾的上下文菜單關聯到任意數量的文件夾。

然後,只要該文件夾的內容發生變化,腳本就會被觸發,並且運行on adding folder items to處理程序。

因此,您應該右鍵單擊要附加腳本的文件夾,然後選擇Set folder action菜單項。

此外,你不應該覆蓋系統提供的文件...你可以保存你的自定義版本在你自己的主目錄,你可以將它附加到任何文件夾。

+0

我只是做了這一點,創造了一個新的腳本與代碼,但每當我添加一個項目到文件夾,什麼也沒有發生。任何想法我做錯了什麼?我只是收到一個提醒,詢問我是否想查看這個文件。 –

+0

這正是你的腳本,該行後' - 創建警報string'在做什麼。所以,你的代碼是至少由小別勝新婚的行動推出,但你的代碼是不是做你打算做什麼。我想這應該是堆棧溢出的另一個問題,獨立於你剛纔問的...... – Yuji

0

的文件夾操作處理程序會默默地消失,所以如果有一個錯誤的腳本將中止。在您發佈的腳本,你是不是通過增加的項目要在所有的,而你使用的是未定義的變量 - eachitem。另外請注意,使用嘗試聲明沒有錯誤部分將只是吃任何和所有錯誤,這對調試無益。我知道你正在使用蘋果的一個示例腳本,但那件事很老舊。

我發現使用文件夾操作時的最佳方法是將腳本的主要部分放在文件夾操作處理程序之外,以便它也可以正常運行(腳本不需要專注於只是一個文件夾操作) - 這有助於調試,也可以給你一個小程序或滴。爲了測試,您還可以註釋文件夾操作處理程序聲明並添加語句以獲取文件夾和文件。

我希望你不介意,但我還是把重新安排腳本,以便它也可以從腳本編輯器運行的自由:

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