如何使用AppleScript自定义OS X中的通知

3
我有一个名为sometext.txt的文件。我想要在使用applescript中的displaynotification触发通知时,显示这个txt文件的内容。
换句话说,我想要做到以下几点:

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

如何实现呢?假设sometext.txtapplescript文件在同一目录下。
2个回答

2

目标
从与此脚本位于同一文件夹中的文件中获取文本并触发通知。

注意
在执行之前,请保存脚本,因为当脚本未保存时,组合的路径指向“〜/ Library / Autosave Information /”文件夹(未保存脚本的位置)或甚至是“/ Applications / Utilities /”(Script-Editor所在的位置)。

用法
如果出现问题,并且 'errBeep' 为 true,则脚本会发出哔声并在通知中显示状态。

  • 如果找不到文本文件,则脚本将请求自动创建。
  • 如果文本文件为空,则会收到通知并打开该文件。
  • 如果文本长度大于 allowedCharactersCount(默认为65),则在触发通知之前可以执行某些操作。
(*  *** 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

这里是简短版本:
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

composeFilePath 中的更改,我将get folder of … 更改为 get container of …。我完全忘记了,但是 https://dev59.com/O4vda4cB1Zd3GeqPgPIg#30824132 使我想起来了。 - user3577225

1
假设文件内容不超过20个字符。
Set theContents to read "/posix/path/to/your/file" as «class utf8»
display notification theContents with title "Title"

假设它超过20个字符,那么怎么办? - l'L'l
超过20-25个将被截断。显示通知中只有很少的空间。你很快就会看到的。顺便说一下,如果这回答解决了你的问题,你可以将其标记为有效,方法是在我的答案左侧某个地方点击。并且,通过从显示通知中删除标题,您可能会获得更多的空间。 - McUsr
我遇到了一个“无法将类型文件转换为”错误。我的文件在桌面上,名为“dummy.txt”。我认为我在设置文件路径方面做错了什么。 - Ankit Sultana
尝试将set pxPath to POSIX path of ((path to desktop folder as text) & "dummy.txt" as alias)翻译为中文。 - McUsr

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接