如何使用 applescript 在 os x 中自定义通知

编程入门 行业动态 更新时间:2024-10-09 08:37:13
本文介绍了如何使用 applescript 在 os x 中自定义通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

假设我有一个文件 sometext.txt .我希望这个 txt 文件的内容显示在我将使用 applescript 中的 displaynotification 触发的通知中.

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:

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

我该怎么做?假设 sometext.txtapplescript 文件在同一个目录下.

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.

注意
在执行之前,请保存脚本,因为当脚本未保存时,组合路径指向~/Library/Autosave Information/"文件夹(未保存脚本所在的位置)甚至指向/Applications/Utilities/"(脚本所在的位置)- 编辑是).

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).

用法
脚本发出哔哔声(当 'errBeep' 为真时)如果没有100% 正常并在通知中显示状态.

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

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

这里是简短版本:

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屋!

更多推荐

[db:关键词]

本文发布于:2023-04-17 14:49:26,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/915762.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:自定义   如何使用   通知   os   applescript

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!