将桌面上的所有文件发送到Evernote然后删除

时间:2012-04-27 08:47:49

标签: applescript

早上好,

我正在尝试编写一个可以运行的AppleScript,它会将我桌面上的所有文件发送到Evernote,然后删除这些文件。我迄今为止的代码是:

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

如果我运行这个,那么第一个和最后一个'tell'工作正常(即脚本突出显示然后删除桌面上的所有文件),但中间'tell'没有做任何事情。

但是,如果我手动突出显示桌面上的所有文件,然后只运行中间的“告诉”,那么它会导入正常 - 每个项目都按照预期单独注释。

正如您所知,我是AppleScript的新手 - 我怀疑我需要将所选文件放入某种类型的数组中,但无法弄明白。救命啊!

非常感谢

2 个答案:

答案 0 :(得分:3)

您的代码失败,因为您的input变量与通过Finder选择的文件之间没有关系 - 这意味着您的列表为空,而Evernote根本不处理任何内容。您通过在try块中包装Evernote导入命令而没有任何错误处理来解决问题,这意味着所有错误都不会被忽视(为了避免这种问题,这是一个好习惯总是在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>更容易,并且会使您远离嵌套的上下文地狱

感谢@adayzone对列表处理和别名强制进行更正

答案 1 :(得分: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

相关问题