Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/cassandra/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
AppleScript错误可以';将路径转换为类型别名_Applescript - Fatal编程技术网

AppleScript错误可以';将路径转换为类型别名

AppleScript错误可以';将路径转换为类型别名,applescript,Applescript,我正在尝试设置一个AppleScript,它将从桌面上的txt文件(mockUPPath.txt)中获取路径,理论上,这将允许我附加一个jpg。文件夹和jpg的名称将更改,因此我无法硬编码它。我还从桌面上的另一个文本文件中获取了电子邮件的正文,我成功地使用了该文件。我可以得到它来建立电子邮件,但没有找到和附加的jpg mockUPPath.txt文件中的文本如下所示:Name\u Mock-up/555.jpg。 jpg555.jpg位于桌面上的文件夹“Name_Mock-up”中 这就是我到目

我正在尝试设置一个AppleScript,它将从桌面上的txt文件(mockUPPath.txt)中获取路径,理论上,这将允许我附加一个jpg。文件夹和jpg的名称将更改,因此我无法硬编码它。我还从桌面上的另一个文本文件中获取了电子邮件的正文,我成功地使用了该文件。我可以得到它来建立电子邮件,但没有找到和附加的jpg

mockUPPath.txt文件中的文本如下所示:Name\u Mock-up/555.jpg。 jpg555.jpg位于桌面上的文件夹“Name_Mock-up”中 这就是我到目前为止(下文)的情况,有人知道我哪里出了问题吗

set AppleScript's text item delimiters to return
set theFile to ((path to desktop as text) & "mockUPText.txt")
set theData to paragraphs of (read file theFile)

set myPath to ((path to desktop as text) & "mockUPPath.txt")
set thePath to paragraphs of (read file myPath)
set newPath to (POSIX path of (path to desktop folder)) & thePath


--Email

set theMessageSubject to "Test email"
set theRecipient to "email@test.com"
set theMessageContent to theData
set theMessageAttachment to newPath

tell application "Mail"
    activate
    set theMessage to make new outgoing message with properties {visible:true, subject:theMessageSubject, content:theMessageContent & linefeed & linefeed}
    tell theMessage
        make new to recipient at end of to recipients with properties {address:theRecipient}
        make new attachment with properties {file name:theMessageAttachment as alias} at after the last word of the last paragraph
    end tell
end tell

首先,与读/写API交互的最可靠方法是强制路径为
«class furl»
,并删除
文件
关键字

其次,大多数文本文件都是UTF 8编码的。如果是,请将其读作«utf8类»

发生此错误是因为无法将POSIX路径强制为alias。你需要多走一步

文本项分隔符
没有意义,请将正文文本作为一个字符串读取,然后获取路径的第一项

set theFile to ((path to desktop as text) & "mockUPText.txt") as «class furl»
set theData to (read theFile as «class utf8»)

set myPath to ((path to desktop as text) & "mockUPPath.txt") as «class furl»
set thePath to paragraphs of (read myPath as «class utf8»)
set newPath to (POSIX path of (path to desktop folder)) & item 1 of thePath

set newAlias to POSIX file newPath as alias -- if the file doesn't exist you'll get an error


--Email

set theMessageSubject to "Test email"
set theRecipient to "email@test.com"
set theMessageContent to theData
set theMessageAttachment to newAlias

tell application "Mail"
    activate
    set theMessage to make new outgoing message with properties {visible:true, subject:theMessageSubject, content:theMessageContent & linefeed & linefeed}
    tell theMessage
        make new to recipient at end of to recipients with properties {address:theRecipient}
        make new attachment with properties {file name:theMessageAttachment} at after the last word of the last paragraph
    end tell
end tell

根据您在OP中提供的信息,下面的示例AppleScript代码适用于我,在脚本编辑器中进行了测试

首先,我做了以下工作:

  • 在我的桌面上创建了一个名为Name\u Mock-up的文件夹,其中包含脚本编辑器中示例AppleScript代码的屏幕截图
  • 在我的桌面上创建了一个名为mockUPText.txt的文件,其中只有一行文本,Name\u Mock-up/555.jpg
运行示例AppleScript代码:

将文件设置为((桌面路径为文本)和“mockUPText.txt”)作为别名
设置要读取文件的数据
将myPath设置为文件
将路径设置为读取myPath
将newPath设置为(POSIX路径的(桌面文件夹的路径))&路径
将MessageSubject设置为“测试电子邮件”
将收件人设置为“email@test.com"
将消息内容设置为数据
将消息附件设置为新路径
告诉应用程序“邮件”
激活
设置消息以生成新的传出消息,其属性为{visible:true,subject:theMessageSubject,content:theMessageContent&linefeed&linefeed}
告诉他们消息
在属性为{address:theRecipient}的收件人的末尾为收件人新建
在最后一段的最后一个字后创建属性为{file name:theMessageAttachment}的新附件
结束语
结束语

下面是运行示例AppleScript代码的结果屏幕截图:


注意:尽管如此,我不一定要用这种方式编码,只是为了更正我在评论中提到的您最初发布的部分代码。

为什么要将
AppleScript的文本项分隔符设置为
return
?macOS通常使用换行符,因此应将其设置为
linefeed
,而不是
return
。另外,当使用
AppleScript的文本项分隔符时,您应该始终将其设置回
{}
“”
。也就是说,根据您所说的,我认为您没有充分的理由首先设置
AppleScript的文本项分隔符。另外,如果
mockUPPath.txt
只有一行文本,那么就不需要使用
的段落,因为一个简单的
read
命令将返回一行文本。我假设,由于您的代码出错,您实际上没有使用真实的文件、文件夹、,以及OP@user3439894中提供的信息。我知道
段落有问题,但我的建议回答了这个问题。底线是你发布的代码不起作用,并且出现错误,因此我认为它没有真正回答这个问题!它仍然会出错,因为您实际上没有根据OP中提供的真实信息测试代码。@user3439894我确实测试了脚本。你在说什么错误?