如何使用AppleScript定制的OS X的通知 [英] How to customise notification in os x using applescript

查看:98
本文介绍了如何使用AppleScript定制的OS X的通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

说我有一个文件 sometext.txt 。我想这个txt文件的内容,我会触发使用 displaynotification 的AppleScript 。<该通知露面/ p>

在换句话说,我希望能够为:

显示的通知文件的内容的标题为标题

我该怎么办呢?假设该 sometext.txt 的AppleScript 文件在同一目录下。


解决方案

  

目标结果
  要驻留在一个文件中火带文本的通知
  同一文件夹此脚本。


  
  

请注意结果
  执行前,由路径点保存脚本,因为当脚本未保存到〜/资源库/自动转存信息/文件夹(未保存脚本的地方),或甚至/应用程序/实用/(其中脚本 - 编者是)。


  
  

用法结果
  该脚本发出蜂鸣声(当'errBeep'是真实的)如果事情是不是
  100%确定,并显示在通知的状态。


  
  

      
  • 当文本​​文件中找不到脚本询问自动创建。

  •   
  • 如果该文本文件是空的,你得到通知,并打开文件。

  •   
  • 如果文本的长度大于在 allowedCharactersCount (默认65),可以烧制通知之前执行的一些操作。

  •   

 (* ***请定制相应部分*** *)
- ---------------------------------
- 嘟时出错
- ---------------------------------
设置errBeep为真
--set errBeep为假 - ---------------------------------
- 文件名和放大器;
- 数允许的字符:
- ---------------------------------
文件名设置为messages.txt
设置allowedCharactersCount 65 - ---------------------------------
- 通知标题:
- ---------------------------------
设置notificationTitle为来源:&放大器;文件名 - ---------------------------------
- CUSTOMIZING END
- --------------------------------- - ---------------------------------
- 撰写文件路径
- ---------------------------------
文件路径设置为我composeFilePath(文件名)
如果文件路径为那么
    如果errBeep然后发出蜂鸣声
    返回
万一 - ---------------------------------
- 检查文件是否存在?
- ---------------------------------
设置filePathExists我FILEEXISTS(文件路径)如果没有filePathExists然后     - ------------------------------------
     - 该文件不存在,询问用户是否应该创建(打开):
     - ------------------------------------
    如果errBeep然后发出蜂鸣声
    设置消息文件&放大器;报价文件名和放大器的形式; 在与&amp;返回&安培;返回&安培;引用文件路径和放大器的形式;返回&安培;返回&安培; 不见了。
    显示对话框消息按钮{创建和放大器;打开,取消}取消按钮2默认按钮2题20后放弃哪里是&放大器;报价文件名和放大器的形式; ?使用图标2
    如果结果的返回按钮,启动创建,然后
        我readFromFile(文件路径) - 此创建文件
        告诉应用程序发现者,打开文件路径项 - 打开要编辑
    万一    返回 - 我们做什么,我们可以万一 - ---------------------------------------
- 找到该文件,现在阅读:
- ---------------------------------------设置textFromFile我readFromFile(文件路径)如果textFromFile不是,则     - ------------------------------------------------ ---------
     - 发现的内容,我们准备射击我们的通知
     - ------------------------------------------------ ---------     - -----------------------------------
     - •但首先应该检查长度•
     - -----------------------------------
    设置在countCharactersInTextFromFile向textFromFile字符计数 - 计数包括回字
    如果(countCharactersInTextFromFile)大于allowedCharactersCount大于         - -----------------------------------
         - •长度不OK
         - 更多的字符允许的。该怎么办 ?在这里,我们只是提示音及放大器;更改消息和标题
         - -----------------------------------
        如果errBeep然后发出蜂鸣声
        设置notificationTitle为错误:&放大器; ((countCharactersInTextFromFile - allowedCharactersCount)文本)及字溢出
        设置notificationText为(countCharactersInTextFromFile文本)及在textFromFile字符,与&amp;返回&安培; (allowedCharactersCount文本)及字符。    其他         - -----------------------------------
         - •长度正常•
         - -----------------------------------
        设置notificationText为textFromFile    万一
     - ------------------------------------------------ ---------
     - 消防通知
     - ------------------------------------------------ ---------    显示通知notificationText标题为notificationTitle     - ------------------------------------------------ ---------
其他     - ------------------------------------------------ ---------
     - 文件是空的!更换相应的操作如下:
     - ------------------------------------------------ ---------
    如果errBeep然后发出蜂鸣声
    显示通知***没有内容文件***标题为错误:空文件
    告诉应用程序发现者,打开文件路径项 - 打开要编辑万一
