2012-04-27 28 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

相关问题