AppleScript 错误无法将路径设置为类型别名
AppleScript error Can't make path into type alias
我正在尝试设置一个 AppleScript,它将从桌面上的 txt 文件 (mockUPPath.txt) 获取路径,理论上这将允许我附加一个 jpg。文件夹的名称和 jpg 会改变,所以我不能硬编码。我还从我设法开始工作的桌面上的另一个文本文件中获取了电子邮件的正文。我可以用它来构建电子邮件,但找不到并附加 jpg。
mockUPPath.txt 文件中的文本如下所示:Name_Mock-up/555.jpg。
jpg 555.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
首先,与 read/write API 交互的最可靠方法是将路径强制转换为 «class furl»
并删除 file
关键字。
其次,大多数文本文件都是 UTF 8 编码的。如果是,请阅读 as «class utf8»
错误发生是因为您无法将 POSIX 路径强制转换为别名。您需要一个额外的步骤
Text item delimiters
没有意义,将正文文本作为一个字符串读取并获取 thePath
.
的第一项
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 代码在 Script Editor.
中测试对我有用
首先我做了以下事情:
- 在我的 桌面 上创建了一个名为 Name_Mock-up 的 文件夹,其中包含一个屏幕截图示例 AppleScript code 在 Script Editor 中的命名555.jpg
- 在我的 桌面 上创建了一个名为 mockUPText.txt 的 文件 text、Name_Mock-up/555.jpg.
行
运行示例AppleScript代码:
set theFile to ((path to desktop as text) & "mockUPText.txt") as alias
set theData to read theFile
set myPath to theFile
set thePath to read myPath
set newPath to (POSIX path of (path to desktop folder)) & thePath
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} at after the last word of the last paragraph
end tell
end tell
这是 运行 示例 AppleScript code[=54] 的结果截图=]:
注意:综上所述,我不一定会以这种方式编码它,只是为了纠正一些code 你最初发布的部分是我对你的 OP 的评论中提到的。
我正在尝试设置一个 AppleScript,它将从桌面上的 txt 文件 (mockUPPath.txt) 获取路径,理论上这将允许我附加一个 jpg。文件夹的名称和 jpg 会改变,所以我不能硬编码。我还从我设法开始工作的桌面上的另一个文本文件中获取了电子邮件的正文。我可以用它来构建电子邮件,但找不到并附加 jpg。
mockUPPath.txt 文件中的文本如下所示:Name_Mock-up/555.jpg。 jpg 555.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
首先,与 read/write API 交互的最可靠方法是将路径强制转换为 «class furl»
并删除 file
关键字。
其次,大多数文本文件都是 UTF 8 编码的。如果是,请阅读 as «class utf8»
错误发生是因为您无法将 POSIX 路径强制转换为别名。您需要一个额外的步骤
Text item delimiters
没有意义,将正文文本作为一个字符串读取并获取 thePath
.
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 代码在 Script Editor.
中测试对我有用首先我做了以下事情:
- 在我的 桌面 上创建了一个名为 Name_Mock-up 的 文件夹,其中包含一个屏幕截图示例 AppleScript code 在 Script Editor 中的命名555.jpg
- 在我的 桌面 上创建了一个名为 mockUPText.txt 的 文件 text、Name_Mock-up/555.jpg. 行
运行示例AppleScript代码:
set theFile to ((path to desktop as text) & "mockUPText.txt") as alias
set theData to read theFile
set myPath to theFile
set thePath to read myPath
set newPath to (POSIX path of (path to desktop folder)) & thePath
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} at after the last word of the last paragraph
end tell
end tell
这是 运行 示例 AppleScript code[=54] 的结果截图=]:
注意:综上所述,我不一定会以这种方式编码它,只是为了纠正一些code 你最初发布的部分是我对你的 OP 的评论中提到的。