使用Growl 2.1规则运行Applescript

时间:2013-08-18 02:57:00

标签: applescript dropbox growl

目前,我有一个流程设置导致文件 - 'cb.txt' - 被添加到我的Dropbox中。我收到了应用程序名称“Dropbox”和Note Title“cb.txt added”的Growl通知。我想要'cb.txt added'通知运行一个applescript,它会将文本从cb.txt复制到我的剪贴板。可以找到Growl通知的规则here

这是我想要运行的AppleScript(当我通过Applescript自行运行它时,它成功地将cb.txt的内容添加到剪贴板中):

set the_file to "HardDrive:Users:Me:Dropbox:Folder:cb.txt"
set the_text to (do shell script "cat " & quoted form of (POSIX path of the_file))
set the clipboard to the_text

我已将此文件保存为~/Library/Application Scripts/com.Growl.GrowlHelperApp作为Apple脚本。我还使用此代码保存了另一个版本:

using terms from application "Growl"
    on perform action with notification
        ignoring case
            if notification's app name is Dropbox then
                if notification's note title contains "cb.txt added" then
                    set the_file to "Macintosh HD:Users:Caleb:Dropbox:Apps:CBApp:cb.txt"
                    set the_text to (do shell script "cat " & quoted form of (POSIX path of the_file))
                    set the clipboard to the_text
                end if
            end if
        end ignoring
    end perform action
end using terms from

以前的脚本在运行时不执行任何操作。我把它保存为Rules.scpt:

using terms from application "Growl"
    on evaluate notification with notification
    --Rules go in here
    --Ultimately return what you want Growl to do with the notification
    end evaluate notification
end using terms from

显然,我有点卡住了。如果有人能够在收到特定的Growl通知时给我建议如何让我的工作AppleScript代码运行,我将不胜感激!

1 个答案:

答案 0 :(得分:0)

您可以使用应用名称,但如果您不在字符串周围加上引号,则会将其视为变量。

如果在尝试访问/使用它之前未声明变量,则会出现错误。

这就是发生在你身上的事。你可以在Consol.app

中看到这个
  

18/08/2013 11:46:11.961 Growl [89225]:完成错误:错误   Domain = NSPOSIXErrorDomain Code = 2“操作不能   完成...... /图书馆/申请   Scripts / com.Growl.GrowlHelperApp / Rules.scpt:执行错误:   变量Dropbox未定义。 (-2753)}

将字符串Dropbox放在引号中将解决此问题。

这是我使用的工作测试。

    using terms from application "Growl"

    on evaluate notification with notification

        if notification's app name contains "Image File" then
            if notification's note title contains "jpeg Error" then
                do shell script "echo " & quoted form of note type of notification & " >  ~/notification.txt "

            end if
        end if


        if notification's app name is "Dropbox" then

        if notification's note title contains "cb" then
            set the_file to ("cb.txt") as string
            set the_text to (do shell script "cat ~/Dropbox/" & quoted form of the_file)
            set the clipboard to the_text
            do shell script "echo " & quoted form of the_text & " >  ~/dbnotification.txt "
        end if
    end if
    end evaluate notification
end using terms from

这也向您展示了如何测试不同的规则

注** 通常我会得到用户主文件夹:

set homePath to (path to home folder from user domain) as text 

但是,在growl中似乎存在一个返回growl文件夹的错误

  

com.Growl.GrowlHelperApp / Rules.scpt:执行错误:cat:   /Users/USERNAME/Library/Containers/com.Growl.GrowlHelperApp/Data/Dropbox/cb.txt:   没有这样的文件或目录(1)