【发布时间】:2020-01-14 13:47:42
【问题描述】:
这段代码几乎可以工作。 :) 基本上我正在尝试压缩文件,然后将该电子邮件附加到新邮件中。
该过程在 automator 中启动,然后脚本运行。除了文件的实际附加之外,一切正常。
我对 AppleScript 的了解非常有限,但我试图解决这个问题。我结合了不同的代码 sn-ps 来达到这个目标。
on run {input, parameters}
set display_text to "Please enter your password:"
repeat
considering case
set init_pass to text returned of (display dialog display_text default answer "" with hidden answer)
set final_pass to text returned of (display dialog "Please verify your password below." buttons {"OK"} default button 1 default answer "" with hidden answer)
if (final_pass = init_pass) then
exit repeat
else
set display_text to "Mismatching passwords, please try again"
end if
end considering
end repeat
tell application "Finder"
set theItems to selection
set theItem to (item 1 of input) as alias
set itemPath to quoted form of POSIX path of theItem
set fileName to name of theItem
set theFolder to POSIX path of (container of theItem as alias)
set zipFile to quoted form of (fileName & ".zip")
do shell script "cd '" & theFolder & "'; zip -x .DS_Store -r0 -P '" & final_pass & "' " & zipFile & " ./'" & fileName & "'"
end tell
tell application "Finder"
#I believe this is where the problem is, I am trying to use the variables from above in order to attach the file in mail.
set folderPath to theFolder
set theFile to zipFile
set fileName to zipFile
end tell
set theSubject to fileName
set theBody to "Hello sir. Here is my " & fileName
set theAddress to "Some Email"
set theAttachment to theFile
set theSender to "Some Sender"
tell application "Mail"
set theNewMessage to make new outgoing message with properties {subject:theSubject, content:theBody & return & return, visible:true}
tell theNewMessage
set visibile to true
set sender to theSender
make new to recipient at end of to recipients with properties {address:theAddress}
try
make new attachment with properties {file name:theAttachment} at after the last word of the last paragraph
set message_attachment to 0
on error errmess -- oops
log errmess -- log the error
set message_attachment to 1
end try
log "message_attachment = " & message_attachment
#send
end tell
end tell
return input
end run
【问题讨论】:
-
在第一个
tell application "Finder"block中你有set theItems to selection,然后是set theItem to (item 1 of input) as alias,为什么!?您再次混合了通常不一起使用的 代码。 Finder 上下文中的selection是在Finder 窗口 或桌面 中选择的内容。也就是说,处理调试帮助的问题应该符合How to create a Minimal, Reproducible Example。请参阅What topics can I ask about here? 下的#1。 -
BTW 使用 Script Editor 来帮助 debug 您的 code 而不是尝试 debug它在 Automator 中。
-
您只是将文件名传递给
make new attachment ...,并且您需要将完全限定的文件路径名传递给它。 (也可能作为别名。)此外,您可以使用at after last paragraph而不是at after the last word of the last paragraph -
您的代码有很多问题。没有验证密码不是空的字符串。绝对不需要两个
tell application "Finder"blocks 并且第一个tell application "Finder"block 应该是 code 来获取容器。您在do shell script命令 中使用双单引号。./不是必需的,因为您已经更改了目录并且将在那里创建 .zip 文件。这只是列举一些我认为不正确的事情。 -
感谢您的意见,就像我说 AppleScript 对我来说是新的一样,我通过构建 Automator 工作流程开始了这项工作。我希望将其保留在其中,但需要做更多的事情。
标签: applescript automator