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

安吉·苏丹娜(Ankit Sultana)

说我有一个文件sometext.txt我想这个txt文件的内容中,我会触发使用通知展现出来displaynotificationapplescript

换句话说,我希望能够:

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

我该怎么做?假定sometext.txtapplescript文件位于同一目录中。

用户名

目标
触发来自与此脚本位于同一文件夹中的文件中的文本的通知。

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

用法
如果某些事情不是100%正确,脚本会发出哔声(“ 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

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

如何在自定义通知textview中设置值

来自分类Dev

如何在Android中创建自定义通知

来自分类Dev

如何在自定义通知textview中设置值

来自分类Dev

如何在 Android 的通知中显示自定义文本

来自分类Dev

如何在 Dynamics 中创建自定义通知

来自分类Dev

如何使用FCM在ios推送通知中播放自定义通知声音

来自分类Dev

使用自定义视图创建自定义通知

来自分类Dev

Spritekit OS X如何使用自定义字体

来自分类Dev

OS X Mavericks中的自定义登录/锁定屏幕

来自分类Dev

OS X 10.10中的自定义NSControl类

来自分类Dev

如何在 django-oscar 通知系统中自定义通知消息

来自分类Dev

如何自定义屏幕通知?

来自分类Dev

如何自定义平视通知的布局?

来自分类Dev

如何发送自定义桌面通知?

来自分类Dev

如何发送自定义桌面通知?

来自分类Dev

如何制作自定义通知?

来自分类Dev

如何自定义平视通知的布局?

来自分类Dev

如何自定义屏幕通知?

来自分类Dev

使用ParsePushBroadcastReceiver自定义BigTextStyle通知

来自分类Dev

使用引导程序的自定义通知

来自分类Dev

使用R中的日期自定义x轴标签

来自分类Dev

使用自定义通知布局时获取空白通知

来自分类Dev

我们如何制作和放置自定义通知,该通知随更改自定义变量而启动?(在SwiftUI或UIKit中)

来自分类Dev

如何在openFrameworks中自定义OS X菜单栏选项?

来自分类Dev

如何在Mac OS X应用程序中嵌入自定义可可框架?

来自分类Dev

如何在OS X中制作自定义键盘布局?

来自分类Dev

如何在openFrameworks中自定义OS X菜单栏选项?

来自分类Dev

如何在可可 (OS X) 中以编程方式自定义工具提示?

来自分类常见问题

如何在Exoplayer通知中设置自定义布局和字体?

Related 相关文章

  1. 1

    如何在自定义通知textview中设置值

  2. 2

    如何在Android中创建自定义通知

  3. 3

    如何在自定义通知textview中设置值

  4. 4

    如何在 Android 的通知中显示自定义文本

  5. 5

    如何在 Dynamics 中创建自定义通知

  6. 6

    如何使用FCM在ios推送通知中播放自定义通知声音

  7. 7

    使用自定义视图创建自定义通知

  8. 8

    Spritekit OS X如何使用自定义字体

  9. 9

    OS X Mavericks中的自定义登录/锁定屏幕

  10. 10

    OS X 10.10中的自定义NSControl类

  11. 11

    如何在 django-oscar 通知系统中自定义通知消息

  12. 12

    如何自定义屏幕通知?

  13. 13

    如何自定义平视通知的布局?

  14. 14

    如何发送自定义桌面通知?

  15. 15

    如何发送自定义桌面通知?

  16. 16

    如何制作自定义通知?

  17. 17

    如何自定义平视通知的布局?

  18. 18

    如何自定义屏幕通知?

  19. 19

    使用ParsePushBroadcastReceiver自定义BigTextStyle通知

  20. 20

    使用引导程序的自定义通知

  21. 21

    使用R中的日期自定义x轴标签

  22. 22

    使用自定义通知布局时获取空白通知

  23. 23

    我们如何制作和放置自定义通知,该通知随更改自定义变量而启动?(在SwiftUI或UIKit中)

  24. 24

    如何在openFrameworks中自定义OS X菜单栏选项?

  25. 25

    如何在Mac OS X应用程序中嵌入自定义可可框架?

  26. 26

    如何在OS X中制作自定义键盘布局?

  27. 27

    如何在openFrameworks中自定义OS X菜单栏选项?

  28. 28

    如何在可可 (OS X) 中以编程方式自定义工具提示?

  29. 29

    如何在Exoplayer通知中设置自定义布局和字体?

热门标签

归档