- ------------------------------------------------ ---------
- 子例程
- ------------------------------------------------ --------- - ---------------------------------
- 文件是否存在?
- ---------------------------------
在FILEEXISTS(文件路径)
    集filePathExists为假
    告诉应用程序发现者
        尝试
            项文件路径存在设置filePathExists
        年底试
    告诉结束
    返回filePathExists
结束FILEEXISTS - ---------------------------------
- composeFilePath(文件名)
- ---------------------------------
在composeFilePath(文件名)
    如果文件名是,然后返回
    设置pathToMe为路径,我 - 这是这个脚本的完整路径
     - 得到这个脚本是在文件夹:
    thisScriptsFolder设置为
    告诉应用程序发现者
        尝试
            设置thisScriptsFolder以(获取pathToMe的容器)文本
        年底试
    告诉结束
    如果thisScriptsFolder是,那么
        返回
    万一
    返回thisScriptsFolder&安培;文件名 - 完整路径
结束composeFilePath - ------------------------------------------------ ---------
- readFromFile(的资源文件)
- ------------------------------------------------ ---------
在readFromFile(的资源文件)
    尝试
        为源设置为源为字符串
        设置open_sourceFile以打开访问文件的资源文件
        设置FILEDATA阅读open_sourceFile
        关闭访问open_sourceFile
        回报FILEDATA
    在错误的ERROR_MESSAGE数ERROR_NUMBER
        尝试
            关闭访问文件的资源文件
        年底试
         - 对话框显示错误:&放大器;在ERROR_NUMBER和放大器; &放大器。在ERROR_MESSAGE按钮{取消}默认按钮1
        返回
    年底试
结束readFromFile

下面的短版:

 将filename设置为messages.txt
文件路径设置为我composeFilePath(文件名)显示通知我readFromFile(文件路径),标题为从:&放大器;文件名在readFromFile(的资源文件)
    尝试
        为源设置为源为字符串
        设置open_sourceFile以打开访问文件的资源文件
        设置FILEDATA阅读open_sourceFile
        关闭访问open_sourceFile
        回报FILEDATA
    在错误的ERROR_MESSAGE数ERROR_NUMBER
        尝试
            关闭访问文件的资源文件
        年底试
        返回
    年底试
结束readFromFile在composeFilePath(文件名)
    如果文件名是,然后返回
    设置pathToMe为路径,我 - 这是这个脚本的完整路径
     - 得到这个脚本是在文件夹:
    thisScriptsFolder设置为
    告诉应用程序发现者
        尝试
            设置thisScriptsFolder以(获取pathToMe的容器)文本
        年底试
    告诉结束
    如果thisScriptsFolder是,那么
        返回
    万一
    返回thisScriptsFolder&安培;文件名 - 完整路径
结束composeFilePath

Say I have a file sometext.txt . I want the contents of this txt file to show up in the notification which I will trigger using displaynotification in applescript.

In other words I want to be able to:

display notification "File's content" with title "Title".

How can I do it? Assume that sometext.txt and the applescript file are in the same directory.

解决方案

GOAL
To fire a notification with text from a file that resides in the same folder as this script.

NOTE
Before execution, save the script because when the script is unsaved, the composed path points to the „~/Library/Autosave Information/„ folder (the place where unsaved scripts are) or even to "/Applications/Utilities/" (where Script-Editor is).

USAGE
The script beeps (when 'errBeep' is true) if something was not 100% ok and displays the status in a notification.

  • when the text file isn't found the script asks for automatic creation.
  • if the text file is empty you get notified and it opens the file.
  • if the length of the text is greater as in allowedCharactersCount (default 65), some action can be performed before firing the notification.

(*  *** please customize the appropriate parts *** *)


-- ---------------------------------
-- BEEP when error
-- ---------------------------------
set errBeep to true
--set errBeep to false



-- ---------------------------------
-- file name & 
-- number of allowed characters:
-- ---------------------------------
set fileName to "messages.txt"
set allowedCharactersCount to 65



-- ---------------------------------
-- Notification title:
-- ---------------------------------
set notificationTitle to "From: " & fileName



-- ---------------------------------
-- END CUSTOMIZING
-- ---------------------------------





-- ---------------------------------
--  compose file path 
-- ---------------------------------
set filePath to my composeFilePath(fileName)
if filePath is "" then
    if errBeep then beep
    return
end if

-- ---------------------------------
--  check file existence ? 
-- ---------------------------------
set filePathExists to my fileExists(filePath)

