当想要从一组文件中随机选择时,要将其内容作为电子邮件发送,同时确保每个文件的内容仅使用一次,则需要维护已经从其发送内容的文件名列表。可以创建,访问和维护此列表的方式有很多种。
从避而远之的Automator,并使用AppleScript的整个过程中,最简单的是作为一个AppleScript的 property
变量,但是一个AppleScript的 property
变量被重置,如果脚本被重新编译的脚本编辑器和或再次保存它最初被后保存。
这不一定是一个问题,如果一旦脚本已经正确配置,以满足需求和测试过程中运行正常,则然后保存为一个应用程序,而不是修改或再保存它的初始运行后,已保存为申请。如果这可能是一个问题,那么列表将需要保存为磁盘文件。(就个人而言,我选择使用磁盘文件。)
保存已经通过电子邮件将内容作为磁盘文件的文件名列表也可以通过多种方式完成。您基本上需要选择一种可能的方法并相应地对其进行编码。
以下AppleScript 代码示例显示了完成整个任务的两种方法。第一个是使用AppleScript property
变量,第二个是使用磁盘文件来维护列表。
- 注:在我的意见之一,我问过了,这样做的原因是,因为“做文件必须是格式文本,也可以是纯文本?” 纯文本文件不需要在被打开GUI来检索内容,与Rich Text一样,需要在.eg TextEdit中打开以检索内容。不必通过GUI进行操作更快,视觉上不那么突兀。此外,如果使用“ 获取TextEdit文档的内容”操作在Automator中完成此操作,则无论如何都会以纯文本形式检索内容。因此,使用纯文本文件实际上是有利的 开始使用富文本。这些示例编码为使用Rich Text和TextEdit,如果使用纯文本,则会在之后显示修改。
在第一个AppleScript 代码示例中,您需要设置一些变量:
thisFolder
- 包含100个文件的文件夹的位置。
theRecipientName
- 电子邮件收件人的姓名。
theRecipientAddress
- 电子邮件收件人的电子邮件地址。
theSubject
- 电子邮件的主题。
在第二个示例AppleScript 代码中,除了上面显示的变量之外:
theAlreadySentListFilename
- 包含已从中发送内容的文件名的文件的位置和名称。
脚本的其余部分以一种方式(标记化)进行编码,以处理这些变量以及脚本中设置的其他变量。此外,块的代码进行编码,自动发送信息,但它已被注释掉,直到测试完成,或者如果不想自动发送。取消注释。tell application "Mail"
要使用这个脚本,在脚本编辑器,创建一个新文档,并将其保存为一个应用程序在/应用程序,命名它,如100个信息给Send.app。
接下来,将下面的示例AppleScript 代码复制并粘贴到脚本编辑器中的100条Send to Send.app中,然后单击工具栏上的“ 编译” 按钮。
修改上面提到的变量。
从脚本编辑器中保存应用程序并进行测试。
完成测试后,点击工具栏上的编译 按钮将AppleScript 变量重置为空列表,然后保存并关闭100个消息到Send.app,以便在生产模式开始后不重置AppleScript 变量。theAlreadySentList
property
theAlreadySentList
property
第一个示例AppleScript 代码:
-- ### Folder Path Variable
-- # Set the location of the folder containing the 100 files.
-- # NOTE: All 100 files must be in the same individual folder.
set thisFolder to (path to documents folder as text) & "100 Messages to Send"
-- ### Mail Variables
set theRecipientName to "John Doe"
set theRecipientAddress to "johndoe@domain.com"
set theSubject to "Hello World"
-- # Leave the following as 'missing value'.
set theMessageContent to missing value
-- ##### DO NOT modify below this line unless necessary. #####
-- ### Some Other Variables
-- # The property variable 'theAlreadySentList', is a list of
-- # filenames from which the messages have already been sent.
-- # The value of this property is maintained so long as
-- # this script is not save again or recompiled after it
-- # has been originally saved for the first time.
property theAlreadySentList : {}
global thisFile
-- ### Handler
on chooseRandomFileFrom(thisFolder)
tell application "Finder"
set thisFile to some file of container thisFolder
end tell
return thisFile
end chooseRandomFileFrom
-- # Get the count of entries in the 'theAlreadySentList' list.
-- # This is used to display a message and break the 'repeat' loop
-- # once all 100 messages have been sent.
set theCount to (count of theAlreadySentList) as integer
-- # Choose a file at random.
set thisFileName to name of chooseRandomFileFrom(thisFolder)
-- # Evaluate the sent status of 'thisFileName' and respond accordingly.
repeat -- 200 times -- # Uncomment if you want a failsafe out of the 'repeat' loop.
if theCount is equal to 100 then
display dialog "All 100 messages have already been sent!" buttons {"OK"} default button 1
exit repeat
else if thisFileName is in theAlreadySentList then
-- # Choose another file at random.
set thisFileName to name of chooseRandomFileFrom(thisFolder)
else
-- # This file has not been used yet, process accordingly.
set end of theAlreadySentList to thisFileName
tell application "TextEdit"
open thisFile
activate
delay 1 --# Adjust as necessary. Time must be given for the file to open before proceeding.
set theMessageContent to text of front document
close front document
end tell
tell application "Mail"
activate
set theMessage to make new outgoing message with properties {visible:true, subject:theSubject, content:theMessageContent}
tell theMessage
make new to recipient at end of to recipients with properties {name:theRecipientName, address:theRecipientAddress}
end tell
-- delay 1 --# Adjust as necessary. Uncomment to use this command.
-- send theMessage --# Uncomment to use this command.
end tell
exit repeat
end if
end repeat
在第二个AppleScript 代码示例中,磁盘文件用于存储已从中发送内容作为电子邮件消息的文件名列表。
要使用这个脚本,在脚本编辑器,创建一个新文档,并将其保存为一个应用程序在/应用程序,命名它,如100个信息给Send.app。这样做是为了您可以将纯文本文件添加到应用程序包中,以用于已发送的文件名列表。(我更喜欢在应用程序包中保留相关的支持文件。)
将文档保存为应用程序后,现在打开应用程序包(在Finder中,右键单击“ 显示包内容”)到“ 内容/资源 ”,然后创建例如“已发送消息文件名List.txt”文件作为空文件,然后关闭应用程序包。
接下来,将下面的示例AppleScript 代码复制并粘贴到脚本编辑器中的100个Messages to Send.app中,然后单击工具栏上的“ 编译” 按钮。
修改变量,如上所述。
从脚本编辑器中保存应用程序并进行测试。
完成测试后,清除已发送消息文件名List.txt中的条目,因此在生产模式下首次运行应用程序时,它是一个空文件。
第二个例子AppleScript 代码:
-- ### Folder Path Variable
-- # Set the location of the folder containing the 100 files.
-- # NOTE: All 100 files must be in the same individual folder.
set thisFolder to (path to documents folder as text) & "100 Messages to Send"
-- ### Mail Variables
set theRecipientName to "John Doe"
set theRecipientAddress to "johndoe@domain.com"
set theSubject to "Hello World"
-- # Leave the following as 'missing value'.
set theMessageContent to missing value
-- ### Sent Messages Filename List Location Variable
-- # Set the location and name of the file containing the
-- # filenames from which the content has already been sent.
set myPath to POSIX path of (path to me as string)
set theAlreadySentListFilename to myPath & "Contents/Resources/Sent Messages Filename List.txt"
-- ##### DO NOT modify below this line unless necessary. #####
-- ### Some Other Variables
global thisFile
global theAlreadySentList
set theAlreadySentList to {}
-- ### Handlers
on buildListFromDiskFile(thisDiskFile)
set thisContent to do shell script "cat " & quoted form of thisDiskFile
repeat with thisParagraph in paragraphs of text in thisContent
set end of theAlreadySentList to (thisParagraph as text)
end repeat
end buildListFromDiskFile
on chooseRandomFileFrom(thisFolder)
tell application "Finder"
set thisFile to some file of container thisFolder
end tell
return thisFile
end chooseRandomFileFrom
-- # Load data from disk file for: 'theAlreadySentList'
buildListFromDiskFile(theAlreadySentListFilename)
-- # Get the count of entries in the 'theAlreadySentList' list.
-- # Used to display a message once all 100 messages have been sent.
set theCount to (count of theAlreadySentList) as integer
-- # Choose a file at random.
set thisFileName to name of chooseRandomFileFrom(thisFolder)
-- # Evaluate the sent status of 'thisFileName' and respond accordingly.
repeat -- 200 times -- # Uncomment if you want a failsafe out of the 'repeat' loop.
if theCount is equal to 100 then
display dialog "All 100 messages have already been sent!" buttons {"OK"} default button 1
exit repeat
else if thisFileName is in theAlreadySentList then
-- # Choose another file.
set thisFileName to name of chooseRandomFileFrom(thisFolder)
else
-- # This file has not been used yet, process accordingly.
-- #
-- # Add the filename to the list.
do shell script "echo " & thisFileName & " >> " & quoted form of theAlreadySentListFilename
-- # Get the content of the Rich Text file.
tell application "TextEdit"
open thisFile
activate
delay 1 --# Adjust as necessary. Time must be given for the file to open before proceeding.
set theMessageContent to text of front document
close front document
end tell
-- # Create the Mail Message (and Send it).
tell application "Mail"
activate
set theMessage to make new outgoing message with properties {visible:true, subject:theSubject, content:theMessageContent}
tell theMessage
make new to recipient at end of to recipients with properties {name:theRecipientName, address:theRecipientAddress}
end tell
-- delay 1 --# Adjust as necessary. Uncomment to use this command.
-- send theMessage -- # Uncomment to use this command.
end tell
exit repeat
end if
end repeat
注意:如果你要使用纯文本文件,而不是富文本文件,然后整个tell application "TextEdit"
块的代码可以与以下两行被替换代码:
set thisFile to POSIX path of (thisFolder & ":" & thisFileName)
set theMessageContent to do shell script "cat " & quoted form of thisFile
您还可以更改其上方的注释-- # Get the content of the Rich Text file.
,将Rich与Plain交换。