2012-04-27 26 views
1

早上好,發送的所有文件在桌面上到Evernote然後刪除

我想寫我可以運行,將發送我的桌面到Evernote上的所有文件,然後刪除文件,一個AppleScript。我至今的代碼是:

on run {input} 

tell application "Finder" 
    select every file of desktop 
end tell 

tell application "Evernote" 
    repeat with SelectedFile in input 
     try 
      create note from file SelectedFile notebook "Auto Import" 
     end try 

    end repeat 

end tell 

tell application "Finder" 
    delete every file of desktop 
end tell 

end run 

如果我跑這則第一個和最後一個「告訴」做工精細(即腳本凸顯然後刪除桌面上的所有文件),但中間「告訴」沒有按什麼都不做。

但是,如果我手動突出桌面上的所有文件,然後只需運行中間的「告訴」那麼進口精 - 每個項目到一個單獨的音符如預期。

正如你所知道的,我是新來的AppleScript - 我懷疑我需要把選定的文件在某種類型的數組,但不能弄明白。幫幫我!

非常感謝

豐富

回答

3

因爲你input變量,並通過查找文件的選擇沒有關係,你的代碼沒有 - 這意味着你的列表是空的,Evernote根本沒有處理任何東西。您通過將Evernote導入命令包裝在try塊中而沒有任何錯誤處理來混淆問題,這意味着所有錯誤都會被忽視(爲了避免這種問題,最好總是將錯誤消息記錄在on error條款,如果沒有別的)。

此外,您實際上不需要通過AppleScript選擇桌面上的文件來處理它們。下面的代碼將抓住所有可見文件(不含僞文件如包/應用程序):

tell application "System Events" 
    set desktopFiles to every disk item of (desktop folder of user domain) whose visible is true and class is file 
end tell 

傳給你檢索的方式到Evernote進行處理名單:

repeat with aFile in desktopFiles as list 
    try 
     tell application "Evernote" to create note from file (aFile as alias) notebook "Auto Import" 
     tell application "System Events" to delete aFile 
    on error errorMessage 
     log errorMessage 
    end try 
end repeat 

,你是好走。

請注意,通過審慎地將刪除命令(正好在導入命令之後,在try塊內,在所有文件的循環內),確保只在Evernote導入時沒有錯誤時纔會調用它,同時避免必須迭代文件幾次。

最後一點:如果只有一條命令要執行,則不必使用tell語句的塊語法 - 使用tell <target> to <command>更容易,並且會使您遠離嵌套的上下文地獄。

對處理列表和別名脅迫

+0

嗨kopischke。 desktopFiles已經是一個列表,你可以從你的重複命令中刪除「作爲列表」。另外,爲什麼((文件的路徑)作爲別名)而不是aFile作爲別名? – adayzdone 2012-04-28 00:17:34

+0

我發現'每個'過濾器通常只返回單個項目而不是單個項目列表,只有一個匹配時 - 將返回值強制到列表將確保循環無論如何工作。 * System Events *返回'disk item'對象,這些對象不能被直接強制轉換爲'alias'對象 - 因此通過路徑屬性繞道。 – kopischke 2012-04-28 00:44:06

+0

告訴應用程序「系統事件」獲取啓動盤的「應用程序」文件夾的每個磁盤項的別名,其可見性爲真 – adayzdone 2012-04-28 01:15:57

1

嘗試

tell application "System Events" to set xxx to get every file of (desktop folder of user domain) whose visible is true 

repeat with i from 1 to count of xxx 
    set SelectedFile to item i of xxx as alias 
    try 
     tell application "Evernote" to create note from file SelectedFile notebook "Auto Import" 
     tell application "Finder" to delete SelectedFile 
    end try 
end repeat 

感謝@fanaugen

+0

@Rich更正感謝@adayzone:此外,你應該只刪除在'xxx'桌面上的文件,而不是** **每一個文件。只需在'repeat'循環中添加一個「tell app」Finder「來刪除SelectedFile'。 – fanaugen 2012-04-27 14:10:43

相關問題