说我有一个文件sometext.txt
。我想这个txt文件的内容中,我会触发使用通知展现出来displaynotification
的applescript
。
换句话说,我希望能够:
display notification "File's content" with title "Title"
。
我该怎么做?假定sometext.txt
和applescript
文件位于同一目录中。
目标
触发来自与此脚本位于同一文件夹中的文件中的文本的通知。注意
在执行之前,请保存脚本,因为未保存脚本时,组合路径指向“〜/ 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] 删除。
我来说两句