【问题标题】:Run Applescript with Growl 2.1 Rules使用 Growl 2.1 规则运行 Applescript
【发布时间】:2013-08-20 03:44:04
【问题描述】:

目前,我设置了一个流程,该流程会导致一个文件——“cb.txt”——被添加到我的 Dropbox。我收到一条 Growl 通知,其中包含应用程序名称“Dropbox”和注释标题“cb.txt added”。我希望'cb.txt added'通知运行一个applescript,它将文本从cb.txt复制到我的剪贴板。咆哮的通知规则可以在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。我还使用此代码保存了另一个版本:

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 代码提供建议,我将不胜感激!

【问题讨论】:

  • 我在该教程页面中没有看到他们提到应用程序名称的任何地方,您是否尝试过不使用该 if 语句?另外,你试过把“Dropbox”放在引号里吗?
  • 嗨@josh,Growl 页面没有更好地显示这一点并不让我感到惊讶。我有时会发现他们的页面缺乏细节。因为您使用的是应用程序“Growl”中的术语,所以您可以使用咆哮 sdef 的任何属性,只要它们以正确的方式使用。如果您不能使用应用名称,那么评估多个规则将更加困难。

标签: applescript dropbox growl


【解决方案1】:

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

如果变量在你尝试访问/使用它之前没有声明,你会得到一个错误。

这就是发生在你身上的事情。您可以在 Consol.app 中看到这一点

18/08/2013 11:46:11.961 咆哮[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 

但是在咆哮中似乎有一个错误,返回一个咆哮文件夹

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

【讨论】:

  • 非常感谢@markhunte,成功了。我需要做一些测试来确定我需要保留 Growl 文件夹中的许多脚本中的哪一个,但它现在可以正常工作。我在 iPhone 上复制文本,在Pythonista 中运行“粘贴”脚本,然后文本就可以在我的 OS X 剪贴板上使用。不错!
  • @TerryMcCall,建议进行编辑,暗示返回咆哮文件夹而不是用户主文件夹的错误是由于沙盒。 since Growl is now sandboxed, it returns a growl folder。这也许就是这种情况。但我将此归类为错误而不是预期的行为。将脚本放在com.Growl.GrowlHelperApp 文件夹中意味着用户同意脚本正常运行并与用户环境交互。我不能说这是苹果的错还是咆哮。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-08-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-10-12
  • 2013-10-12
相关资源
最近更新 更多