if not filePathExists then

    -- ------------------------------------
    -- The file isn't there, ask the user if it should be created (and opened):
    -- ------------------------------------
    if errBeep then beep
    set message to "File " & quoted form of fileName & " at " & return & return & quoted form of filePath & return & return & "is missing."
    display dialog message buttons {"Create & Open", "Cancel"} cancel button 2 default button 2 giving up after 20 with title "Where is " & quoted form of fileName & "?" with icon 2
    if button returned of the result starts with "Create" then
        my readFromFile(filePath) -- this creates the file
        tell application "Finder" to open item filePath -- open for edit
    end if

    return -- we did what we could

end if



-- ---------------------------------------
-- Found the file, now read it:
-- ---------------------------------------

set textFromFile to my readFromFile(filePath)

if textFromFile is not "" then

    -- ---------------------------------------------------------
    -- Found content, we are ready to fire our notification
    -- ---------------------------------------------------------

    -- -----------------------------------
    --  • but first check length •
    -- -----------------------------------
    set countCharactersInTextFromFile to count characters in textFromFile -- count includes the "return" characters
    if (countCharactersInTextFromFile) is greater than allowedCharactersCount then

        -- -----------------------------------      
        -- • Length is NOT OK
        -- More characters as allowed. What to do ? Here, we just beep & change the message and title
        -- -----------------------------------      
        if errBeep then beep
        set notificationTitle to "ERROR: " & ((countCharactersInTextFromFile - allowedCharactersCount) as text) & " characters overflow"
        set notificationText to (countCharactersInTextFromFile as text) & " characters in textFromFile," & return & (allowedCharactersCount as text) & " characters are allowed."

    else

        -- -----------------------------------      
        -- • Length is OK • 
        -- -----------------------------------      
        set notificationText to textFromFile

    end if


    -- ---------------------------------------------------------
    -- Fire the notification 
    -- ---------------------------------------------------------

    display notification notificationText with title notificationTitle

    -- ---------------------------------------------------------


else

    -- ---------------------------------------------------------
    -- File is empty! Replace following lines with appropriate action:
    -- ---------------------------------------------------------
    if errBeep then beep
    display notification "*** NO TEXT IN THIS FILE ***" with title "ERROR: EMPTY FILE"
    tell application "Finder" to open item filePath -- open for edit

end if




-- ---------------------------------------------------------
-- Sub Routines
-- ---------------------------------------------------------

-- ---------------------------------
--  file existence ? 
-- ---------------------------------
on fileExists(filePath)
    set filePathExists to false
    tell application "Finder"
        try
            set filePathExists to item filePath exists
        end try
    end tell
    return filePathExists
end fileExists

-- ---------------------------------
-- composeFilePath(fileName)
-- ---------------------------------
on composeFilePath(fileName)
    if fileName is "" then return ""
    set pathToMe to path to me -- this is the full path to this script
    -- get the folder this script is in:
    set thisScriptsFolder to ""
    tell application "Finder"
        try
            set thisScriptsFolder to (get container of pathToMe) as text
        end try
    end tell
    if thisScriptsFolder is "" then
        return ""
    end if
    return thisScriptsFolder & fileName -- full path
end composeFilePath

-- ---------------------------------------------------------
-- readFromFile(sourceFile)
-- ---------------------------------------------------------
on readFromFile(sourceFile)
    try
        set the sourceFile to the sourceFile as string
        set the open_sourceFile to open for access file sourceFile
        set fileData to read the open_sourceFile
        close access the open_sourceFile
        return fileData
    on error the error_message number the error_number
        try
            close access file sourceFile
        end try
        -- display dialog "Error: " & the error_number & ". " & the error_message buttons {"Cancel"} default button 1
        return ""
    end try
end readFromFile

Here the short version:

set fileName to "messages.txt"
set filePath to my composeFilePath(fileName)

display notification my readFromFile(filePath) with title "From: " & fileName

on readFromFile(sourceFile)
    try
        set the sourceFile to the sourceFile as string
        set the open_sourceFile to open for access file sourceFile
        set fileData to read the open_sourceFile
        close access the open_sourceFile
        return fileData
    on error the error_message number the error_number
        try
            close access file sourceFile
        end try
        return ""
    end try
end readFromFile

on composeFilePath(fileName)
    if fileName is "" then return ""
    set pathToMe to path to me -- this is the full path to this script
    -- get the folder this script is in:
    set thisScriptsFolder to ""
    tell application "Finder"
        try
            set thisScriptsFolder to (get container of pathToMe) as text
        end try
    end tell
    if thisScriptsFolder is "" then
        return ""
    end if
    return thisScriptsFolder & fileName -- full path
end composeFilePath

这篇关于如何使用AppleScript定制的OS X的通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