How to set up notification in os x using applescript

Let's say I have a file sometext.txt

. I want the content of this txt file to be displayed in a notification that I will call using displaynotification

in applescript

.

In other words, I want to be able to:

display notification "File content" with title "Title"

...

How can i do this? Let's assume that sometext.txt

both applescript

are in the same directory.

+3


source to share


2 answers


TASK
To launch a notification with text from a file that is in the same folder as this script.

Note
Save the script before executing, because when the script is not saved, the compiled path points to the "~ / Library / Autosave Information /" folder (where the unsaved scripts are located) or even "/ Applications / Utilities /" (where Script -Editor ).

USE
script beeps (when "errBeep" is true) if something was not 100% ok and displays the status in the notification.

  • when no text file is found, the script asks for automatic creation.
  • If the text file is empty, you get a notification and it opens the file.
  • If the text length is longer than allowedCharactersCount

    (default 65), some action may be taken before the notification is triggered.
(*  *** 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's a 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

      

+2


source


Assuming the file content is less than 20 characters.



Set theContents to read "/posix/path/to/your/file" as ยซclass utf8ยป
display notification theContents with title "Title"

      

+1


source







All Articles